有時候在跑代碼是會出現(xiàn)下面這個錯誤
/root/miniconda3/lib/python3.8/site-packages/torch/functional.py:568: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:2228.)
這個UserWarning警告是關于torch.meshgrid函數(shù)的改動提示鹃栽。
在PyTorch的新版本中侄榴,torch.meshgrid函數(shù)需要增加一個indexing參數(shù)來指定索引模式。
目前torch.meshgrid默認的索引模式是'ij'朵耕,新版本中需要顯式指定:
根據(jù)報錯的提示點擊上述報錯提示內容,然后跳轉到相應的functional[.py文件]
kwargs = {} if indexing is None else {'indexing': indexing}
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
改為
# 新版本中需要指定indexing模式
kwargs = {} if indexing is None else {'indexing': indexing}
return _VF.meshgrid(tensors, **kwargs, indexing='ij') # type: ignore[attr-defined]
x = torch.arange(4)
y = torch.arange(3)
xx, yy = torch.meshgrid(x, y) # 默認'ij'模式
# 新版本中需要指定indexing模式
xx, yy = torch.meshgrid(x, y, indexing='ij')
這個indexing參數(shù)有兩種模式:
'ij'模式: 返回的第一個矩陣索引是i嗅辣,第二個矩陣索引是j桐猬,這是默認的傳統(tǒng)矩陣索引法則。
'xy'模式: 返回的第一個矩陣索引是x上遥,第二個矩陣索引是y搏屑。
所以為了兼容新版本PyTorch,使用torch.meshgrid時需要顯式添加indexing='ij'參數(shù)粉楚。
這個UserWarning提醒我們新版本的改動辣恋,需要注意傳入索引模式亮垫,以避免默認模式改變后代碼出錯。