日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 少妇特黄一区二区 | 色呦呦免费 | 一区亚洲 | 亚洲国产精品激情在线观看 | 成人久久网 | 国产免费一区二区 | 国产性猛交xxxx免费看久久 | 毛毛毛片 | 美国做爰xxxⅹ性视频 | 日韩av电影网 | 久久影院国产 | 无套内谢大学处破女www小说 | 久久撸视频 | 日韩欧美精品在线观看 | 亚洲精品在线不卡 | 日日夜夜干 | 婚后打屁股高h1v1调教 | 国产激情一区 | 91麻豆精品国产91久久久久久久久 | 国产精品国语自产拍在线观看 | 免费观看理伦片在线播放视频软件 | 欧美xx在线 | 一区二区国产视频 | 免费黄色在线网站 | 亚洲成人aa | 亚洲熟妇无码一区二区三区导航 | jlzzzjlzzz国产免费观看 | 波多野结衣一区二 | 超碰97最新| 精品九九九 | 色5月婷婷 | 91色啪| 日韩欧美一二三 | 亚洲女人毛茸茸 | 国产精品污网站 | 午夜视频福利 | 欧美精品一区二区三区视频 | 懂色av成人一区二区三区 | 中文字幕大全 | 成人网在线免费观看 | 日本一区二区三区四区在线观看 | 黄色片网站大全 | 久久韩日 | 久久精品欧美一区二区三区不卡 | 成人在线免费视频观看 | 蜜臀视频一区二区三区 | 久久精品国产精品亚洲色婷婷 | 丁香av | 日本少妇裸体 | 外国电影免费观看高清完整版 | 成人伊人网 | 亚洲激情久久 | 欧美在线视频免费播放 | 日本久久高清 | 老色鬼网站 | 一本一道久久综合狠狠老精东影业 | 欧美播放 | 久草精品在线观看 | 欧美成年人在线视频 | 色呦呦官网 | 亚洲一区二区在线观看视频 | 国产精品区在线 | 久久成人18免费观看 | 久久观看| 探花一区 | 三上悠亚久久 | 日韩电影福利 | 亚洲午夜久久久久久久久久久 | 午夜国产福利视频 | 五月婷av| 自拍亚洲综合 | 免费看女生裸体视频 | 成人免费播放视频 | 麻豆性生活| 国产精品96久久久久久 | 99在线精品视频免费观看20 | 伊人激情 | 一区日韩 | 四虎影视永久免费观看 | 伊人影院中文字幕 | 麻豆精品在线播放 | 中国人与拘一级毛片 | 国产aⅴ无码片毛片一级一区2 | 日韩欧美极品 | 女优中文字幕 | 精品国产免费一区二区三区 | 污网站免费观看 | jizz中国少妇 | 宅男午夜在线 | 超碰碰碰碰 | 在线视频综合网 | 国产片一区二区三区 | 成人免费视频网站在线看 | av午夜天堂 | 国产视频一二三四区 | 午夜精品久久久久久久蜜桃 | 在线观看第一页 | 国产精品无码999 | 国产视频在线播放 |