《深度学习框架PyTorch入门与实践》——Tensor基本操作(1)
一.PyTorch入门第一步
1.构建53矩阵*
import torch as t
x = t.Tensor(5,3)
print(x)
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
2.使用【0,1】均匀分布随机初始化二维数组
import torch as t
x = t.Tensor(5,3)
x= t.rand(5,3)
print(x)
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
3.查看x形状和列的个数
import torch as t
x = t.rand(5,3)
print(x.size())
print(x.size()[0])
print(x.size(1))
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
4.加法的三种写法
import torch as t
x = t.rand(5,3)
y = t.rand(5,3)
print("最初y,x")
print(y)
print(x)
print("第一种加法,y的结果")
print(x+y)
print("第二种加法,y的结果")
print(t.add(x,y))
print("第三种加法,y的结果")
result=t.Tensor(5,3)
t.add(x,y,out=result)
print(result)
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
5.对y的两种加法对比
import torch as t
x = t.rand(5,3)
y = t.rand(5,3)
print("最初y,x")
print(y)
print(x)
print("第一种加法,y的结果")
print(y.add(x))
print("第二种加法,y的结果")
print(y.add_(x))
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
6.Tensor的选取操作
import torch as t
x = t.rand(5,3)
print(x)
print(x[:, 1])
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
7.Tensor与numpy之间的互操作
import torch as t
import numpy as np
a=t.ones(5)
print(a)
b=a.numpy()
print(b)
a=np.ones(5)
b=t.from_numpy(a)
print(a)
print(b)
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
8.Tensor与numpy共同改变
import torch as t
import numpy as np
a=np.ones(5)
b=t.from_numpy(a)
b.add_(1)
print(a)
print(b)
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
二.Tensor和autograd
1.创建tensor
import torch as t
a=t.Tensor(2,3)
print(a)
b=t.Tensor([[1,2,3],[4,5,6]])
print(b)
b.tolist()
print(b)
b_size=b.size()
print(b_size)
print(b.numel())
c=t.Tensor(b_size)
d=t.Tensor((2,3))
print(c)
print(d)
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
2. 查看形状
import torch as t
c=t.Tensor(b_size)
print(c.shape)
print(c.size)
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
3. 其他创建方法
import torch as t
print(t.ones(2,3))
print(t.zeros(2,3))
print(t.arange(1,6,2))
print(t.linspace(1,10,3))
print(t.randn(2,3))
print(t.randperm(5))
print(t.eye(2,3))
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
4.常用的tensor操作
import torch as t
a=t.arange(0,6)
a.view(2,3)
print(a)
b=a.view(-1,3)
print(b)
print(b.unsqueeze(1))
print(b.unsqueeze(-2))
c=b.view(1,1,1,2,3)
c.squeeze(0)
print(c)
c.squeeze()
a[1]=100
print(b)
b.resize_(1,3)
print(b)
b.resize_(3,3)
print(b)
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
5.索引操作
import torch as t
a=t.randn(3,4)
print(a)
print(a[0])
print(a[:0])
print(a[0][2])
print(a[0,-1])
print(a[:2])
print(a[:2,0:2])
print(a[0:1,:2])
print(a[0,:2])
print(a>1)
print(a[a>1])
print(a[t.LongTensor([0,1])])
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
6.gather,scatter_操作
import torch as t
a=t.arange(0,16).view(4,4)
print(a)
index=t.LongTensor([[0,1,2,3]])
print(a.gather(0,index))
index=t.LongTensor([[3,2,1,0]]).t()
print(a.gather(1,index))
index=t.LongTensor([[0,1,2,3]])
print(a.gather(0,index))
index=t.LongTensor([[0,1,2,3],[3,2,1,0]]).t()
b=a.gather(1,index)
print(b)
#scatter
c=t.zeros(4,4,dtype=t.int64)
c.scatter_(1,index,b).float()
print(c)
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
7.高级索引
import torch as t
x=t.arange(0,27).view(3,3,3)
print(x)
print(x[[1,2],[1,2],[2,0]])
print(x[[2,1,0],[0],[1]])
print(x[[0,2],...])
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
8.tensor类型
import torch as t
t.set_default_tensor_type('torch.DoubleTensor')
a=t.Tensor(2,3)
print(a)
b=a.float()
print(b)
c=a.type_as(b)
print(c)
d=a.new(2,3)
print(d)
print(a.new)
t.set_default_tensor_type('torch.FloatTensor')
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------
9.逐元素操作
import torch as t
a=t.arange(0,6).view(2,3)
print(t.cos(a))
print(a%3)
print(a**2)
print(a)
print(t.clamp(a,min=3))
----------------------------------------------在pycharm中的运行结果-------------------------------------------------------