Creating and shaping data for 1D CNN

…衆ロ難τιáo~ 提交于 2021-01-27 21:43:04

问题


I have a (244, 108) numpy array. It contains percentage change of close value of a trade for each minute in one day ie 108 values and like that for 244 days. Basically its a 1D vector. So in order to do 1D CNN how should I shape my data?

What i have done:

x.shape = (244, 108)
x = np.expand_dims(x, axis=2)
x.shape = (243, 108, 1)
y.shape = (243,)

Model:

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

        self.layer1 = torch.nn.Conv1d(in_channels=108, out_channels=1, kernel_size=1, stride=1)
        self.act1 = torch.nn.ReLU()
        self.act2 = torch.nn.MaxPool1d(kernel_size=1, stride=1)
        self.layer2 = torch.nn.Conv1d(in_channels=1, out_channels=1, kernel_size=1, stride=1)
        self.act3 = torch.nn.ReLU()
        self.act4 = torch.nn.MaxPool1d(kernel_size=1, stride=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 = self.linear_layers(x)
        return x



回答1:


If each day should be separate instance for convolution your data should have the shape (248, 1, 108). This seems more reasonable.

If you want all your days and minutes to be a continuum for network to learn it should be of shape (1, 1, 248*108).

Basically first dimension is batch (how many training samples), second is the number of channels or features of sample (only one in your case) and last is the number of timesteps.

Edit

Your pooling layer should be torch.nn.AdaptiveMaxPool1d(1). You should also reshape output from this layer like this: pooled.reshape(x.shape[0], -1) before pushing it through torch.nn.Linear layer.



来源:https://stackoverflow.com/questions/61782774/creating-and-shaping-data-for-1d-cnn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!