深度学习正向传播中DNN和CNN的矩阵参数变化

# [32, 3, 100, 100]
# [32, 3, 100, 200]
# [32, 3, 100, 300]
# [32, 3, 100, 500]
# [32, 3, 100, 100]
# [32, 30000]
# [32, 10]

#定义DNN网络
class MyDNN(fluid.dygraph.Layer):
    def __init__(self):
        super(MyDNN,self).__init__()
        self.hidden1 = Linear(100, 200, act='relu')
        self.hidden2 = Linear(200, 300, act='relu')
        self.hidden3 = Linear(300, 500, act='relu')
        self.hidden4 = Linear(500, 100, act='relu')
        self.hidden5 = Linear(3 * 100 * 100, 10, act='softmax')
    def forward(self,input):
        print(input.shape)
        x = self.hidden1(input)
        print(x.shape)
        x = self.hidden2(x)
        print(x.shape)
        x = self.hidden3(x)
        print(x.shape)
        x = self.hidden4(x)
        print(x.shape)
        x = fluid.layers.reshape(x, shape=[-1, 3 * 100 * 100])
        print(x.shape)
        y = self.hidden5(x)
        print(y.shape)
       
        return y

  

#定义CNN网络
# [128, 1, 20, 20] 输入的图像 [B , C, W, H]
# [128, 28, 16, 16]
# [128, 28, 15, 15]
# [128, 32, 13, 13]
# [128, 32, 12, 12]
# [128, 32, 10, 10]
# [128, 3200]
# [128, 65]
class MyLeNet(fluid.dygraph.Layer):
    def __init__(self):
        super(MyLeNet,self).__init__()
        self.hidden1_1 = Conv2D(1, 28, 5, 1)
        self.hidden1_2 = Pool2D(pool_size=2, pool_type='max', pool_stride=1)
        self.hidden2_1 = Conv2D(28, 32, 3, 1)
        self.hidden2_2 = Pool2D(pool_size=2, pool_type='max', pool_stride=1)
        self.hidden3 = Conv2D(32, 32, 3, 1)
        self.hidden4 = Linear(32 * 10 * 10, 65, act='softmax')
    def forward(self,input):
        #print(input.shape)
        x = self.hidden1_1(input)
        #print(x.shape)
        x = self.hidden1_2(x)
        # print(x.shape)
        x = self.hidden2_1(x)
        # print(x.shape)
        x = self.hidden2_2(x)
        # print(x.shape)
        x = self.hidden3(x)
        # print(x.shape)
        x = fluid.layers.reshape(x, shape=[-1, 32*10*10])
        # print(x.shape)
        y = self.hidden4(x)
        # print(y.shape)
        return y

  

posted @ 2020-04-03 10:42  smagic  阅读(379)  评论(0)    收藏  举报