如何修复输入和参数张量不在同一设备上?

2024-03-01

我看到其他人也遇到此错误,我尝试按照步骤解决,但仍然收到此错误。 “运行时错误:输入和参数张量不在同一设备上,在 cpu 处找到输入张量,在 cuda:0 处找到参数张量”

我运行 model.to(device) 和 input_seq.to(device)。错误表示它在 CPU 上找到了输入张量,但所有输入数据都应该通过 input_seq.to(device) 位于 GPU 上。下面是填写代码

text = ['hey how are you','good i am fine','have a nice day']

# Join all the sentences together and extract the unique characters from the combined sentences
chars = set(''.join(text))

# Creating a dictionary that maps integers to the characters
int2char = dict(enumerate(chars))

# Creating another dictionary that maps characters to integers
char2int = {char: ind for ind, char in int2char.items()}

# Finding the length of the longest string in our data
maxlen = len(max(text, key=len))

# Padding

# A simple loop that loops through the list of sentences and adds a ' ' whitespace until the length of
# the sentence matches the length of the longest sentence
for i in range(len(text)):
  while len(text[i])<maxlen:
      text[i] += ' '

# Creating lists that will hold our input and target sequences
input_seq = []
target_seq = []

for i in range(len(text)):
    # Remove last character for input sequence
  input_seq.append(text[i][:-1])
    
    # Remove first character for target sequence
  target_seq.append(text[i][1:])
  print("Input Sequence: {}\nTarget Sequence: {}".format(input_seq[i], target_seq[i]))
  
for i in range(len(text)):
    input_seq[i] = [char2int[character] for character in input_seq[i]]
    target_seq[i] = [char2int[character] for character in target_seq[i]]
    
dict_size = len(char2int)
seq_len = maxlen - 1
batch_size = len(text)

def one_hot_encode(sequence, dict_size, seq_len, batch_size):
    # Creating a multi-dimensional array of zeros with the desired output shape
    features = np.zeros((batch_size, seq_len, dict_size), dtype=np.float32)
    
    # Replacing the 0 at the relevant character index with a 1 to represent that character
    for i in range(batch_size):
        for u in range(seq_len):
            features[i, u, sequence[i][u]] = 1
    return features

# Input shape --> (Batch Size, Sequence Length, One-Hot Encoding Size)
input_seq = one_hot_encode(input_seq, dict_size, seq_len, batch_size)


input_seq = torch.from_numpy(input_seq)
target_seq = torch.Tensor(target_seq)

# torch.cuda.is_available() checks and returns a Boolean True if a GPU is available, else it'll return False
is_cuda = torch.cuda.is_available()

# If we have a GPU available, we'll set our device to GPU. We'll use this device variable later in our code.
if is_cuda:
    device = torch.device("cuda")
    print("GPU is available")
else:
    device = torch.device("cpu")
    print("GPU not available, CPU used")

class Model(nn.Module):
    def __init__(self, input_size, output_size, hidden_dim, n_layers):
        super(Model, self).__init__()

        # Defining some parameters
        self.hidden_dim = hidden_dim
        self.n_layers = n_layers

        #Defining the layers
        # RNN Layer
        self.rnn = nn.RNN(input_size, hidden_dim, n_layers, batch_first=True)   
        # Fully connected layer
        self.fc = nn.Linear(hidden_dim, output_size)
    
    def forward(self, x):
        
        batch_size = x.size(0)

        # Initializing hidden state for first input using method defined below
        hidden = self.init_hidden(batch_size)

        # Passing in the input and hidden state into the model and obtaining outputs
        out, hidden = self.rnn(x, hidden)
        
        # Reshaping the outputs such that it can be fit into the fully connected layer
        out = out.contiguous().view(-1, self.hidden_dim)
        out = self.fc(out)
        
        return out, hidden
    
    def init_hidden(self, batch_size):
        # This method generates the first hidden state of zeros which we'll use in the forward pass
        # We'll send the tensor holding the hidden state to the device we specified earlier as well
        hidden = torch.zeros(self.n_layers, batch_size, self.hidden_dim)
        return hidden



# Instantiate the model with hyperparameters
model = Model(input_size=dict_size, output_size=dict_size, hidden_dim=12, n_layers=1)
# We'll also set the model to the device that we defined earlier (default is CPU)
model.to(device)

# Define hyperparameters
n_epochs = 100
lr=0.01

# Define Loss, Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=lr)


# Training Run
for epoch in range(1, n_epochs + 1):
    optimizer.zero_grad() # Clears existing gradients from previous epoch
    input_seq.to(device)
    target_seq.to(device)
    output, hidden = model(input_seq)
    loss = criterion(output, target_seq.view(-1).long())
    loss.backward() # Does backpropagation and calculates gradients
    optimizer.step() # Updates the weights accordingly
    
    if epoch%10 == 0:
        print('Epoch: {}/{}.............'.format(epoch, n_epochs), end=' ')
        print("Loss: {:.4f}".format(loss.item()))


不像to https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.to方法可用于nn.Module比如你的model. The to https://pytorch.org/docs/stable/generated/torch.Tensor.to.html#torch.Tensor.to方法上Tensors 不是就地操作!如文档页面所述:

这个方法[nn.Module.to] 就地修改模块。

vs for Tensor.to:

[...]返回的张量是copy自我与所需的[...]torch.device.

换句话说,您需要重新分配张量才能有效地将它们发送到设备。

input_seq = input_seq.to(device)
target_seq = target_seq.to(device)

虽然nn.Module不需要这种治疗:

model.to(device)

为了清楚地理解这里发生的情况,请看这个例子:

>>> x = torch.zeros(1)  # on cpu
>>> y = x.cuda()        # y is a copy of x

>>> y.device            # placed on cuda device
'cuda:0'

>>> x.device            # but x remains on the original device
'cpu'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复输入和参数张量不在同一设备上? 的相关文章

随机推荐

  • jQuery 在 DOM 中获取特定元素之后的下一个匹配项

    我不愿意承认这一点 但我一直在努力弄清楚如何做到这一点 例如假设您具有以下结构 div div ul li a href img class foo a li li a href img class bar a li li a href i
  • 快速教程----函数中的“条件”是什么意思?

    I know that in the first line we can use lessThanTen number Int to replace int and contidion means a label but in the th
  • 此绑定的转换器参数应该是什么

    我正在尝试实现一个 wpf 用户控件 该控件使用转换器将文本框绑定到双精度列表 如何将用户控件的实例设置为转换器参数 控件的代码如下所示 Thanks
  • Pyautogui TypeError:“NoneType”对象不可迭代

    我正在尝试使用locateCenterOnScreen PyAutoGUI 的功能 但是它会引发 Traceback most recent call last File C Users windows Desktop asd py lin
  • JavaScript - 在 display:none 和 display:block 之间添加过渡

    我正在使用 JavaScript 来切换通知 如下所示 如何添加之间的过渡display block and display none 我不想添加像 jQuery 这样的外部库 因为我只会使用toggle单独作用 var btn docum
  • PHP 扩展 (memcache|memcached) 未显示在 phpinfo() 中,但显示在 php -m 和 php -i 中?

    当我使用时 我将两个模块列为已安装 配置 php m 或者如果我使用 php i 但是当我使用时 m new Memcache or m new Memcache or m new Memcached or m new Memcached
  • 如何随着标签宽度的增加对齐所有字段

    我有一个表单 其中标签位于左侧 字段位于右侧 当标签包含少量文本时 此布局非常有效 我可以轻松设置min width标签上 以确保它们与田地的距离相同 在下面的第一张图片中 这按预期工作 当标签的文本变得太长时就会出现问题 它要么溢出到下一
  • XQuery 获取属性列表

    如果我有几个SectionXML 文档中的元素 我使用什么 XQuery 来获取所有元素的列表name values section 在 XPath 2 0 XQuery 的子集 中 可以使用以下表达式来获取 Section 元素的 nam
  • Django 1.6 和 Celery 3.0 内存泄漏

    将 Django 升级到 1 6 后 我的 celery Worker 正在耗尽 RAM 似乎分配给工作人员的内存没有被释放 并且在每次任务后都会增加 相关设置 DB DATABASES default ENGINE django db b
  • python + googledrive:上传xlsx,转换为googlesheet,获取可共享链接

    我想要的程序的流程是 上传 xlsx 电子表格到驱动器 它是使用 pandas 创建的to excel 将其转换为 Google 表格格式 指定任何知道该链接的人都可以对其进行编辑 获取链接并与将输入信息的人共享 下载完成的表格 我目前正在
  • JavaScript 保留关键字

    我想知道 JavaScript 的保留关键字 函数是如何管理的 Example 根据 http www quackit com javascript javascript reserved words cfm http www quacki
  • JSF2/PrimeFaces 中的命名容器 [重复]

    这个问题在这里已经有答案了 PrimeFaces 中可能的命名容器有哪些 当我们想要使用以下命令更新表单上的某些 UI 控件时 为什么需要为 Ajax 更新调用附加命名容器 idupdate mainForm MainAccordian u
  • Visual Studio Code 中的语言可以扩展吗?

    Scenario 我有 JSON 文件 描述了一系列要执行的任务 其中每个任务都可以引用 JSON 文件中的其他任务和对象 tasks id first action doSomething result id second action
  • 如何使用python解压文件

    我怎样才能提取一个 zip or rar使用 Python 文件 迟到了 但我对任何答案都不满意 pip install patool import patoolib patoolib extract archive foo bar rar
  • 我可以使用 URL 打开 Windows 8 应用程序吗?

    我正在创建一个具有共享会话功能的应用程序 例如 私人应用程序到应用程序的聊天会话 我会启动应用程序并创建一个 聊天室 然后通过电子邮件与某人 共享 我想要做的是创建一个 URL 当单击它时 它会打开您计算机上的应用程序 如果我邀请您到我的
  • 在 C# Windows 窗体应用程序中捕获 Ctrl + Shift + P 击键 [重复]

    这个问题在这里已经有答案了 可能的重复 在 Windows 窗体应用程序中捕获组合键事件 https stackoverflow com questions 3062587 I need to perform a particular op
  • Java使用索引来一一显示数组

    我在按索引显示数组时遇到问题 我不知道为什么会发生这种情况 任何帮助将不胜感激 这是我的代码片段 create token2 String token2 create Scanner inFile2 Scanner inFile2 new
  • Swift 3 - 调整字体大小以适合宽度、多行

    我有一个 UILabel 它设置为 42 0 pt 字体 并且标签的宽度是使用基于标签本身以外的因素的自动约束设置的 也就是标签右侧和左侧的内容决定标签的宽度 我想自动调整字体大小以适应标签的宽度 但也可以的话分成两行 与此类似 我知道您可
  • 如何将表达式插入到R中的函数体中

    我有一个函数f lt function x x 我想插入该行x lt 2 x into f这样它最终会变成 function x x lt 2 x x 我明白我应该使用body 但到目前为止我只知道如何替换entire身体 这对于我的真正目
  • 如何修复输入和参数张量不在同一设备上?

    我看到其他人也遇到此错误 我尝试按照步骤解决 但仍然收到此错误 运行时错误 输入和参数张量不在同一设备上 在 cpu 处找到输入张量 在 cuda 0 处找到参数张量 我运行 model to device 和 input seq to d