import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D,Pool2D,Linear

class ConvolutionalNeuralNetwork(fluid.dygraph.Layer):
    def __init__(self,name_scope):
        super(ConvolutionalNeuralNetwork,self).__init__(name_scope)
        #卷积层，使用20个5*5的滤波器，激活函数为relu
        self.conv1=Conv2D(num_channels=1,num_filters=20,filter_size=5,act='relu')
        self.pool1=Pool2D(pool_size=2,pool_stride=2,pool_type='max')
        self.conv2=Conv2D(num_channels=20,num_filters=50,filter_size=5,act='relu')
        self.pool2=Pool2D(pool_size=2,pool_stride=2,pool_type='max')
        self.fc=Linear(input_dim=800,output_dim=10,act='softmax')

    def forward(self,x):
        x=self.conv1(x)
        x=self.pool1(x)
        x=self.conv2(x)
        x=self.pool2(x)
        #因为最后一层为全连接层，需要将数据reshape为一维向量
        #print(x.shape[0])
        x=fluid.layers.reshape(x,[x.shape[0],-1])
        x=self.fc(x)
        return(x)
