Paddle < 论文复现> 在将参数传入Linear时报错

guykilcj  于 2022-10-20  发布在  其他
关注(0)|答案(6)|浏览(201)

Paddle版本 1.8.4(在本地还有aistudio上都进行过尝试,均报错),使用cpu训练,单机单卡,在将一个LoDTensor传入Linear层的时候出现报错:

x type and shape: VarType.LOD_TENSOR VarType.FP32 [56, 25733]

Traceback (most recent call last):
File "paddle_demo.py", line 75, in
patience=config['patience']
File "/home/aistudio/work/train_VASC.py", line 282, in vasc
decoded_x,z_mean = model([fluid.dygraph.base.to_variable(expr_train),tau])
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 461, incall
outputs = self.forward(inputs,kwargs)
File "/home/aistudio/work/train_VASC.py", line 86, in forward
x = self.fc1(x)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 461, in
call
*
outputs = self.forward(*inputs,**kwargs)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/nn.py", line 943, in forward
'transpose_Y', False, "alpha", 1)
TypeError: matmul(): incompatible function arguments. The following argument types are supported:

  1. (arg0: paddle::imperative::VarBase, arg1: paddle::imperative::VarBase, arg2: paddle::imperative::VarBase, *args) -> paddle::imperative::VarBase

Invoked with: <paddle.fluid.core_avx.VarBase object at 0x7f998f973bb0>, name: "linear_0.w_0"
type {
type: LOD_TENSOR
lod_tensor {
tensor {
data_type: FP32
dims: 25733
dims: 512
}
}
}
persistable: true
, <paddle.fluid.core_avx.VarBase object at 0x7f998f973c30>, 'transpose_X', False, 'transpose_Y', False, 'alpha', 1
其中x type是我打印出传入Linear层的datatype,确实是lodtensor。 我在网络上也搜索了一些类似情况的解决方案,提到的都是忘记使用dygraph.to_variable转化输入数据所以出错了,但是这份代码似乎不是因为参数类型的原因。
self.fc1的实现如下:
self.fc1 = Linear(input_dim=self.in_dim,output_dim=512)
是不是因为某些设置或配置的问题?

9njqaruj

9njqaruj1#

可以将相关代码贴一下吗?log来看确实是参数类型不匹配的问题。

sy5wg1nm

sy5wg1nm2#

def __init__(self,in_dim,latent=2,var=False):
       super(PVASC,self).__init__()
       self.in_dim = in_di
       self.latent = latent
       self.var = var

       l1 = fluid.regularizer.L1Decay(regularization_coeff=0.01)
       w_param = fluid.ParamAttr(regularizer=l1)
       self.fc1 = Linear(input_dim=self.in_dim,output_dim=512)
      ##下面的代码都没运行到就报错了

def forward(self,input_list):
       self.input_data,tau = input_list
       x = L.dropout(self.input_data, dropout_prob=0.5)
       print("x type and shape:",x.type," ",x.dtype," ",x.shape)
       x = self.fc1(x)
       ##就是在self.sc1(x)这里报错

然后调用model的时候是这么用的:
decoded_x,z_mean = model([fluid.dygraph.base.to_variable(expr_train),tau])

全部的代码我有在aistudio分享 链接是 https://aistudio.baidu.com/aistudio/projectdetail/1114094?shared=1
进去之后在终端运行python paddle_demo.py就好了 不过我感觉这个bug和剩下部分的代码应该关系不太大?

iyfamqjs

iyfamqjs3#

这个dropout是静态图的,动态图Dropout参考:https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/dygraph_cn/Dropout_cn.html#dropout

cnjp1d6j

cnjp1d6j4#

好像还是不行..

def __init__(self,in_dim,latent=2,var=False):
        super(PVASC,self).__init__()
        self.in_dim = in_dim
        self.latent = latent
        self.var = var
        self.dropoutLayer =fluid.dygraph.Dropout(p=0.5)

def forward(self,input_list):
        self.input_data,tau = input_list
        x = self.dropoutLayer(self.input_data)
        # x = fluid.dygraph.to_variable(self.input_data)
        # print(self.in_dim)
        # print("x shape:")
        # print(x.shape)
        # x = L.reshape(x,shape=[-1,self.in_dim])
        #x = fluid.dygraph.to_variable(x)
        print("x type and shape:",x.type," ",x.dtype," ",x.shape)
        x = self.fc1(x)

Traceback (most recent call last):
File "paddle_demo.py", line 75, in
patience=config['patience']
File "/home/aistudio/work/train_VASC.py", line 283, in vasc
decoded_x,z_mean = model([fluid.dygraph.base.to_variable(expr_train),tau])
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 461, incall
outputs = self.forward(inputs,kwargs)
File "/home/aistudio/work/train_VASC.py", line 87, in forward
x = self.fc1(x)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py", line 461, in
call
*
outputs = self.forward(*inputs,**kwargs)
File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/nn.py", line 943, in forward
'transpose_Y', False, "alpha", 1)
TypeError: matmul(): incompatible function arguments. The following argument types are supported:

  1. (arg0: paddle::imperative::VarBase, arg1: paddle::imperative::VarBase, arg2: paddle::imperative::VarBase, *args) -> paddle::imperative::VarBase

Invoked with: <paddle.fluid.core_avx.VarBase object at 0x7f21c9de66b0>, name: "linear_0.w_0"
type {
type: LOD_TENSOR
lod_tensor {
tensor {
data_type: FP32
dims: 25733
dims: 512
}
}
}
persistable: true
, <paddle.fluid.core_avx.VarBase object at 0x7f21c2709670>, 'transpose_X', False, 'transpose_Y', False, 'alpha', 1

b09cbbtk

b09cbbtk5#

你的代码需要在动态图guard中声明模型。

请参考1.8的文档熟悉一下动态图使用: https://www.paddlepaddle.org.cn/documentation/docs/zh/beginners_guide/basic_concept/dygraph/DyGraph.html

目前动态图还在快速开发阶段,2.0版本发布后才会趋于稳定,所以变动较大。

vhipe2zx

vhipe2zx6#

好的,我把model的声明挪进guard之后就没有这歌错误了 谢谢1

相关问题