Paddle Linear层 实例详解

>>> import paddle
>>> import paddle.fluid as fluid
>>> from paddle.fluid.dygraph.nn import Linear
>>> import numpy as np
>>> np.random.seed(500) #生成样本数据
>>> data = np.random.rand(2,3,3,3).astype(np.float32)
>>>data
array([[[[0.6936795 , 0.06171699, 0.6666116 ],
         [0.5592089 , 0.08511062, 0.39241907],
         [0.49707752, 0.3321491 , 0.34233844]],

        [[0.36563683, 0.8506258 , 0.44849744],
         [0.31647202, 0.36173317, 0.45072243],
         [0.91526806, 0.45995858, 0.27367947]],

        [[0.91749614, 0.45953223, 0.5445912 ],
         [0.6906904 , 0.8308381 , 0.6359093 ],
         [0.64981717, 0.95375836, 0.5699544 ]]],


       [[[0.6352015 , 0.03646224, 0.74912673],
         [0.8580684 , 0.48540735, 0.23216176],
         [0.50338805, 0.13613188, 0.1730703 ]],

        [[0.10495835, 0.24399056, 0.6607235 ],
         [0.6013877 , 0.6612481 , 0.92053974],
         [0.9552766 , 0.8237738 , 0.13482119]],

        [[0.7702043 , 0.5734177 , 0.7452945 ],
         [0.2655834 , 0.88600963, 0.28006935],
         [0.25279555, 0.43771923, 0.7263506 ]]]], dtype=float32)
# Linear 进行全连接操作
>>> with fluid.dygraph.guard():
...     fc = Linear(input_dim=3, output_dim=1)
...     x = fluid.dygraph.to_variable(data)
...     y = fc(x)
...     y = y.numpy()
...
>>> print(y.shape) # 输出y数据的属性结果
(2, 3, 3, 1)
>>>
>>> y  # 输出y数据
array([[[[-0.3612218 ],
         [-0.2644405 ],
         [-0.26413566]],

        [[-0.30829245],
         [-0.23571613],
         [-0.39218292]],

        [[-0.45066255],
         [-0.44509062],
         [-0.43313923]]],


       [[[-0.35800967],
         [-0.36889583],
         [-0.20663647]],

        [[-0.2020746 ],
         [-0.4584977 ],
         [-0.41793662]],

        [[-0.4621275 ],
         [-0.24601594],
         [-0.2841791 ]]]], dtype=float32)
>>> fc.parameters() #查看网络 shape 是3  这个与input_dim=3相同
[Parameter containing:
Tensor(shape=[3, 1], dtype=float32, place=CPUPlace, stop_gradient=False,
       [[-0.30495870],
        [-0.11875451],
        [-0.21354115]]), Parameter containing:
Tensor(shape=[1], dtype=float32, place=CPUPlace, stop_gradient=False,
       [0.])]
>>>

将3修改为27试一试,结果报错了,如下:

>>> with fluid.dygraph.guard():
...     fc = Linear(input_dim=27, output_dim=1)
...     x = fluid.dygraph.to_variable(data)
...     y = fc(x)
...     y = y.numpy()
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "C:\Users\YANG\.conda\envs\python38\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "C:\Users\YANG\.conda\envs\python38\lib\site-packages\paddle\fluid\dygraph\nn.py", line 983, in forward
    _C_ops.matmul(input, self.weight, pre_bias, 'transpose_X', False,
ValueError: (InvalidArgument) The fisrt matrix width should be same as second matrix height,but received fisrt matrix width 3, second matrix height 27
  [Hint: Expected dim_a.width_ == dim_b.height_, but received dim_a.width_:3 != dim_b.height_:27.] (at C:\home\workspace\Paddle_release\paddle/fluid/operators/math/blas_impl.h:1258)
  [operator < matmul > error]
>>>

这说明模型不会自动进行维度转换,需要手动修改

>>> with fluid.dygraph.guard():
...     fc = Linear(input_dim=27, output_dim=1)
...     tran_data = data.reshape(2,27)
...     x = fluid.dygraph.to_variable(tran_data)
...     y = fc(x)
...     y = y.numpy()
...
>>> print(y.shape)
(2, 1)
>>> fc.parameters()
[Parameter containing:
Tensor(shape=[27, 1], dtype=float32, place=CPUPlace, stop_gradient=False,
       [[-0.04505026],
        [-0.04598817],
        [ 0.03557009],
        [-0.40564117],
        [-0.19702300],
        [ 0.30788404],
        [ 0.17336142],
        [-0.31959006],
        [-0.13518333],
        [-0.20228916],
        [ 0.09338391],
        [-0.10551131],
        [-0.08375382],
        [-0.41486526],
        [ 0.05802917],
        [-0.24436146],
        [-0.22367257],
        [ 0.35788244],
        [-0.32128781],
        [-0.16492695],
        [-0.05866092],
        [ 0.18266976],
        [-0.35306722],
        [-0.32018191],
        [-0.33231631],
        [-0.02183020],
        [ 0.43464482]]), Parameter containing:
Tensor(shape=[1], dtype=float32, place=CPUPlace, stop_gradient=False,
       [0.])]
>>> print(y)
[[-1.3826181]
 [-1.5819726]]
>>> y
array([[-1.3826181],
       [-1.5819726]], dtype=float32)
>>>

修改后的输出 

到此时

我要是可以保证输入的数据相同,输出的数据不仅形状一致而且值一致,那就更好了,这需要我们给定统一的初始化权重参数

我们再进行修改:

with fluid.dygraph.guard():
...     w =np.zeros((27,1))
...     w[0,0] = .5
...     w[1,0] = .5
...     fc = Linear(input_dim=27, output_dim=1,param_attr= fluid.initializer.NumpyArrayInitializer(value=w))
...     train_data = data.reshape(2,27)
...     x = fluid.dygraph.to_variable(train_data)
...     y = fc(x)
...     y = y.numpy()
...
>>> print(y.shape)
(2, 1)
>>> fc.parameters()
[Parameter containing:
Tensor(shape=[27, 1], dtype=float32, place=CPUPlace, stop_gradient=False,
       [[0.50000000],
        [0.50000000],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ],
        [0.        ]]), Parameter containing:
Tensor(shape=[1], dtype=float32, place=CPUPlace, stop_gradient=False,
       [0.])]

在实际使用的时候Linear是作为最后一层使用的,其输入是和前面的输出有关的,也就是,在它的前面需要存在一层操作也就是flatten, 从而实现自动的操作

>>> with fluid.dygraph.guard():
...     w =np.zeros((27,1))
...     w[0,0] = .5
...     w[1,0] = .5
...     fc = Linear(input_dim=27, output_dim=1,param_attr= fluid.initializer.NumpyArrayInitializer(value=w))
...     x = fluid.dygraph.to_variable(train_data)
...     y = fluid.layers.flatten(x,axis=1)
...     y = fc(x)
...     y = y.numpy()
...
>>> print(y)
[[0.37769824]
 [0.33583188]]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值