在Pytorch中训练1D CNN

时间:2020-05-31 18:35:48

标签: pytorch cnn

我想训练以下模型。我正在PyTorch中开发一维CNN模型。通常我们在PyTorch中使用数据加载器。但是我没有在我的实现中使用数据加载器。我需要有关如何在pytorch中训练模型的指导。

import torch
import torch.nn as nn
import torch.nn.functional as F

class  CharCNN(nn.Module):
def __init__(self,num_labels=11):
    super(CharCNN, self).__init__()

    self.conv1 = nn.Sequential(
        nn.Conv1d(num_channels, depth_1, kernel_size=kernel_size_1, stride=stride_size),
        nn.ReLU(),
        nn.MaxPool1d(kernel_size=kernel_size_1, stride=stride_size),
        nn.Dropout(0.1),
    )

    self.conv2 = nn.Sequential(
        nn.Conv1d(depth_1, depth_2, kernel_size=kernel_size_2, stride=stride_size),
        nn.ReLU(),
        nn.MaxPool1d(kernel_size=kernel_size_2, stride=stride_size),
        nn.Dropout(0.25)
    )            


    self.fc1 = nn.Sequential(
        nn.Linear(depth_2*kernel_size_2, num_hidden),
        nn.ReLU(),
        nn.Dropout(0.5)
    )

    self.fc2 = nn.Sequential(
        nn.Linear(num_hidden, num_labels),
        nn.ReLU(),
        nn.Dropout(0.5)
    )



def forward(self, x):
    out = self.conv1(x)
    out = self.conv2(out)

    # collapse
    out = x.view(x.size(0), -1)
    # linear layer
    out = self.fc1(out)
    # output layer
    out = self.fc2(out)
    #out = self.log_softmax(x,dim=1)

    return out

我正在像这样训练我的网络:

criterion = nn.CrossEntropyLoss()    
opt = torch.optim.Adam(model.parameters(),lr=learning_rate)


for e in range(training_epochs):
  if(train_on_gpu):
     net.cuda()
    train_losses = []    

   for batch in iterate_minibatches(train_x, train_y, batch_size):
      x, y = batch
      inputs, targets = torch.from_numpy(x), torch.from_numpy(y)
      if(train_on_gpu):
        inputs, targets = inputs.cuda(), targets.cuda()
      opt.zero_grad()   
      output = model(inputs, batch_size)

    loss = criterion(output, targets.long())
    train_losses.append(loss.item())
    loss.backward()
    opt.step()
val_losses = []
accuracy=0
f1score=0
print("Epoch: {}/{}...".format(e+1, training_epochs),
              "Train Loss: {:.4f}...".format(np.mean(train_losses)))

但是我遇到了以下错误

          TypeError                                 Traceback (most recent call last)
          <ipython-input-60-3a3df06ef2f8> in <module>
   14             inputs, targets = inputs.cuda(), targets.cuda()
   15         opt.zero_grad()
   ---> 16         output = model(inputs, batch_size)
   17 
   18         loss = criterion(output, targets.long())

   ~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, 
   * input, **kwargs)
   530             result = self._slow_forward(*input, **kwargs)
   531         else:
   --> 532             result = self.forward(*input, **kwargs)
   533         for hook in self._forward_hooks.values():
   534             hook_result = hook(self, input, result)

   TypeError: forward() takes 2 positional arguments but 3 were given

请指导我如何解决此问题。

2 个答案:

答案 0 :(得分:0)

模型的forward方法仅使用一个参数,但是您使用两个参数调用它:

output = model(inputs, batch_size)

应该是:

output = model(inputs)

答案 1 :(得分:0)

时间序列数据使用 5 元素窗口。目标是滚动窗口为 5。卷积 1d 模型接收一个 Sales 张量 3 维结构,其中包含特定持续时间 (https://krzjoa.github.io/2019/12/28/pytorch-ts-v1.html) 内的所有销售额,内核设置为 5 以匹配移动窗口大小。输入和输出为 1。损失函数计算超过 1000 个 epoch。然后将预测张量转换为一个 numpy 数组,并将其与实际移动平均值进行比较显示。我确实找到了 iterate_minibatches 代码,但它不适用于时间序列数据,因为维度不同(32 个目标与 36 个源)

import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

df=pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/shampoo.csv')

#created a three dimensional tensor
#1. number of samples
#2. number of channels
#3. -1 means infer value from dimension

X=data.Sales.copy()
y=data.Sales.rolling(5).mean().copy()

net = nn.Conv1d(1, 1, 5,  bias = False)
optimizer=optim.Adam(net.parameters(), lr=0.01) #l2
running_loss=0.0

X=data.Sales.copy()
y=data.Sales.rolling(5).mean().copy()


X_tensor = torch.Tensor(X).reshape(1, 1, -1)
print("Sales", X_tensor)
y=y[4:,].to_numpy()
y_tensor = torch.Tensor(y).reshape(1, 1, -1)
print("Avg", y_tensor)

ts_tensor = torch.Tensor(X).reshape(1, 1, -1)
kernel = [0.5, 0.5]
kernel_tensor = torch.Tensor(kernel).reshape(1, 1, -1)
print("Kernel", F.conv1d(ts_tensor, kernel_tensor))

for epoch in range(1000):
    optimizer.zero_grad()
    outputs=net(X_tensor)
    #print("Outputs",outputs)
    loss_value = torch.mean((outputs - y_tensor)**2)
    loss_value.backward()
    optimizer.step()
    
    running_loss += loss_value.item()

    if epoch % 100 == 0:
        print('[%d] loss: %.3f' % (epoch, loss_value.item()))
        print(net.weight.data.numpy())
        
        
prediction = (net(X_tensor).data).float()
prediction=(prediction.numpy().flatten())
data.Sales.plot()
plt.plot(prediction)

#actual moving average
data.Sales.plot()
plt.plot(y)