前言
前篇numpy 的 ndarray类型的操作大多数都可套用到tensor上。
一、基础知识
1.Tensor介绍
Tensor是PyTorch中的基本对象,翻译成张量,表示多维的矩阵。Tensor的大多数运算与Numpy相通,大多数情况下可以考虑直接迁移。
import torch
x = torch.Tensor(5,3)
print(x.size())
##output:
##torch.Size([5,3])
Tensor与 Numpy中的array可以互相转换,转换函数如下:x.numpy() 和 torch.from_numpy()
import torch
import numpy as np
x = torch.rand(5,3)
y = x.numpy()
z = torch.from_numpy(y)
print(x.type()) #tensor查看ndarray对应属性均加上括号
print(y.dtype) #ndarray无type属性
print(z.type())
# output:
# torch.FloatTensor
# float32
# torch.FloatTensor
**书中有介绍Variable,但因Pytorch的新版本已去除了Varia