Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytorch上的1D CNN:mat1和mat2形状不能相乘(10x3和10x2)_Python_Neural Network_Pytorch_Torch - Fatal编程技术网

Python Pytorch上的1D CNN:mat1和mat2形状不能相乘(10x3和10x2)

Python Pytorch上的1D CNN:mat1和mat2形状不能相乘(10x3和10x2),python,neural-network,pytorch,torch,Python,Neural Network,Pytorch,Torch,我有一个样本大小为500的时间序列和两种类型的标签,我想构建一个带有pytorch的1D CNN: class Simple1DCNN(torch.nn.Module): def __init__(self): super(Simple1DCNN, self).__init__() self.layer1 = torch.nn.Conv1d(in_channels=50, out

我有一个样本大小为500的时间序列和两种类型的标签,我想构建一个带有pytorch的1D CNN:

class Simple1DCNN(torch.nn.Module):
    def __init__(self):
        super(Simple1DCNN, self).__init__()
        self.layer1 = torch.nn.Conv1d(in_channels=50, 
                                      out_channels=20, 
                                      kernel_size=5, 
                                      stride=2)
        self.act1 = torch.nn.ReLU()
        self.layer2 = torch.nn.Conv1d(in_channels=20, 
                                      out_channels=10, 
                                      kernel_size=1)
        
        self.fc1 = nn.Linear(10* 1 * 1, 2)
    def forward(self, x):
        x = x.view(1, 50,-1)
        x = self.layer1(x)
        x = self.act1(x)
        x = self.layer2(x)
        x = self.fc1(x)
        
        return x

model = Simple1DCNN()
model(torch.tensor(np.random.uniform(-10, 10, 500)).float())
但收到了以下错误消息:

Traceback (most recent call last):
  File "so_pytorch.py", line 28, in <module>
    model(torch.tensor(np.random.uniform(-10, 10, 500)).float())
  File "/Users/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "so_pytorch.py", line 23, in forward
    x = self.fc1(x)
  File "/Users/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Users/lib/python3.8/site-packages/torch/nn/modules/linear.py", line 93, in forward
    return F.linear(input, self.weight, self.bias)
  File "/Users/lib/python3.8/site-packages/torch/nn/functional.py", line 1692, in linear
    output = input.matmul(weight.t())
RuntimeError: mat1 and mat2 shapes cannot be multiplied (10x3 and 10x2)
回溯(最近一次呼叫最后一次):
文件“so_pytorch.py”,第28行,在
模型(torch.tensor(np.random.uniform(-10,10500)).float())
文件“/Users/lib/python3.8/site packages/torch/nn/modules/module.py”,第727行,在
结果=自我转发(*输入,**kwargs)
文件“so_pytorch.py”,第23行,向前
x=自身.fc1(x)
文件“/Users/lib/python3.8/site packages/torch/nn/modules/module.py”,第727行,在
结果=自我转发(*输入,**kwargs)
文件“/Users/lib/python3.8/site packages/torch/nn/modules/linear.py”,第93行,向前
返回F.linear(输入、自重、自偏压)
文件“/Users/lib/python3.8/site packages/torch/nn/functional.py”,第1692行,线性
输出=输入.matmul(weight.t())
运行时错误:mat1和mat2形状不能相乘(10x3和10x2)

我做错了什么?

x=self.layer2(x)
(也是下一行
x=self.fc1(x)
的输入)的输出形状是
torch.Size([1,10,3])

现在从
self.fc1
的定义来看,它期望其输入的最后一个维度是
10*1*1
,即
10
,而您的输入有
3
,因此出现错误

我不知道你想做什么,但假设你想做的是

  • 将整个
    500
    大小序列标记为两个标签中的一个,然后执行此操作
  • 10
    timesteps分别标记为两个标签中的一个,然后执行此操作
  • 1的输出形状将是(批量大小,2),而2的输出形状将是(批量大小,10,2)

    # replace self.fc1 = nn.Linear(10* 1 * 1, 2) with
    self.fc1 = nn.Linear(10 * 3, 2)
    
    # replace x = self.fc1(x) with
    x = x.view(1, -1)
    x = self.fc1(x)
    
    # replace self.fc1 = nn.Linear(10* 1 * 1, 2) with
    self.fc1 = nn.Linear(2, 2)