RuntimeError: Input and parameter tensors are not at the same device, found input tensor at cpu and

RuntimeError: Input and parameter tensors are not at the same device, found input tensor at cpu and parameter tensor at cuda:0

在学习pytorch的时候遇到的错误,意思是,输入和参数的张量不是在同一个device里,有一部分在CPU有一部分在GPU.

下面是一个例子,参考于 https://github.com/zergtant/pytorch-handbook/blob/master/chapter3/3.3-rnn.ipynb

只需要将下面代码 #x = x.cuda() #y = y.cuda()取消注释就可以运行。
因为下面的x,y这两个tensor放在CPU里,所以只需要把他们放入GPU中

import torch
import torch.nn as nn
from torch.nn import functional as F
from torch import optim
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.animation
import math, random
TIME_STEP = 10  # rnn 时序步长数
INPUT_SIZE = 1  # rnn 的输入维度
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
H_SIZE = 64  # of rnn 隐藏单元个数
EPOCHS = 300  # 总共训练次数
h_state = None  # 隐藏层状态

steps = np.linspace(0, np.pi * 2, 256, dtype=np.float32)
x_np = np.sin(steps)
y_np = np.cos(steps)


class RNN(nn.Module):
    def __init__(self):
        super(RNN, self).__init__()
        self.rnn = nn.RNN(
            input_size=INPUT_SIZE,
            hidden_size=H_SIZE,
            num_layers=1,
            batch_first=True,
        )
        self.out = nn.Linear(H_SIZE, 1)

    def forward(self, x, h_state):
        # x (batch, time_step, input_size)
        # h_state (n_layers, batch, hidden_size)
        # r_out (batch, time_step, hidden_size)
        r_out, h_state = self.rnn(x, h_state)
        outs = []  # 保存所有的预测值
        for time_step in range(r_out.size(1)):  # 计算每一步长的预测值
            outs.append(self.out(r_out[:, time_step, :]))
        return torch.stack(outs, dim=1), h_state
        # 也可使用以下这样的返回值
        # r_out = r_out.view(-1, 32)
        # outs = self.out(r_out)
        # return outs, h_state


rnn = RNN().to(DEVICE)
optimizer = torch.optim.Adam(rnn.parameters())  # Adam优化,几乎不用调参
criterion = nn.MSELoss()  # 因为最终的结果是一个数值,所以损失函数用均方误差
rnn.train()

plt.figure(2)
for step in range(EPOCHS):
    start, end = step * np.pi, (step + 1) * np.pi  # 一个时间周期
    steps = np.linspace(start, end, TIME_STEP, dtype=np.float32)
    x_np = np.sin(steps)
    y_np = np.cos(steps)
    x = torch.from_numpy(x_np[np.newaxis, :, np.newaxis]) # shape (batch, time_step, input_size)
    y = torch.from_numpy(y_np[np.newaxis, :, np.newaxis])
    
    #加上这两行就不会报错了
    #x = x.cuda()
    #y = y.cuda()
    
    prediction, h_state = rnn(x, h_state)  # rnn output
    # 这一步非常重要
    h_state = h_state.data  # 重置隐藏层的状态, 切断和前一次迭代的链接
    loss = criterion(prediction, y)
    # 这三行写在一起就可以
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if (step + 1) % 20 == 0:  # 每训练20个批次可视化一下效果,并打印一下loss
        print("EPOCHS: {},Loss:{:4f}".format(step, loss))
        #plt.plot(steps, y_np.flatten(), 'r-')
        #plt.plot(steps, prediction.data.numpy().flatten(), 'b-')
        #plt.draw()
        #plt.pause(0.01)

上面的代码我注释掉画图的部分。
如果不注释,会出现这个错误。
TypeError: can’t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
这是因为GPU的tensor不能转化为numpy。
如果需要画图的话,可以把所以数据都放在cpu中

DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")

改为

DEVICE = torch.device("CUP")

或者使用tensorboard等可视化工具

你可能感兴趣的:(pytorch)