paddle作业2-4

题1:

#作业2-4
import paddle
from paddle.nn import Conv2D, Linear, MaxPool2D
import numpy as np
import paddle.nn.functional as F
class Net(paddle.nn.Layer):
    def __init__(self):
        super().__init__()
        self.conv1 = Conv2D(in_channels=1, out_channels=3, kernel_size=3)
        self.conv2 = Conv2D(in_channels=3, out_channels=10, kernel_size=3)
        self.maxpool = MaxPool2D(kernel_size=2, stride=2)
        # self.fc = Linear(in_features=)
    def forward(self, x):
        x = self.conv1(x)
        print("conv1--shape:{}".format(x.shape))
        print("content:",x)
        x = self.maxpool(x)
        print("conv1-maxpool--shape:{}".format(x.shape))
        print("content:",x)
        x = self.conv2(x)
        print("conv2--shape:{}".format(x.shape))
        print("content:",x)
        x = self.maxpool(x)
        print("conv2-maxpool--shape:{}".format(x.shape))
        print("content:",x)
        return x

u = np.random.rand(1,1,64,64)
u = paddle.to_tensor(u).astype('float32')
net = Net()
net(u)

输出:
conv1--shape:[1, 3, 62, 62]
content: Tensor(shape=[1, 3, 62, 62], dtype=float32, place=CUDAPlace(0), stop_gradient=False,
       [[[[-1.03177869, -0.77748996, -0.37205794, ..., -1.16914952, -1.03177857, -0.58381915],
          [-0.67789686, -1.58549762, -0.97597510, ..., -0.56963080, -1.82829785, -1.42108345],
          [ 0.25646657, -0.19316070, -0.06493981, ..., -1.43284070, -1.58845973, -1.06161082],
          ...,
          [-1.06657410, -0.12878723, -0.89841843, ...,  0.47988266, -0.54047906, -1.42637694],
          [-0.84822047,  0.06302693, -0.20786586, ..., -0.75426561, -0.53132033, -0.93996078],
          [-1.38931227,  0.21905223, -0.71452707, ..., -1.00175893, -1.16727090, -1.04180014]],

         [[-0.34005663,  1.01721966,  0.46573788, ...,  0.36177707,  0.49334481,  0.93489450],
          [-0.16840670, -0.19559138, -0.35780585, ..., -0.10786653, -0.10211647,  0.27730283],
          [ 0.47618026, -0.57042152,  0.33241624, ..., -0.68744487, -0.44638035,  0.59348673],
          ...,
          [ 0.43587872,  0.37292024, -0.55591142, ...,  0.35407147, -0.51093435,  0.15134723],
          [ 0.77757734, -0.19600271, -0.73693144, ...,  0.06336728, -0.21787606,  0.04249654],
          [ 0.45646501,  0.97877854, -0.10154613, ..., -0.20721525,  0.36874118, -0.34399495]],

         [[ 0.03909174, -0.24251559,  0.32627419, ..., -0.35300124,  0.05717748, -0.15156426],
          [-0.75969607, -0.72273302, -0.21693535, ...,  0.16040878, -0.60953814, -0.76194024],
          [ 0.24897744,  0.20954224, -0.35446906, ..., -0.53350484, -0.65325189, -0.71377838],
          ...,
          [-0.64520627, -0.13371752, -0.00226474, ...,  0.43722275,  0.04000597, -0.68827093],
          [-0.47013941,  0.06369685,  0.07199863, ...,  0.15542001,  0.02171265, -0.53287303],
          [-0.38482872,  0.12792701,  0.20928183, ..., -0.41419211, -0.34249121, -0.37504444]]]])
conv1-maxpool--shape:[1, 3, 31, 31]
content: Tensor(shape=[1, 3, 31, 31], dtype=float32, place=CUDAPlace(0), stop_gradient=False,
       [[[[-0.67789686, -0.37205794, -0.62262011, ...,  0.18379803,  0.14368679, -0.58381915],
          [ 0.25646657, -0.06493981, -0.66757733, ...,  0.08953179,  0.10545183, -0.91407675],
          [ 0.16089660,  0.06359247, -0.30854785, ...,  0.04017820, -0.31908986, -0.17216061],
          ...,
          [-0.38638410,  0.43577391, -0.52118969, ..., -0.50289881, -0.37256008, -0.08074930],
          [-0.12878723, -0.89841843, -0.11317723, ..., -0.04990890,  0.47988266, -0.40975910],
          [ 0.21905223, -0.11454697, -0.56476015, ..., -0.02640892, -0.06468204, -0.53132033]],

         [[ 1.01721966,  0.50235915,  0.48203111, ...,  1.17043245,  0.36177707,  0.93489450],
          [ 0.47618026,  0.49586532,  0.79181617, ..., -0.04261628,  0.30811083,  0.59348673],
          [ 0.99659878,  0.19138895,  0.81142896, ...,  0.85095757,  0.70458364,  0.81303638],
          ...,
          [ 0.68253225,  0.43649405,  0.30807868, ...,  0.93992198,  0.08084880,  0.42516100],
          [ 0.43587872,  0.00076535,  0.90764409, ...,  0.52016580,  0.75320625,  0.33648261],
          [ 0.97877854,  1.03922081,  0.63778490, ...,  0.31145221,  0.45375410,  0.36874118]],

         [[ 0.03909174,  0.32627419, -0.11361890, ...,  0.28261650,  0.16040878,  0.05717748],
          [ 0.24897744,  0.18517545, -0.32565966, ..., -0.11045824,  0.06884519, -0.30248123],
          [ 0.24063696,  0.01648869, -0.21375470, ...,  0.18693715, -0.00632723, -0.00128613],
          ...,
          [-0.14140783,  0.34060207, -0.04902748, ..., -0.13388914, -0.24003626, -0.10710047],
          [-0.13371752, -0.00226474,  0.00684804, ...,  0.11446057,  0.43722275,  0.39217085],
          [ 0.12792701,  0.20928183,  0.22429550, ...,  0.40111300,  0.15542001,  0.02171265]]]])
conv2--shape:[1, 10, 29, 29]
content: Tensor(shape=[1, 10, 29, 29], dtype=float32, place=CUDAPlace(0), stop_gradient=False,
       [[[[ 0.44623196,  0.62388515,  0.17902687, ...,  0.72025532,  0.53890043,  0.24215084],
          [ 1.18032026,  0.27674440, -0.19034995, ...,  0.23119810, -0.05347760,  1.21775162],
          [ 0.63521343,  0.55254978, -0.01318052, ...,  0.48960438,  0.47024673,  0.33397472],
          ...,
          [ 0.30813912,  0.09670782,  0.40232271, ..., -0.10699759,  0.89743155, -0.19544327],
          [-0.10760272,  1.14544046,  0.04281932, ...,  0.48013151,  0.30368382,  0.67060882],
          [ 0.52567440,  0.11980435,  0.17403756, ...,  0.73117226, -0.06727744, -0.12170704]],

         [[ 1.21443903,  0.51579273,  0.85561645, ...,  0.78079736,  0.41128790,  0.91624510],
          [ 0.65348673,  0.45180458,  0.52336526, ...,  0.11084333,  0.14484397, -0.34574771],
          [ 0.59918010,  0.56237960,  0.84820282, ...,  0.83417332,  0.47399926,  0.71474850],
          ...,
          [ 0.32853597, -0.23827338,  0.12160541, ...,  0.79289854,  0.47846511,  0.79586089],
          [ 1.23849845,  0.18347394,  0.56944412, ...,  0.58740354, -0.08288956,  0.29207882],
          [ 0.07357436,  1.00488615,  0.61807150, ...,  0.32044291,  0.12979138,  0.83888710]],

         [[ 0.61238801, -0.20202814,  0.38081983, ...,  0.34322435,  0.65360999,  0.17780209],
          [ 0.08400173,  0.23523483,  0.57989526, ...,  0.26658696,  0.19919813, -0.53452939],
          [-0.00422187,  0.05944386,  0.65046120, ..., -0.08986238,  0.32434759,  0.11998253],
          ...,
          [ 0.07090414,  0.42234129,  0.06542711, ...,  0.66841406, -0.26657850,  0.48105952],
          [ 0.62220156, -0.12735876,  0.69410717, ...,  0.30230391, -0.06783611, -0.17921242],
          [-0.10617691,  0.22948569,  0.47753474, ...,  0.14906025,  0.73869264,  0.67903733]],

         ...,

         [[ 0.58693612, -0.18672022,  0.63746285, ...,  0.46479458,  0.50665700, -0.19183946],
          [ 0.37139094,  0.53905475,  1.19847333, ...,  0.34966975,  0.17246890,  0.32180622],
          [-0.29460990, -0.02060680,  0.92494738, ...,  0.27885771,  1.14765000, -0.23069966],
          ...,
          [ 0.89485133, -0.09401222,  0.00301947, ...,  0.83435619, -0.20381044,  0.80572271],
          [ 0.27982765,  0.57542384,  0.99463135, ..., -0.08496559, -0.18287888,  0.28955793],
          [ 0.57256705,  0.05427426,  0.51110739, ...,  0.06475183,  0.79297543,  0.36223978]],

         [[ 0.47162932,  0.20084937,  0.74264425, ...,  0.49598989,  0.40802330,  0.03926894],
          [ 1.03775215,  0.64674914,  1.08719802, ...,  0.63512015,  0.14761853,  0.79650056],
          [ 0.30375546,  0.38327909,  0.88709396, ...,  0.68090713,  0.49056888,  0.72294033],
          ...,
          [ 0.78672379,  0.37813893,  0.51430082, ...,  0.42658037,  0.96949989,  0.06683272],
          [ 0.53548479,  0.70059055,  0.54305643, ...,  0.12597957, -0.13162267,  1.08301067],
          [ 0.75669861,  0.78659368,  0.64943862, ...,  0.39239758,  0.80140257,  1.17412639]],

         [[-0.03030671, -0.23092379, -0.82495379, ..., -0.02047309, -0.13223293, -0.25102410],
          [-0.98404789, -0.19093001, -0.36319470, ..., -0.16261804, -0.11625568, -0.68067783],
          [-0.27763790, -0.16244254, -0.20049295, ..., -0.49187917, -0.36560684, -0.45228794],
          ...,
          [-0.55032504, -0.06628364, -0.26941240, ...,  0.05824207, -0.91152292, -0.05734545],
          [-0.33500731, -0.82734483, -0.01479590, ..., -0.38682261,  0.02892387, -0.38761553],
          [-0.47419488, -0.25726256, -0.54496294, ..., -0.78106833,  0.23607337,  0.28471929]]]])
conv2-maxpool--shape:[1, 10, 14, 14]
content: Tensor(shape=[1, 10, 14, 14], dtype=float32, place=CUDAPlace(0), stop_gradient=False,
       [[[[ 1.18032026,  0.73366624,  0.78654218, ...,  0.83051729,  0.44774604,  0.72025532],
          [ 0.82915610,  0.91865158,  0.95083821, ...,  1.02050555,  0.44698304,  0.80424589],
          [ 1.14481282,  0.55444986,  0.57537168, ...,  0.38508829,  0.71621978,  0.53432995],
          ...,
          [ 0.69975263,  0.92470175,  0.61134225, ...,  1.01343369,  0.78940690,  1.37072110],
          [ 0.65116763,  1.21608710,  0.53525656, ...,  0.48411304,  0.85110980,  0.77779889],
          [ 1.14544046,  0.91810060,  0.52337706, ...,  0.51168656,  0.81643701,  0.89743155]],

         [[ 1.21443903,  0.94713080,  0.84755552, ...,  1.19978046,  0.74996358,  0.78079736],
          [ 0.59918010,  0.84820282,  1.15260458, ...,  1.61056054,  0.88557887,  0.83417332],
          [ 1.17461693,  0.91230959,  1.09217799, ...,  1.19446337,  0.88778430,  0.61754584],
          ...,
          [ 1.21085870,  0.59851116,  0.84720647, ...,  1.55403900,  1.21921003,  1.58946621],
          [ 0.85086536,  0.99615830,  1.03322124, ...,  0.82606924,  1.39627910,  0.81494212],
          [ 1.23849845,  1.25585008,  0.69309294, ...,  0.99248862,  0.66527927,  0.79289854]],

         [[ 0.61238801,  0.57989526,  0.77676761, ...,  0.21774773,  0.42530563,  0.65360999],
          [ 0.41089585,  1.02843177,  0.72878689, ...,  0.23436627,  0.33608097,  0.32434759],
          [ 1.09274590,  0.87833339,  0.66920429, ...,  0.35917246,  0.26506528,  0.29494682],
          ...,
          [ 0.55407143,  0.56813610,  0.46117276, ..., -0.00291205, -0.13898167,  0.32209030],
          [ 0.33941227,  0.59389615,  0.72072250, ...,  0.56878316,  0.49741602,  0.90810311],
          [ 0.62220156,  0.69410717,  0.23148048, ...,  0.76910746,  0.72130215,  0.66841406]],

         ...,

         [[ 0.58693612,  1.19847333,  0.66977620, ...,  0.89528000,  0.82926500,  0.50665700],
          [ 1.01370800,  0.92494738,  0.78183734, ...,  0.97884059,  0.56055748,  1.14765000],
          [ 0.95192552,  1.06201458,  0.87643880, ...,  1.43337226,  0.59050602,  0.69752371],
          ...,
          [ 0.78550702,  0.82474697,  1.28047204, ...,  0.72682345,  1.14773560,  1.22152841],
          [ 0.73417985,  1.02442372,  0.58345133, ...,  0.72091520,  0.85812181,  1.01788020],
          [ 0.89485133,  0.99463135,  0.73533934, ...,  0.71784866,  1.06157923,  0.83435619]],

         [[ 1.03775215,  1.16232347,  0.72688782, ...,  1.08088934,  0.96468461,  0.63512015],
          [ 1.23473525,  1.06870127,  1.49160647, ...,  1.21071362,  0.75042331,  1.00574136],
          [ 1.49424028,  1.16570246,  1.26114154, ...,  0.70151198,  0.76903617,  0.57344830],
          ...,
          [ 0.81895471,  1.30834210,  1.10446966, ...,  1.15589213,  1.29913092,  1.29558706],
          [ 0.79784775,  0.96286309,  1.38311434, ...,  1.26586366,  1.32836044,  1.12565398],
          [ 0.78672379,  1.02574015,  1.07679272, ...,  0.91598916,  1.00484693,  0.96949989]],

         [[-0.03030671, -0.18821266,  0.00659555, ..., -0.54614818, -0.26359645, -0.02047309],
          [-0.16244254, -0.20049295,  0.21887742, ..., -0.30385906, -0.17079493, -0.07464117],
          [-0.01447295,  0.28109685, -0.50979286, ..., -0.33690268,  0.13603975,  0.45781386],
          ...,
          [ 0.31133324, -0.06370067, -0.25026637, ..., -0.18924497, -0.19423440,  0.27997458],
          [ 0.58236080,  0.08960927,  0.00888072, ...,  0.00078234,  0.02889496, -0.00263232],
          [-0.06628364, -0.01479590, -0.14334446, ...,  0.31529629,  0.36629245,  0.05824207]]]])
Tensor(shape=[1, 10, 14, 14], dtype=float32, place=CUDAPlace(0), stop_gradient=False,
       [[[[ 1.18032026,  0.73366624,  0.78654218, ...,  0.83051729,  0.44774604,  0.72025532],
          [ 0.82915610,  0.91865158,  0.95083821, ...,  1.02050555,  0.44698304,  0.80424589],
          [ 1.14481282,  0.55444986,  0.57537168, ...,  0.38508829,  0.71621978,  0.53432995],
          ...,
          [ 0.69975263,  0.92470175,  0.61134225, ...,  1.01343369,  0.78940690,  1.37072110],
          [ 0.65116763,  1.21608710,  0.53525656, ...,  0.48411304,  0.85110980,  0.77779889],
          [ 1.14544046,  0.91810060,  0.52337706, ...,  0.51168656,  0.81643701,  0.89743155]],

         [[ 1.21443903,  0.94713080,  0.84755552, ...,  1.19978046,  0.74996358,  0.78079736],
          [ 0.59918010,  0.84820282,  1.15260458, ...,  1.61056054,  0.88557887,  0.83417332],
          [ 1.17461693,  0.91230959,  1.09217799, ...,  1.19446337,  0.88778430,  0.61754584],
          ...,
          [ 1.21085870,  0.59851116,  0.84720647, ...,  1.55403900,  1.21921003,  1.58946621],
          [ 0.85086536,  0.99615830,  1.03322124, ...,  0.82606924,  1.39627910,  0.81494212],
          [ 1.23849845,  1.25585008,  0.69309294, ...,  0.99248862,  0.66527927,  0.79289854]],

         [[ 0.61238801,  0.57989526,  0.77676761, ...,  0.21774773,  0.42530563,  0.65360999],
          [ 0.41089585,  1.02843177,  0.72878689, ...,  0.23436627,  0.33608097,  0.32434759],
          [ 1.09274590,  0.87833339,  0.66920429, ...,  0.35917246,  0.26506528,  0.29494682],
          ...,
          [ 0.55407143,  0.56813610,  0.46117276, ..., -0.00291205, -0.13898167,  0.32209030],
          [ 0.33941227,  0.59389615,  0.72072250, ...,  0.56878316,  0.49741602,  0.90810311],
          [ 0.62220156,  0.69410717,  0.23148048, ...,  0.76910746,  0.72130215,  0.66841406]],

         ...,

         [[ 0.58693612,  1.19847333,  0.66977620, ...,  0.89528000,  0.82926500,  0.50665700],
          [ 1.01370800,  0.92494738,  0.78183734, ...,  0.97884059,  0.56055748,  1.14765000],
          [ 0.95192552,  1.06201458,  0.87643880, ...,  1.43337226,  0.59050602,  0.69752371],
          ...,
          [ 0.78550702,  0.82474697,  1.28047204, ...,  0.72682345,  1.14773560,  1.22152841],
          [ 0.73417985,  1.02442372,  0.58345133, ...,  0.72091520,  0.85812181,  1.01788020],
          [ 0.89485133,  0.99463135,  0.73533934, ...,  0.71784866,  1.06157923,  0.83435619]],

         [[ 1.03775215,  1.16232347,  0.72688782, ...,  1.08088934,  0.96468461,  0.63512015],
          [ 1.23473525,  1.06870127,  1.49160647, ...,  1.21071362,  0.75042331,  1.00574136],
          [ 1.49424028,  1.16570246,  1.26114154, ...,  0.70151198,  0.76903617,  0.57344830],
          ...,
          [ 0.81895471,  1.30834210,  1.10446966, ...,  1.15589213,  1.29913092,  1.29558706],
          [ 0.79784775,  0.96286309,  1.38311434, ...,  1.26586366,  1.32836044,  1.12565398],
          [ 0.78672379,  1.02574015,  1.07679272, ...,  0.91598916,  1.00484693,  0.96949989]],

         [[-0.03030671, -0.18821266,  0.00659555, ..., -0.54614818, -0.26359645, -0.02047309],
          [-0.16244254, -0.20049295,  0.21887742, ..., -0.30385906, -0.17079493, -0.07464117],
          [-0.01447295,  0.28109685, -0.50979286, ..., -0.33690268,  0.13603975,  0.45781386],
          ...,
          [ 0.31133324, -0.06370067, -0.25026637, ..., -0.18924497, -0.19423440,  0.27997458],
          [ 0.58236080,  0.08960927,  0.00888072, ...,  0.00078234,  0.02889496, -0.00263232],
          [-0.06628364, -0.01479590, -0.14334446, ...,  0.31529629,  0.36629245,  0.05824207]]]])

 题2:

import matplotlib.pyplot as plt
import paddle.nn.functional as F
from paddle.nn import Conv2D, MaxPool2D, Linear
import paddle
import numpy as np
class MNIST(paddle.nn.Layer):
    def __init__(self):
        super().__init__()
        self.conv1 = Conv2D(in_channels=1, out_channels=20, kernel_size=5, stride=1, padding=2)
        self.maxpool = MaxPool2D(kernel_size=2, stride=2)
        self.conv2 = Conv2D(in_channels=20, out_channels=20, kernel_size=5, stride=1, padding=2)
        self.fc = Linear(in_features=980, out_features=10)
    def forward(self, inputs, label=None, check_shape=False, check_content=False):
        outputs1 = self.conv1(inputs)
        outputs2 = F.relu(outputs1)
        outputs3 = self.maxpool(outputs2)
        outputs4 = self.conv2(outputs3)
        outputs5 = F.relu(outputs4)
        outputs6 = self.maxpool(outputs5)
        outputs6 = paddle.reshape(outputs6, [outputs6.shape[0], 980])
        outputs7 = self.fc(outputs6)
        if check_shape:
            print("\n######## print networks layer's superparams #########")
            print("conv1-- kernel_size:{}, padding:{}, stride:{}".format(self.conv1.weight.shape, self.conv1._padding, self.conv1._stride))
            print("conv2-- kernel_size:{}, padding:{}, stride:{}".format(self.conv2.weight.shape, self.conv2._padding, self.conv2._stride))
            print("fc-- weight_size:{}, bias_size_{}".format(self.fc.weight.shape, self.fc.bias.shape))
            
            #打印每层的输出尺寸
            print("\n####### print shape of features of every layer ###########")
            print("inputs_shape:{}".format(inputs.shape))
            print("outputs1_shape:{}".format(outputs1.shape))
            print("outputs2_shape:{}".format(outputs2.shape))
            print("outputs3_shape:{}".format(outputs3.shape))
            print("outputs4_shape:{}".format(outputs4.shape))
            print("outputs5_shape:{}".format(outputs5.shape))
            print("outputs6_shape:{}".format(outputs6.shape))
            print("outputs7_shape:{}".format(outputs7.shape))
        if check_content:
            print("\n########print convolution layer's kernel ########")
            print("conv1 params -- kernel weights:", self.conv1.weight[0][0])
            print("conv2 params -- kernel weights:", self.conv2.weight[0][0])
            
            #创建随机数,随机打印某一个通道的输出值
            idx1 = np.random.randint(0, outputs1.shape[1])
            idx2 = np.random.randint(0, outputs4.shape[1])
            #打印卷积-池化后的结果,进打印batch中第一个图像对应的特征
            print("\nThe {}th channel of conv1 layer:".format(idx1), outputs1[0][idx1])
            print("The {}th channel of conv2 layer:".format(idx2),outputs4[0][idx2])
            print("The output of last layer:", outputs7[0], '\n')
        if label is not None:
            acc = paddle.metric.accuracy(input=F.softmax(outputs7), label=label)
            return outputs7, acc
        else:
            return outputs7
transform = paddle.vision.transforms.Compose([paddle.vision.ToTensor(),
                                            paddle.vision.Normalize((0.1307,),(0.3081,))])
train_data = paddle.vision.datasets.MNIST(mode='train', transform=transform)
train_loader = paddle.io.DataLoader(train_data, shuffle=True, batch_size=100)
def train(model):
    model.train()
    EPOCH_NUM = 3
    opt = paddle.optimizer.Adam(learning_rate=0.01, weight_decay=paddle.regularizer.L2Decay(), parameters=model.parameters())
    iter1 = 0
    iters = []
    losses = []
    accs = []
    for epoch_id in range(EPOCH_NUM):
        for batch_id, data in enumerate(train_loader()):
            imgs, labels = data
            imgs = paddle.to_tensor(imgs)
            labels = paddle.to_tensor(labels)
            predicts, acc = model(imgs, labels)
            loss = F.cross_entropy(predicts, labels)
            avg_loss = paddle.mean(loss)
            if batch_id % 100 == 0:
                print("epoch:{}, batch:{}, loss:{}, acc:{}".format(epoch_id, batch_id, avg_loss.numpy(), acc.numpy()))
                iters.append(iter1)
                iter1 += 100
                losses.append(avg_loss.numpy())
                accs.append(acc.numpy())
            avg_loss.backward()
            opt.step()
            opt.clear_grad()
    paddle.save(model.state_dict(), "mnist.pdparams")
    return iters, losses, accs
model = MNIST()
iters, losses, accs = train(model)


plt.figure()
plt.title("Accuracy")
plt.xlabel("iter",fontsize=14)
plt.ylabel("accu",fontsize=14)
plt.plot(iters,accs,label='train accu')
plt.legend()
plt.show()

输出: 

paddle作业2-4_第1张图片

 题3:对比两个损失函数(第一个损失函数为交叉熵函数,第二个损失函数为最小二乘法函数)

import matplotlib.pyplot as plt
import paddle.nn.functional as F
from paddle.nn import Conv2D, MaxPool2D, Linear
import paddle
import numpy as np
class MNIST(paddle.nn.Layer):
    def __init__(self):
        super().__init__()
        self.conv1 = Conv2D(in_channels=1, out_channels=20, kernel_size=5, stride=1, padding=2)
        self.maxpool = MaxPool2D(kernel_size=2, stride=2)
        self.conv2 = Conv2D(in_channels=20, out_channels=20, kernel_size=5, stride=1, padding=2)
        self.fc = Linear(in_features=980, out_features=10)
    def forward(self, inputs, label=None, check_shape=False, check_content=False):
        outputs1 = self.conv1(inputs)
        outputs2 = F.relu(outputs1)
        outputs3 = self.maxpool(outputs2)
        outputs4 = self.conv2(outputs3)
        outputs5 = F.relu(outputs4)
        outputs6 = self.maxpool(outputs5)
        outputs6 = paddle.reshape(outputs6, [outputs6.shape[0], 980])
        outputs7 = self.fc(outputs6)
        if check_shape:
            print("\n######## print networks layer's superparams #########")
            print("conv1-- kernel_size:{}, padding:{}, stride:{}".format(self.conv1.weight.shape, self.conv1._padding, self.conv1._stride))
            print("conv2-- kernel_size:{}, padding:{}, stride:{}".format(self.conv2.weight.shape, self.conv2._padding, self.conv2._stride))
            print("fc-- weight_size:{}, bias_size_{}".format(self.fc.weight.shape, self.fc.bias.shape))
            
            #打印每层的输出尺寸
            print("\n####### print shape of features of every layer ###########")
            print("inputs_shape:{}".format(inputs.shape))
            print("outputs1_shape:{}".format(outputs1.shape))
            print("outputs2_shape:{}".format(outputs2.shape))
            print("outputs3_shape:{}".format(outputs3.shape))
            print("outputs4_shape:{}".format(outputs4.shape))
            print("outputs5_shape:{}".format(outputs5.shape))
            print("outputs6_shape:{}".format(outputs6.shape))
            print("outputs7_shape:{}".format(outputs7.shape))
        if check_content:
            print("\n########print convolution layer's kernel ########")
            print("conv1 params -- kernel weights:", self.conv1.weight[0][0])
            print("conv2 params -- kernel weights:", self.conv2.weight[0][0])
            
            #创建随机数,随机打印某一个通道的输出值
            idx1 = np.random.randint(0, outputs1.shape[1])
            idx2 = np.random.randint(0, outputs4.shape[1])
            #打印卷积-池化后的结果,进打印batch中第一个图像对应的特征
            print("\nThe {}th channel of conv1 layer:".format(idx1), outputs1[0][idx1])
            print("The {}th channel of conv2 layer:".format(idx2),outputs4[0][idx2])
            print("The output of last layer:", outputs7[0], '\n')
        if label is not None:
            acc = paddle.metric.accuracy(input=F.softmax(outputs7), label=label)
            return outputs7, acc
        else:
            return outputs7
transform = paddle.vision.transforms.Compose([paddle.vision.ToTensor(),
                                            paddle.vision.Normalize((0.1307,),(0.3081,))])
train_data = paddle.vision.datasets.MNIST(mode='train', transform=transform)
train_loader = paddle.io.DataLoader(train_data, shuffle=True, batch_size=100)
def train(model):
    model.train()
    EPOCH_NUM = 3
    opt = paddle.optimizer.Adam(learning_rate=0.01, weight_decay=paddle.regularizer.L2Decay(), parameters=model.parameters())
    iter1 = 0
    iters = []
    losses = []
    accs = []
    for epoch_id in range(EPOCH_NUM):
        for batch_id, data in enumerate(train_loader()):
            imgs, labels = data
            imgs = paddle.to_tensor(imgs)
            labels = paddle.to_tensor(labels)
            predicts, acc = model(imgs, labels)
            loss = F.cross_entropy(predicts, labels)
            avg_loss = paddle.mean(loss)
            if batch_id % 100 == 0:
                print("epoch:{}, batch:{}, loss:{}, acc:{}".format(epoch_id, batch_id, avg_loss.numpy(), acc.numpy()))
                iters.append(iter1)
                iter1 += 100
                losses.append(avg_loss.numpy())
                accs.append(acc.numpy())
            avg_loss.backward()
            opt.step()
            opt.clear_grad()
    paddle.save(model.state_dict(), "mnist.pdparams")
    return iters, losses, accs
model = MNIST()
iters, losses, accs = train(model)

输出:

epoch:0, batch:0, loss:[4.154338], acc:[0.08]
epoch:0, batch:100, loss:[0.286233], acc:[0.9]
epoch:0, batch:200, loss:[0.22489373], acc:[0.94]
epoch:0, batch:300, loss:[0.19534074], acc:[0.94]
epoch:0, batch:400, loss:[0.17166412], acc:[0.93]
epoch:0, batch:500, loss:[0.23943755], acc:[0.91]
epoch:1, batch:0, loss:[0.10949133], acc:[0.96]
epoch:1, batch:100, loss:[0.2541501], acc:[0.92]
epoch:1, batch:200, loss:[0.06903129], acc:[0.98]
epoch:1, batch:300, loss:[0.10762461], acc:[0.96]
epoch:1, batch:400, loss:[0.03312943], acc:[0.99]
epoch:1, batch:500, loss:[0.08275294], acc:[0.96]
epoch:2, batch:0, loss:[0.06648406], acc:[0.99]
epoch:2, batch:100, loss:[0.04650707], acc:[0.97]
epoch:2, batch:200, loss:[0.08721491], acc:[0.97]
epoch:2, batch:300, loss:[0.05407384], acc:[0.99]
epoch:2, batch:400, loss:[0.10466648], acc:[0.95]
epoch:2, batch:500, loss:[0.028617], acc:[0.99]
plt.figure()
plt.plot(iters, accs)
plt.show()

输出:

paddle作业2-4_第2张图片

 

import matplotlib.pyplot as plt
import paddle.nn.functional as F
from paddle.nn import Conv2D, MaxPool2D, Linear
import paddle
import numpy as np
class MNIST(paddle.nn.Layer):
    def __init__(self):
        super().__init__()
        self.conv1 = Conv2D(in_channels=1, out_channels=20, kernel_size=5, stride=1, padding=2)
        self.maxpool = MaxPool2D(kernel_size=2, stride=2)
        self.conv2 = Conv2D(in_channels=20, out_channels=20, kernel_size=5, stride=1, padding=2)
        self.fc = Linear(in_features=980, out_features=10)
    def forward(self, inputs, label=None, check_shape=False, check_content=False):
        outputs1 = self.conv1(inputs)
        outputs2 = F.relu(outputs1)
        outputs3 = self.maxpool(outputs2)
        outputs4 = self.conv2(outputs3)
        outputs5 = F.relu(outputs4)
        outputs6 = self.maxpool(outputs5)
        outputs6 = paddle.reshape(outputs6, [outputs6.shape[0], 980])
        outputs7 = self.fc(outputs6)
        if check_shape:
            print("\n######## print networks layer's superparams #########")
            print("conv1-- kernel_size:{}, padding:{}, stride:{}".format(self.conv1.weight.shape, self.conv1._padding, self.conv1._stride))
            print("conv2-- kernel_size:{}, padding:{}, stride:{}".format(self.conv2.weight.shape, self.conv2._padding, self.conv2._stride))
            print("fc-- weight_size:{}, bias_size_{}".format(self.fc.weight.shape, self.fc.bias.shape))
            
            #打印每层的输出尺寸
            print("\n####### print shape of features of every layer ###########")
            print("inputs_shape:{}".format(inputs.shape))
            print("outputs1_shape:{}".format(outputs1.shape))
            print("outputs2_shape:{}".format(outputs2.shape))
            print("outputs3_shape:{}".format(outputs3.shape))
            print("outputs4_shape:{}".format(outputs4.shape))
            print("outputs5_shape:{}".format(outputs5.shape))
            print("outputs6_shape:{}".format(outputs6.shape))
            print("outputs7_shape:{}".format(outputs7.shape))
        if check_content:
            print("\n########print convolution layer's kernel ########")
            print("conv1 params -- kernel weights:", self.conv1.weight[0][0])
            print("conv2 params -- kernel weights:", self.conv2.weight[0][0])
            
            #创建随机数,随机打印某一个通道的输出值
            idx1 = np.random.randint(0, outputs1.shape[1])
            idx2 = np.random.randint(0, outputs4.shape[1])
            #打印卷积-池化后的结果,进打印batch中第一个图像对应的特征
            print("\nThe {}th channel of conv1 layer:".format(idx1), outputs1[0][idx1])
            print("The {}th channel of conv2 layer:".format(idx2),outputs4[0][idx2])
            print("The output of last layer:", outputs7[0], '\n')
        if label is not None:
            acc = paddle.metric.accuracy(input=F.softmax(outputs7), label=label)
            return outputs7, acc
        else:
            return outputs7
transform = paddle.vision.transforms.Compose([paddle.vision.ToTensor(),
                                            paddle.vision.Normalize((0.1307,),(0.3081,))])
train_data = paddle.vision.datasets.MNIST(mode='train', transform=transform)
train_loader = paddle.io.DataLoader(train_data, shuffle=True, batch_size=100)
def train(model):
    model.train()
    EPOCH_NUM = 3
    opt = paddle.optimizer.Adam(learning_rate=0.01, weight_decay=paddle.regularizer.L2Decay(), parameters=model.parameters())
    iter1 = 0
    iters = []
    losses = []
    accs = []
    for epoch_id in range(EPOCH_NUM):
        for batch_id, data in enumerate(train_loader()):
            imgs, labels = data
            imgs = paddle.to_tensor(imgs)
            labels = paddle.to_tensor(labels)
            predicts, acc = model(imgs, labels)
            predicts = predicts.astype('float32')
            labels = labels.astype('float32')
            loss = F.square_error_cost(predicts, labels)
            avg_loss = paddle.mean(loss)
            if batch_id % 100 == 0:
                print("epoch:{}, batch:{}, loss:{}, acc:{}".format(epoch_id, batch_id, avg_loss.numpy(), acc.numpy()))
                iters.append(iter1)
                iter1 += 100
                losses.append(avg_loss.numpy())
                accs.append(acc.numpy())
            avg_loss.backward()
            opt.step()
            opt.clear_grad()
    paddle.save(model.state_dict(), "mnist1.pdparams")
    return iters, losses, accs
model = MNIST()
iters, losses, accs = train(model)

输出:

epoch:0, batch:0, loss:[41.008236], acc:[0.18]
epoch:0, batch:100, loss:[21.90275], acc:[0.13]
epoch:0, batch:200, loss:[14.258028], acc:[0.12]
epoch:0, batch:300, loss:[14.339616], acc:[0.1]
epoch:0, batch:400, loss:[10.583021], acc:[0.1]
epoch:0, batch:500, loss:[10.328342], acc:[0.05]
epoch:1, batch:0, loss:[9.5673275], acc:[0.1]
epoch:1, batch:100, loss:[9.391169], acc:[0.04]
epoch:1, batch:200, loss:[8.717621], acc:[0.11]
epoch:1, batch:300, loss:[8.4292], acc:[0.07]
epoch:1, batch:400, loss:[9.091347], acc:[0.11]
epoch:1, batch:500, loss:[8.235337], acc:[0.11]
epoch:2, batch:0, loss:[9.305361], acc:[0.13]
epoch:2, batch:100, loss:[8.223233], acc:[0.09]
epoch:2, batch:200, loss:[9.535088], acc:[0.09]
epoch:2, batch:300, loss:[7.5045133], acc:[0.1]
epoch:2, batch:400, loss:[8.797383], acc:[0.07]
epoch:2, batch:500, loss:[9.310794], acc:[0.09]
plt.figure()
plt.plot(iters, accs)
plt.show()

 输出:

paddle作业2-4_第3张图片

 

你可能感兴趣的:(paddle,paddle,python,深度学习)