通过trace方式将包含L.batch_norm的动态图保存时, 报错`NotFoundError: Persistable var batch_norm_1.w_1 does not exist`
Created by: miraiwk
我在用TracedLayer
保存调用了paddle.fluid.layers.batch_norm
的动态图时, PaddlePaddle报错NotFoundError: Persistable var batch_norm_1.w_1 does not exist
。 但如果去掉batch_norm的调用, 能够成功保存。请问应该如何解决呢? 谢谢!
- 版本、环境信息: 1)PaddlePaddle版本:
对主分支的代码进行编译,Only-CPU版本: 9373cf5a
- 复现代码
from paddle import fluid
import paddle.fluid.layers as L
from paddle.fluid.dygraph import to_variable, TracedLayer
import numpy as np
class MyInstanceNorm2d(fluid.dygraph.Layer):
def __init__(self, num_features, eps=1e-5):
super(MyInstanceNorm2d, self).__init__()
self.num_features = num_features
self.eps = eps
def forward(self, x):
N, C, H, W = x.shape
x_r = L.reshape(x, (1, N*C, H, W))
y_r = L.batch_norm(x_r, epsilon=self.eps, do_model_average_for_mean_and_var=False, use_global_stats=False)
return L.reshape(y_r, (N, C, H, W))
with fluid.dygraph.guard():
x = np.random.uniform(-1, 1, size=(2, 3, 4, 5))
px = to_variable(x)
model = MyInstanceNorm2d(x.shape[1])
py = model(px)
print(py)
in_var = px
out_dygraph, static_layer = TracedLayer.trace(model, inputs=[in_var])
out_static_graph = static_layer([in_var])
print(len(out_static_graph))
print(out_static_graph[0].shape)
dirname = './save_infer_model'
static_layer.save_inference_model(dirname=dirname)