所以我正在学习一门关于Udemy (人工智能a-z)的课程,我遇到了一个错误,它显示如下内容: return map(lambda x: Variable(torch.cat(x,0)),samples) RuntimeError:标量类型为Float的预期对象,但在sequence参数中的序列元素1的标量类型为Long。
元素似乎每次都会改变,所以我不知道问题出在哪里,有人能帮我解决这个问题吗?
我已经尝试修改前面的行(示例),看看这是否是我需要转换为Long的内容,但它不起作用,除非做错了
包含ai.py的43-45行
def sample(self, batch_size):
samples = zip(*random.sample(self.memory, batch_size))
return map(lambda x: Variable(torch.cat(x, 0)), samples)包含ai.py的第74-87行(调用sample方法的是什么)
def update(self, reward, new_signal):
new_state = torch.Tensor(new_signal).float().unsqueeze(0)
self.memory.push((self.last_state, new_state,
torch.LongTensor([int(self.last_action)]),
torch.tensor([self.last_reward])))
action = self.select_action(new_state)
if len(self.memory.memory) > 100:
batch_state, batch_next_state, batch_reward, batch_action =
self.memory.sample(100)
self.learn(batch_state, batch_next_state, batch_reward, batch_action)
self.last_action = action
self.last_state = new_state
self.last_reward = reward
self.reward_window.append(reward)
if len(self.reward_window) > 1000:
del self.reward_window[0]
return action老实说,我希望程序的其余部分都能运行,但它似乎总是在这里崩溃,抱歉我不能更具体地说明我的期望,因为我不知道这是我第一次做一个合适的人工智能
发布于 2020-01-07 08:32:36
根据https://pytorch.org/docs/stable/torch.html#torch.cat的说法,对于return map(lambda x: Variable(torch.cat(x, 0)), samples)中的x中的所有张量,您需要具有相同的类型。但是你混合了张量的类型
self.memory.push((self.last_state, new_state,
torch.LongTensor([int(self.last_action)]),
torch.tensor([self.last_reward])))这就是它抛出异常的原因。
https://stackoverflow.com/questions/54024150
复制相似问题