工作流程
1.用上篇博客编写的函数生成batches
2.搭建网络(两层卷积加一层全连接)
3.训练
补充关键的数学知识
1.卷积后图像的尺寸(ww)大小等于什么
w=(照片的输入尺寸-卷积核尺寸+2padding)➗步数+1
各个参数的注释:
照片的输入尺寸:input_size
卷积核尺寸:filter_size
步数:strides
2.感受野
根据当前图片尺寸反向计算卷积前的尺寸
代码
一、准备工作
导入昨天写的生成batch的函数,当然没什么卵用,只是不想浪费昨天写的代码。
直接通过生成器的方式在当前生成的数组进行训练是更简洁的方法。
import paddleimport paddle.fluid as fluidfrom paddle.fluid.dygraph.nn import Conv2D,Pool2D,Linearimport numpy as npfrom paddle.dataset import mnistdef creat_batch(reader,batch_size,shuffle_size=False):label_batch=[]iamge_flatten_batch=[]if shuffle_size==False:Generator=fluid.io.batch(reader,batch_size)for i in Generator():label_batch_curr=[]iamge_flatten_batch_curr=[]for curr in i:label_batch_curr.append(curr[1])iamge_flatten_batch_curr.append(curr[0].reshape(1,28,28))label_batch.append(np.array(label_batch_curr).reshape(-1,1))iamge_flatten_batch.append(np.array(iamge_flatten_batch_curr))else:Generator=fluid.io.batch(paddle.reader.shuffle(reader,shuffle_size),batch_size)for i in Generator():label_batch_curr=[]iamge_flatten_batch_curr=[]for curr in i:label_batch_curr.append(curr[1])iamge_flatten_batch_curr.append(curr[0].reshape(1,28,28))label_batch.append(np.array(label_batch_curr).astype(\"float32\").reshape(-1,1))iamge_flatten_batch.append(np.array(iamge_flatten_batch_curr))return np.array(iamge_flatten_batch),np.array(label_batch)
搭建模型
class Mnist(fluid.dygraph.Layer):def __init__(self):super(Mnist,self).__init__()self.c1=Conv2D(num_channels=1,num_filters=20,filter_size=5,stride=1,padding=2,act=\"relu\")self.p1=Pool2D(pool_size=2,pool_stride=2,pool_type=\"max\")self.c2=Conv2D(num_channels=20,num_filters=20,filter_size=5,stride=1,padding=2,act=\"relu\")self.p2=Pool2D(pool_size=2,pool_stride=2,pool_type=\"max\")self.fc=Linear(input_dim=980,output_dim=1,act=None)def forward(self,Input):x=self.c1(Input)x=self.p1(x)x=self.c2(x)x=self.p2(x)x=fluid.layers.reshape(x,[x.shape[0],-1])x=self.fc(x)return x
训练
train_data=mnist.train()with fluid.dygraph.guard():model=Mnist()model.train()optimizer=fluid.optimizer.SGDOptimizer(learning_rate=1e-4,parameter_list=model.parameters())for i in range(10):image,label=creat_batch(train_data,16,4)for j in range(image.shape[0]):i_curr=image[j]l_curr=label[j]image_curr=fluid.dygraph.to_variable(i_curr)label_curr=fluid.dygraph.to_variable(l_curr)predict=model(image_curr)loss=fluid.layers.square_error_cost(predict,label_curr)avg_loss=fluid.layers.mean(loss)print(avg_loss)avg_loss.backward()optimizer.minimize(avg_loss)model.clear_gradients()