Variable、Tensor、Numpy的转换
生活随笔
收集整理的這篇文章主要介紹了
Variable、Tensor、Numpy的转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Tensor <——> Numpy
Tensor與Numpy之間可以相互轉換:
代碼:
[plain]?view plaincopy
輸出:
[plain]?view plaincopy
Variable -> Numpy
Variable可以直接轉為Numpy:
[plain]?view plaincopy
如果把requires_grad改為False,得到的是Tensor:
[plain]?view plaincopy
輸出:
tensor([4.])
Numpy ->Tensor -> Variable
但是Numpy不可以直接轉為Variable,所以要借助Tensor進行轉換:
[plain]?view plaincopy
輸出:
[plain]?view plaincopy
若試圖直接將Numpy轉為Variable,
data_Variable = Variable(data_numpy),會出現如下錯誤:
來源:https://www.geek-share.com/detail/2774546287.html
總結
以上是生活随笔為你收集整理的Variable、Tensor、Numpy的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pytorch中的variable, t
- 下一篇: 绚田人入境中国探亲要中国邀请函吗