亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯(cuò)誤問(wèn)題

 更新時(shí)間:2020年06月28日 10:46:39   作者:Hungryof  
這篇文章主要介紹了解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

錯(cuò)誤信息:

RuntimeError: in-place operations can be only used on variables that don't share storage with any other variables, but detected that there are 4 objects sharing it

自動(dòng)求導(dǎo)是很方便, 但是想想, 如果兩個(gè)Variable共享內(nèi)存, 再對(duì)這個(gè)共享的內(nèi)存的數(shù)據(jù)進(jìn)行修改, 就會(huì)引起錯(cuò)誤!

一般是由于 inplace操作或是indexing或是轉(zhuǎn)置. 這些都是共享內(nèi)存的.

 @staticmethod
 def backward(ctx, grad_output):
  ind_lst = ctx.ind_lst
  flag = ctx.flag

  c = grad_output.size(1)
  grad_former_all = grad_output[:, 0:c//3, :, :]
  grad_latter_all = grad_output[:, c//3: c*2//3, :, :]
  grad_swapped_all = grad_output[:, c*2//3:c, :, :]

  spatial_size = ctx.h * ctx.w

  W_mat_all = Variable(ctx.Tensor(ctx.bz, spatial_size, spatial_size).zero_())
  for idx in range(ctx.bz):
   W_mat = W_mat_all.select(0,idx)
   for cnt in range(spatial_size):
    indS = ind_lst[idx][cnt] 

    if flag[cnt] == 1:
     # 這里W_mat是W_mat_all通過(guò)select出來(lái)的, 他們共享內(nèi)存.
     W_mat[cnt, indS] = 1

   W_mat_t = W_mat.t()

   grad_swapped_weighted = torch.mm(W_mat_t, grad_swapped_all[idx].view(c//3, -1).t())
   grad_swapped_weighted = grad_swapped_weighted.t().contiguous().view(1, c//3, ctx.h, ctx.w)
   grad_latter_all[idx] = torch.add(grad_latter_all[idx], grad_swapped_weighted.mul(ctx.triple_w))

由于 這里W_mat是W_mat_all通過(guò)select出來(lái)的, 他們共享內(nèi)存. 所以當(dāng)對(duì)這個(gè)共享的內(nèi)存進(jìn)行修改W_mat[cnt, indS] = 1, 就會(huì)出錯(cuò). 此時(shí)我們可以通過(guò)clone()將W_mat和W_mat_all獨(dú)立出來(lái). 這樣的話, 梯度也會(huì)通過(guò) clone()操作將W_mat的梯度正確反傳到W_mat_all中.

 @staticmethod
 def backward(ctx, grad_output):
  ind_lst = ctx.ind_lst
  flag = ctx.flag

  c = grad_output.size(1)
  grad_former_all = grad_output[:, 0:c//3, :, :]
  grad_latter_all = grad_output[:, c//3: c*2//3, :, :]
  grad_swapped_all = grad_output[:, c*2//3:c, :, :]

  spatial_size = ctx.h * ctx.w

  W_mat_all = Variable(ctx.Tensor(ctx.bz, spatial_size, spatial_size).zero_())
  for idx in range(ctx.bz):
   # 這里使用clone了
   W_mat = W_mat_all.select(0,idx).clone()
   for cnt in range(spatial_size):
    indS = ind_lst[idx][cnt]

    if flag[cnt] == 1:
     W_mat[cnt, indS] = 1

   W_mat_t = W_mat.t()

   grad_swapped_weighted = torch.mm(W_mat_t, grad_swapped_all[idx].view(c//3, -1).t())
   grad_swapped_weighted = grad_swapped_weighted.t().contiguous().view(1, c//3, ctx.h, ctx.w)

   # 這句話刪了不會(huì)出錯(cuò), 加上就吹出錯(cuò)
   grad_latter_all[idx] = torch.add(grad_latter_all[idx], grad_swapped_weighted.mul(ctx.triple_w))

但是現(xiàn)在卻出現(xiàn) 4個(gè)objects共享內(nèi)存. 如果將最后一句話刪掉, 那么則不會(huì)出錯(cuò).

如果沒有最后一句話, 我們看到

grad_swapped_weighted = torch.mm(W_mat_t, grad_swapped_all[idx].view(c//3, -1).t())

grad_swapped_weighted = grad_swapped_weighted.t().contiguous().view(1, c//3, ctx.h, ctx.w)

grad_swapped_weighted 一個(gè)新的Variable, 因此并沒有和其他Variable共享內(nèi)存, 所以不會(huì)出錯(cuò). 但是最后一句話,

grad_latter_all[idx] = torch.add(grad_latter_all[idx], grad_swapped_weighted.mul(ctx.triple_w))

你可能會(huì)說(shuō), 不對(duì)啊, 修改grad_latter_all[idx]又沒有創(chuàng)建新的Variable, 怎么會(huì)出錯(cuò). 這是因?yàn)間rad_latter_all和grad_output是共享內(nèi)存的. 因?yàn)?grad_latter_all = grad_output[:, c//3: c*2//3, :, :], 所以這里的解決方案是:

 @staticmethod
 def backward(ctx, grad_output):
  ind_lst = ctx.ind_lst
  flag = ctx.flag

  c = grad_output.size(1)
  grad_former_all = grad_output[:, 0:c//3, :, :]
  # 這兩個(gè)后面修改值了, 所以也要加clone, 防止它們與grad_output共享內(nèi)存
  grad_latter_all = grad_output[:, c//3: c*2//3, :, :].clone()
  grad_swapped_all = grad_output[:, c*2//3:c, :, :].clone()

  spatial_size = ctx.h * ctx.w

  W_mat_all = Variable(ctx.Tensor(ctx.bz, spatial_size, spatial_size).zero_())
  for idx in range(ctx.bz):
   W_mat = W_mat_all.select(0,idx).clone()
   for cnt in range(spatial_size):
    indS = ind_lst[idx][cnt]

    if flag[cnt] == 1:
     W_mat[cnt, indS] = 1

   W_mat_t = W_mat.t()

   grad_swapped_weighted = torch.mm(W_mat_t, grad_swapped_all[idx].view(c//3, -1).t())

   grad_swapped_weighted = grad_swapped_weighted.t().contiguous().view(1, c//3, ctx.h, ctx.w)
   grad_latter_all[idx] = torch.add(grad_latter_all[idx], grad_swapped_weighted.mul(ctx.triple_w))

  grad_input = torch.cat([grad_former_all, grad_latter_all], 1)

  return grad_input, None, None, None, None, None, None, None, None, None, None

補(bǔ)充知識(shí):Pytorch 中 expand, expand_as是共享內(nèi)存的,只是原始數(shù)據(jù)的一個(gè)視圖 view

如下所示:

mask = mask_miss.expand_as(sxing).clone() # type: torch.Tensor
mask[:, :, -2, :, :] = 1 # except for person mask channel

為了避免對(duì)expand后對(duì)某個(gè)channel操作會(huì)影響原始tensor的全部元素,需要使用clone()

如果沒有clone(),對(duì)mask_miss的某個(gè)通道賦值后,所有通道上的tensor都會(huì)變成1!

# Notice! expand does not allocate more memory but just make the tensor look as if you expanded it.
# You should call .clone() on the resulting tensor if you plan on modifying it
# https://discuss.pytorch.org/t/very-strange-behavior-change-one-element-of-a-tensor-will-influence-all-elements/41190

以上這篇解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯(cuò)誤問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • tkinter自定義下拉多選框問(wèn)題

    tkinter自定義下拉多選框問(wèn)題

    這篇文章主要介紹了tkinter自定義下拉多選框問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • python打印日志方法的使用教程(logging模塊)

    python打印日志方法的使用教程(logging模塊)

    Python標(biāo)準(zhǔn)庫(kù)自帶日志模塊logging,logging中涉及到4個(gè)核心組件,這些組件構(gòu)建了logging體系,下面這篇文章主要給大家介紹了關(guān)于python打印日志方法的使用教程,需要的朋友可以參考下
    2022-06-06
  • Python解析json代碼實(shí)例解析

    Python解析json代碼實(shí)例解析

    這篇文章主要介紹了Python解析json代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • FFrpc python客戶端lib使用解析

    FFrpc python客戶端lib使用解析

    這篇文章主要介紹了FFrpc python客戶端lib使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python PyAutoGUI實(shí)現(xiàn)自動(dòng)化任務(wù)應(yīng)用場(chǎng)景示例

    Python PyAutoGUI實(shí)現(xiàn)自動(dòng)化任務(wù)應(yīng)用場(chǎng)景示例

    這篇文章主要為大家介紹了Python PyAutoGUI實(shí)現(xiàn)自動(dòng)化任務(wù)應(yīng)用場(chǎng)景示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 詳談Python3 操作系統(tǒng)與路徑 模塊(os / os.path / pathlib)

    詳談Python3 操作系統(tǒng)與路徑 模塊(os / os.path / pathlib)

    下面小編就為大家分享一篇詳談Python3 操作系統(tǒng)與路徑 模塊(os / os.path / pathlib),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python中函數(shù)的參數(shù)傳遞與可變長(zhǎng)參數(shù)介紹

    Python中函數(shù)的參數(shù)傳遞與可變長(zhǎng)參數(shù)介紹

    這篇文章主要介紹了Python中函數(shù)的參數(shù)傳遞與可變長(zhǎng)參數(shù)介紹,本文分別給出多個(gè)代碼實(shí)例來(lái)講解多種多樣的函數(shù)參數(shù),需要的朋友可以參考下
    2015-06-06
  • Pycharm中配置Jupyter環(huán)境的圖文教程

    Pycharm中配置Jupyter環(huán)境的圖文教程

    本文主要介紹了Pycharm中配置Jupyter環(huán)境的圖文教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • python PIL中ImageFilter模塊圖片濾波處理和使用方法

    python PIL中ImageFilter模塊圖片濾波處理和使用方法

    這篇文章主要介紹PIL中ImageFilter模塊幾種圖片濾波處理和使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • 如何將tensorflow訓(xùn)練好的模型移植到Android (MNIST手寫數(shù)字識(shí)別)

    如何將tensorflow訓(xùn)練好的模型移植到Android (MNIST手寫數(shù)字識(shí)別)

    這篇文章主要介紹了將tensorflow訓(xùn)練好的模型移植到Android (MNIST手寫數(shù)字識(shí)別),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04

最新評(píng)論