[英]PyTorch: The number of sizes provided (0) must be greater or equal to the number of dimensions in the tensor (1)
I'm trying to convert a CPU model to GPU using Pytorch, but I'm running into issues.我正在尝试使用 Pytorch 将 CPU model 转换为 GPU,但我遇到了问题。 I'm running this on Colab and I'm sure that Pytorch detects a GPU.我在 Colab 上运行它,我确信 Pytorch 检测到 GPU。 This is a deep Q network (RL).这是一个深度 Q 网络 (RL)。
I declare my network as: Q = Q_Network(input_size, hidden_size, output_size).to(device)
我将我的网络声明为: Q = Q_Network(input_size, hidden_size, output_size).to(device)
I ran into an issue when I tried to pass arguments through the network (It expected type cuda but got type cpu) so I add.to(device):当我尝试通过网络传递 arguments 时遇到了一个问题(它需要 cuda 类型但得到了 cpu 类型)所以我 add.to(设备):
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
Here is the error:这是错误:
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)
target[j, b_pact[j]]
is a single element of the tensor (a scalar, hence size of torch.Size([])
). target[j, b_pact[j]]
是张量的单个元素(一个标量,因此是torch.Size([])
的大小)。 If you want to assign anything to it, the right hand side can only be a scalar.如果你想给它分配任何东西,右手边只能是一个标量。 That is not the case, as one of the terms is a tensor with 1 dimension (a vector), namely your maxq[j]
.情况并非如此,因为其中一个术语是具有一维(向量)的张量,即您的maxq[j]
。
When specifying a dimension dim
( axis
is treated as a synonym) to torch.max
, it will return a named tuple of (values, indices)
, where values
contains the maximum values and indices
the location of each of the maximum values (equivalent to argmax).当为torch.max
指定维度dim
( axis
被视为同义词)时,它将返回(values, indices)
的命名元组,其中values
包含最大值, indices
每个最大值的位置(相当于最大参数)。
maxq[j]
is not indexing into the maximum values, but rather the tuple of (values, indices)
. maxq[j]
不是索引最大值,而是(values, indices)
的元组。 If you only want the values you can use one of the following to get the values out of the tuple (all of them are equivalent, you can use whichever you prefer):如果您只想要这些值,您可以使用以下方法之一从元组中获取值(它们都是等价的,您可以使用任何您喜欢的值):
# 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.