最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

錯誤信息:

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

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

一般是由于 inplace操作或是indexing或是轉置. 這些都是共享內(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通過select出來的, 他們共享內(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通過select出來的, 他們共享內(nèi)存. 所以當對這個共享的內(nèi)存進行修改W_mat[cnt, indS] = 1, 就會出錯. 此時我們可以通過clone()將W_mat和W_mat_all獨立出來. 這樣的話, 梯度也會通過 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)

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

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

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

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 一個新的Variable, 因此并沒有和其他Variable共享內(nèi)存, 所以不會出錯. 但是最后一句話,

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

你可能會說, 不對啊, 修改grad_latter_all[idx]又沒有創(chuàng)建新的Variable, 怎么會出錯. 這是因為grad_latter_all和grad_output是共享內(nèi)存的. 因為 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, :, :]
  # 這兩個后面修改值了, 所以也要加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

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

如下所示:

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

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

如果沒有clone(),對mask_miss的某個通道賦值后,所有通道上的tensor都會變成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)存錯誤問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Python?Cloudinary實現(xiàn)圖像和視頻上傳詳解

    Python?Cloudinary實現(xiàn)圖像和視頻上傳詳解

    這篇文章主要介紹了Python?Cloudinary實現(xiàn)圖像和視頻上傳功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-11-11
  • python腳本和網(wǎng)頁有何區(qū)別

    python腳本和網(wǎng)頁有何區(qū)別

    在本篇文章里小編給大家整理的是關于python腳本和網(wǎng)頁的區(qū)別點總結,有興趣的朋友們可以學習下。
    2020-07-07
  • 樸素貝葉斯Python實例及解析

    樸素貝葉斯Python實例及解析

    這篇文章主要為大家詳細介紹了樸素貝葉斯Python算法實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • python實現(xiàn)列表中由數(shù)值查到索引的方法

    python實現(xiàn)列表中由數(shù)值查到索引的方法

    今天小編就為大家分享一篇python實現(xiàn)列表中由數(shù)值查到索引的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 詳解python的函數(shù)遞歸與調(diào)用

    詳解python的函數(shù)遞歸與調(diào)用

    Python中的函數(shù)遞歸是一種函數(shù)調(diào)用自身的編程技術,遞歸可以用來解決問題,特別是那些可以分解為更小、相似子問題的問題,本文將給大家詳細的講解一下python的函數(shù)遞歸與調(diào)用,需要的朋友可以參考下
    2023-10-10
  • Python基于回溯法子集樹模板實現(xiàn)8皇后問題

    Python基于回溯法子集樹模板實現(xiàn)8皇后問題

    這篇文章主要介紹了Python基于回溯法子集樹模板實現(xiàn)8皇后問題,簡單說明了8皇后問題的原理并結合實例形式分析了Python回溯法子集樹模板解決8皇后問題的具體實現(xiàn)技巧,需要的朋友可以參考下
    2017-09-09
  • Python利用itchat模塊定時給朋友發(fā)送微信信息

    Python利用itchat模塊定時給朋友發(fā)送微信信息

    這篇文章主要介紹了在Python中利用itchat模塊編寫一個爬蟲腳本,可以實現(xiàn)每天定時給朋友發(fā)微信暖心話,感興趣的可以跟隨小編一起學習一下
    2022-01-01
  • 3行Python代碼實現(xiàn)圖像照片摳圖和換底色的方法

    3行Python代碼實現(xiàn)圖像照片摳圖和換底色的方法

    這篇文章主要介紹了3行Python代碼實現(xiàn)圖像照片摳圖和換底色的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • Windows平臺Python編程必會模塊之pywin32介紹

    Windows平臺Python編程必會模塊之pywin32介紹

    在Windows平臺上,從原來使用C/C++編寫原生EXE程序,到使用Python編寫一些常用腳本程序,成熟的模塊的使用使得編程效率大大提高了
    2019-10-10
  • 解決Windows下python和pip命令無法使用的問題

    解決Windows下python和pip命令無法使用的問題

    這篇文章主要介紹了解決Windows下python和pip命令無法使用的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08

最新評論

乐亭县| 西安市| 永川市| 车致| 麻城市| 赤壁市| 临湘市| 老河口市| 台江县| 陆丰市| 弋阳县| 准格尔旗| 吉木萨尔县| 奈曼旗| 平凉市| 汉源县| 太和县| 微博| 雷波县| 乐陵市| 巴楚县| 厦门市| 察哈| 渭南市| 华容县| 天津市| 芦山县| 阳泉市| 江西省| 灵武市| 扎赉特旗| 饶平县| 张掖市| 于都县| 合水县| 霸州市| 南川市| 大兴区| 辉县市| 北海市| 玉林市|