简体   繁体   English

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

[英]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指定维度dimaxis被视为同义词)时,它将返回(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.

相关问题 RuntimeError: expand(torch.cuda.FloatTensor{[3, 3, 3, 3]}, size=[]): 提供的尺寸数 (0) 必须 >= 张量中的维数 (4) - RuntimeError: expand(torch.cuda.FloatTensor{[3, 3, 3, 3]}, size=[]): the number of sizes provided (0) must be >= number of dimensions in the tensor(4) 张量维度必须相等 - Tensor Dimensions must be equal PyTorch Tensor必须是实数,不能是NoneType - PyTorch Tensor must be real number, not NoneType 索引张量必须与自张量具有相同的维数 - Index tensor must have the same number of dimensions as self tensor 关于Pytorch中的张量尺寸和批量大小感到困惑 - Confused about tensor dimensions and batch sizes in pytorch 从 pytorch 中的张量获取具有任意数量尾随维度的矩阵 - obtain matrix from a tensor in pytorch with arbitrary number of trailing dimensions pytorch DataLoader:`张量必须具有相同的维数` - pytorch DataLoader: `Tensors must have same number of dimensions` Index tensor must have the number of dimensions as input tensor error 在使用 torch.gather() 时遇到 - Index tensor must have the same number of dimensions as input tensor error encountered when using torch.gather() 匹配 PyTorch 张量维度 - matching PyTorch tensor dimensions RuntimeError:张量的大小必须匹配,但维度 2 除外。预期大小为 32,但列表中 3 号张量的大小为 1 - RuntimeError: Sizes of tensors must match except in dimension 2. Expected size 32 but got size 1 for tensor number 3 in the list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM