神经网络层不可以放在数组里面吗?
收藏
想通过数组定义网架结构,但是神经网络层不可以放在数组里面吗?
xframe = [1,2,1]
class LineNet(nn.Layer):
def __init__(self):
super(LineNet, self).__init__()
#self.fcs = [nn.Linear(in_features=xframe[i], out_features=xframe[i+1]) for i in range(len(xframe)-1)]#'''
self.fcs = [ nn.Linear(1, 1) ]
def forward(self, x, label=None):
x = paddle.nn.functional.tanh(self.fcs[0](x))
return x
运行就报错:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/tmp/ipykernel_161/1595878955.py in
88 loss = nn.functional.mse_loss(out, label)
89 loss.backward()
---> 90 opt.step()
91 opt.clear_grad()
92 epochidx += 1
in step(self)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/base.py in __impl__(func, *args, **kwargs)
297 def __impl__(func, *args, **kwargs):
298 with _switch_tracer_mode_guard_(is_train=False):
--> 299 return func(*args, **kwargs)
300
301 return __impl__(func)
in step(self)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/wrapped_decorator.py in __impl__(func, *args, **kwargs)
23 def __impl__(func, *args, **kwargs):
24 wrapped_func = decorator_func(func)
---> 25 return wrapped_func(*args, **kwargs)
26
27 return __impl__
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/framework.py in __impl__(*args, **kwargs)
432 assert _non_static_mode(
433 ), "We only support '%s()' in dynamic graph mode, please call 'paddle.disable_static()' to enter dynamic graph mode." % func.__name__
--> 434 return func(*args, **kwargs)
435
436 return __impl__
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/optimizer/optimizer.py in step(self)
1208 """
1209
-> 1210 if not isinstance(self._param_groups[0], dict):
1211 params_grads = []
1212 for param in self._param_groups:
IndexError: list index out of range
0
收藏
请登录后评论
需要使用paddle.nn.LayerList,感谢坛友lfyzzz