AIstudio平台:动态图继承Layer运行后自动重启
Created by: DrRyanHuang
-
环境
AIstudio平台
python3.7
PaddlePaddle 1.7.1
-
报错信息:直接运行会使得AIstudio重新启动,但是略微改动可以运行,后面会提供复现步骤
-
报错代码以及复现步骤:(已将代码简化)
from paddle.fluid.dygraph import Conv2D, Layer, Pool2D, Linear, Sequential, to_variable
from paddle.fluid.layers import flatten, concat
import paddle.fluid as fluid
import numpy as np
class Inception(Layer):
def __init__(self, num_channels, ch1x1, ch3x3reduced, ch3x3, ch5x5reduced, ch5x5, pool_proj):
super(Inception, self).__init__()
self.branch11 = Conv2D(num_channels=3,
num_filters=10,
filter_size=3,
stride=1,
padding=1,
act='leaky_relu',
bias_attr=False,
# param_attr=conv_weight,
)
def forward(self, inputs):
print(inputs.shape)
print(self.branch11)
branch1 = self.branch11(inputs)
print(branch1.shape)
return inputs
model = Inception(3, 5, 5, 5, 5, 5, 5)
with fluid.dygraph.guard():
a = np.ones(shape=(4, 3, 5, 5), dtype=np.float32)
a = to_variable(a)
out = model(a)
print(out.shape)
直接运行以上代码会使得AIstudio重新启动
将forward函数改一下让其报错
def forward(self, inputs):
print(inputs.shape)
print(self.branch1) # <---------------将 `branch11` 改为 `branch1`
branch1 = self.branch11(inputs)
print(branch1.shape)
return inputs
按 Enter + shift
运行,当然会报错说没有该属性:
AttributeError: 'Inception' object has no attribute 'branch1'
这时再将 forward
函数改回来:
def forward(self, inputs):
print(inputs.shape)
print(self.branch11) # <---------------将 `branch1` 改为 `branch11`
branch1 = self.branch11(inputs)
print(branch1.shape)
return inputs
这时平台可运行,且没有问题 输出为:
[4, 3, 5, 5]
<paddle.fluid.dygraph.nn.Conv2D object at 0x7f8a69ae4170>
[4, 10, 5, 5]
[4, 3, 5, 5]
以上是我遇到的问题,请您帮忙看看,不胜感激!