使用PyTorch时遇到此错误:RuntimeError: gather_out_cpu(): 索引的预期数据类型为int64

我在尝试用PyTorch创建一个AI时遇到了这个错误:

RuntimeError: gather_out_cpu(): Expected dtype int64 for index

这是我的函数:

def learn(self, batch_state, batch_next_state, batch_reward, batch_action):    outputs = self.model(batch_state).gather(1, batch_action.unsqueeze(1)).squeeze(1)    next_outputs = self.model(batch_next_state).detach().max(1)[0]    target = self.gamma * next_outputs + batch_reward    td_loss = F.smooth_l1_loss(outputs, target)    self.optimizer.zero_grad()    td_loss.backward(retain_variables = True)    self.optimizer.step()

回答:

您需要在将batch_action张量传递给torch.gather之前更改其数据类型。

def learn(...):    batch_action = batch_action.type(torch.int64)     outputs = ...    ...# oroutputs = self.model(batch_state).gather(1, batch_action.type(torch.int64).unsqueeze(1)).squeeze(1)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注