【问题标题】:Feeding a 1D vector to Pytorch CNN将一维向量输入 Pytorch CNN
【发布时间】:2020-08-30 12:39:44
【问题描述】:

如何将一维向量输入到 Pytorch CNN 数据的形状为 (244, 108)。它包含一天内每分钟交易收盘价的百分比变化,即 108 个值,类似 244 天。基本上它是一个一维向量。

如何将此数据加载到 conv1d 以进行回归。 in_channel、out_channel 和 kernal_size 是什么?

数据:

x.shape = (243, 108)
y.shape = (243,)

模型(我试过):-

class Net(torch.nn.Module):   
    def __init__(self):
        super(Net, self).__init__()

        self.layer1 = torch.nn.Conv1d(in_channels=1, out_channels=1, kernel_size=3, stride=1)
        self.act1 = torch.nn.ReLU()
        self.act2 = torch.nn.AdaptiveMaxPool1d(1)
        self.layer2 = torch.nn.Conv1d(in_channels=1, out_channels=1, kernel_size=3, stride=1)
        self.act3 = torch.nn.ReLU()
        self.act4 = torch.nn.AdaptiveMaxPool1d(1)


        self.linear_layers = nn.Linear(1, 1)


    # Defining the forward pass    
    def forward(self, x):
        x = self.layer1(x)
        x = self.act1(x)
        x = self.act2(x)
        x = self.layer2(x)
        x = self.act3(x)
        x = self.act4(x)
        x = x.reshape(x.shape[0], -1)
        x = self.linear_layers(x)
        return x

【问题讨论】:

  • 我也不是专家,但我认为您的 kernel_size=3 会出错,因为您使用的是 1D Conv。
  • @Seankala 为什么会这样?它与一维卷积有什么关系?
  • 这能回答你的问题吗? Creating and shaping data for 1D CNN

标签: pytorch regression conv-neural-network


【解决方案1】:

我不确定这是否是您想要实现的目标,但是

>>> import torch
>>> import torch.nn as nn
>>> class Net(nn.Module):
...     def __init__(self): 
...         super().__init__() 
...         self.layer1 = nn.Conv1d(in_channels=1, out_channels=1, kernel_size=1) 
...         self.act1 = nn.ReLU() 
...         self.act2 = nn.AdaptiveMaxPool1d(output_size=1) 
...
...         self.layer2 = nn.Conv1d(in_channels=1, out_channels=1, kernel_size=1) 
...         self.act3 = nn.ReLU() 
...         self.act4 = nn.AdaptiveMaxPool1d(output_size=1)
...
...         self.linear_layer = nn.Linear(in_features=1, out_features=1)
...
...     def forward(self, x): 
...         x = self.act2(self.act1(self.layer1(x))) 
...         x = self.act4(self.act3(self.layer2(x))) 
...         x = x.reshape(x.shape[0], -1) 
...         x = self.linear_layer(x) 
...         return x
>>> net = Net()
>>> output = net(x.view(x.shape[0], 1, -1))

这给出了形状为[244, 1] 的输出。我们重塑x,因为 PyTorch 卷积层的输入通常需要您指定输入的通道数,在这种情况下,您似乎有 1 个通道。查看the official documentation 了解更多信息。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-20
    • 2018-09-24
    • 2020-09-30
    • 1970-01-01
    • 2019-10-23
    • 2022-01-19
    • 2020-10-13
    • 1970-01-01
    相关资源
    最近更新 更多