日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[pytorch ] (a) must be greater or equal to the number of dimensions (b)

發(fā)布時(shí)間:2025/1/21 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [pytorch ] (a) must be greater or equal to the number of dimensions (b) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1. The number of sizes provided (0) must be greater or equal to the number of dimensions in the tensor(1)
      • 建議
    • 2. the number of sizes provided (1) must be greater or equal to the number of dimensions in the tensor (3)

1. 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. I’m running this on Colab and I’m sure that Pytorch detects a GPU. This is a deep Q network (RL).

I declare my network as: 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):

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([])). 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].
由于target[j, b_pact[j]]的值是單個(gè)元素,也就是標(biāo)量,如果要進(jìn)行賦值的話(huà),=右邊也應(yīng)該是一個(gè)標(biāo)量,但是事實(shí)卻是右邊是一個(gè)矢量;例如 maxq[j]這就是一個(gè)矢量;

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).

maxq[j] is not indexing into the maximum values, but rather the tuple of (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):

根據(jù)需求,要使得 對(duì) maxq[j] 提取到的是一個(gè)標(biāo)量,這個(gè)時(shí)候可以使用一些幾種方法

1. Destructure/unpack and ignore the indices maxq, _ = torch.max(q_.data,axis=1)2. Access first element of the tuple maxq = torch.max(q_.data,axis=1)[0]3. Access `values` of the named tuple maxq = torch.max(q_.data,axis=1).values

建議

for循環(huán)和計(jì)算結(jié)果不適合 GPU,因?yàn)樗鼈兛梢詧?zhí)行大量并行計(jì)算,所以它們大多速度非常快。您需要避免 for 循環(huán),因?yàn)樗鼈儫o(wú)法被 PyTorch 優(yōu)化,并用矢量化計(jì)算替換它們;
替換方法:

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

這兩種方法等價(jià)

target[torch.arange(batch_size), b_pact] = b_reward + gamma * maxq * (~b_done).

2. the number of sizes provided (1) must be greater or equal to the number of dimensions in the tensor (3)

總結(jié)

以上是生活随笔為你收集整理的[pytorch ] (a) must be greater or equal to the number of dimensions (b)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。