import paddle import numpy as np x = np.array([[1,2,3], [4,5,6]]).astype('float32') x = paddle.to_tensor(x) ####################### p =paddle.nn.functional.dropout(x) y_train=p(x)# 这种方式报错! ############################### y_train = paddle.nn.functional.dropout(x, 0.5) ###这是正确的使用方式 print(x) print(y_train) print(x) print("经过drop_out 的数据",y_train)
由于才学习python 和百度paddlepaddle框架,我觉得这个错误的原因是,平时定义卷积神经网络的方式是引用Conv2D等类,然后实例化。这个paddle.nn.functional.dropout()是不是只是个函数?
乱猜的,反正要添加Drop_out层,直接调用这个函数就完了。别再像下面的代码那样错误操作了。
#定义CNN网络 错误的操作示范!(复制代码记得将这行注释掉) class MyCNN(fluid.dygraph.Layer): def __init__(self): super(MyCNN,self).__init__() #self.conv0 = Conv2D(num_channels=3, num_filters=20, filter_size=5, padding=0, act='relu') #输入32*32 输出32-5+1=28*28 #self._batch_norm_0 = BatchNorm(num_channels = 20 , act = None) #self.pool0 = Pool2D(pool_size=2, pool_stride=2, pool_type='max') #输入28*28 输出(28-2)/2+1=14*14 #----------------------错误的操作示范------------------------ self.dropout_0=paddle.nn.functional.dropout(x, p=0.5, axis=None, training=True, mode="downscale_in_infer", name=None) #----------------------------------------------------------------------- #self.conv1 = Conv2D(num_channels=20, num_filters=50, filter_size=5, padding=0, act='relu') #输入14*14 输出(14-5)+1=10*10 #self.pool1 = Pool2D(pool_size=2, pool_stride=2, pool_type='max') #输入10*10 输出(10-2)/2+1=5*5 #self._batch_norm_1 = BatchNorm(num_channels = 50 , act = None) #错误的操作示范 self.dropout_1=paddle.nn.functional.dropout(x, p=0.5, axis=None, training=True, mode="downscale_in_infer", name=None) #self.conv2 = Conv2D(num_channels=50, num_filters=50, filter_size=5, padding=0, act='relu') #输入5*5 输出5-5+1=1*1 #self.pool2 = Pool2D(pool_size=2, pool_stride=2, pool_type='max') #输入1*1 输出1*1 (1-2)/2+1=(-0.5)*(-0.5) #错误的操作示范 self.dropout_2=paddle.nn.functional.dropout(x, p=0.5, axis=None, training=True, mode="downscale_in_infer", name=None) #self.fc1 = Linear(input_dim=50, output_dim=10,act="softmax") #输入50*1,输出10*1 def forward(self,input): x = self.conv0(input) #卷积, x = self.pool0(x) #池化 x = self._batch_norm_0(x) #批归一化 x = self.dropout_0(x) #错误的操作示范 x = self.conv1(x) x = self.pool1(x) x = self._batch_norm_1(x) x = self.dropout_1(x) #错误的操作示范 x = self.conv2(x) x = self.pool2(x) x = fluid.layers.reshape(x, [x.shape[0], -1]) x = self.dropout_2(x) #错误的操作示范 y = self.fc1(x) return y
错误信息如下:
训练开始时间: 2022-01-09 22:04:00 learning-rate: 0.001 ---------------------------------------------------------------------------TypeError Traceback (most recent call last)/tmp/ipykernel_97/3922009731.py in <module> 39 image=fluid.dygraph.to_variable(images) ######fluid.dygraph.to_variable(images) 40 label=fluid.dygraph.to_variable(labels) ---> 41 predict=model(image)#预测 42 #print(predict) 43 loss=fluid.layers.cross_entropy(predict,label) /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py in __call__(self, *inputs, **kwargs) /tmp/ipykernel_97/2768139255.py in forward(self, input) 22 x = self.conv0(input) #卷积, 23 x = self.pool0(x) #池化 ---> 24 x1 = self.drop0(x) 25 x1 = self._batch_norm_0(x1) #批归一化 26 TypeError: 'Tensor' object is not callable