PyTorch:提供的尺寸数(0)必须大于或等于张量中的尺寸数(1)

时间:2020-06-03 00:00:21

标签: python numpy pytorch tensor

我正在尝试使用Pytorch将CPU模型转换为GPU,但是遇到了问题。我正在Colab上运行此程序,并且确定Pytorch可以检测到GPU。这是一个深度Q网络(RL)。

我将我的网络声明为:Q = Q_Network(input_size, hidden_size, output_size).to(device)

当我尝试通过网络传递参数时遇到了一个问题(它的预期类型为cuda但类型为cpu),所以我添加了.to(device):

batch = np.array(shuffled_memory[i:i+batch_size])
b_pobs = np.array(batch[:, 0].tolist(), dtype=np.float32).reshape(batch_size, -1)
b_pact = np.array(batch[:, 1].tolist(), dtype=np.int32)
b_reward = np.array(batch[:, 2].tolist(), dtype=np.int32)
b_obs = np.array(batch[:, 3].tolist(), dtype=np.float32).reshape(batch_size, -1)
b_done = np.array(batch[:, 4].tolist(), dtype=np.bool)

q = Q(torch.from_numpy(b_pobs).to(device))
q_ = Q_ast(torch.from_numpy(b_obs).to(device))

maxq = torch.max(q_.data,axis=1)
target = copy.deepcopy(q.data)

for j in range(batch_size):
    print(target[j, b_pact[j]].shape) # torch.Size([])
    target[j, b_pact[j]] = b_reward[j]+gamma*maxq[j]*(not b_done[j]) #I run into issues here

这是错误:

RuntimeError: expand(torch.cuda.FloatTensor{[50]}, size=[]): the number of sizes provided (0) must be greater or equal to the number of dimensions in the tensor (1)

1 个答案:

答案 0 :(得分:1)

target[j, b_pact[j]]是张量的单个元素(标量,因此大小为torch.Size([]))。如果要为其分配任何内容,则右侧只能是标量。事实并非如此,因为其中一项是具有一维(向量)的张量,即maxq[j]

在将维度dimaxis被视为同义词)指定为torch.max时,它将返回命名为(values, indices)的元组,其中values包含最大值和indices的每个最大值的位置(等效于argmax)。

maxq[j]不是索引最大值,而是(values, indices)的元组。如果只希望这些值,则可以使用以下方法之一从元组中获取这些值(它们都是等效的,您可以使用任意一个):

# Destructure/unpack and ignore the indices
maxq, _ = torch.max(q_.data,axis=1)

# Access first element of the tuple
maxq = torch.max(q_.data,axis=1)[0]

# Access `values` of the named tuple
maxq  = torch.max(q_.data,axis=1).values