import numpy as np
import torch
torch.linspace(0.1,1,5)
tensor([0.1000, 0.3250, 0.5500, 0.7750, 1.0000])
print(torch.empty([3,4]))
print(torch.empty(3,5))
tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
tensor([[1.0286e-38, 1.0194e-38, 9.6429e-39, 9.2755e-39, 9.1837e-39],
[9.3674e-39, 1.0745e-38, 1.0653e-38, 9.5510e-39, 1.0561e-38],
[1.0194e-38, 1.1112e-38, 1.0561e-38, 9.9184e-39, 1.0653e-38]])
torch.logspace(2,6,5,base = 2)
tensor([ 4., 8., 16., 32., 64.])
torch.full((3,4),2)
tensor([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
torch.eye(5)
tensor([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
依概率分布创建张量
print(torch.normal(2, 3, size=(1,5)))
print(torch.normal(mean = torch.arange(1.,11.), std = torch.arange(1,0,-0.1)))
tensor([[6.0627, 3.5489, 3.7053, 0.9814, 3.8663]])
tensor([-0.7955, 1.0690, 3.7225, 3.0768, 4.5076, 5.9667, 6.9369, 8.6122,
8.8240, 9.8426])
torch.randn((3,4))
tensor([[-0.1535, -1.9643, -0.8889, -0.4511],
[ 0.4905, 0.1066, 0.5044, 1.4474],
[ 1.2457, -0.8081, 2.6163, -1.1373]])
torch.randint(1, 10, (2,3))
tensor([[3, 7, 8],
[9, 7, 8]])
torch.randperm(5)
tensor([2, 0, 1, 4, 3])
s = torch.empty(3, 3).uniform_(0,1)
print(s)
print(torch.bernoulli(s))
tensor([[0.8420, 0.6974, 0.6081],
[0.2279, 0.3899, 0.1904],
[0.9900, 0.6994, 0.6235]])
tensor([[1., 1., 1.],
[1., 1., 0.],
[1., 1., 0.]])
张量拼接与切分
x = torch.randint(1, 10, (2, 3))
torch.cat((x, x, x),dim = 0)
tensor([[7, 3, 7],
[7, 4, 7],
[7, 3, 7],
[7, 4, 7],
[7, 3, 7],
[7, 4, 7]])
y = torch.randint(1, 10, (2,3))
torch.stack((x,y),1)
tensor([[[7, 3, 7],
[9, 1, 8]],
[[7, 4, 7],
[9, 5, 9]]])
a = torch.arange(10).reshape(5,2)
print('a:',a)
print('为int时:',torch.split(a, 1))
print('为list时:',torch.split(a,[2,3]))
a: tensor([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
为int时: (tensor([[0, 1]]), tensor([[2, 3]]), tensor([[4, 5]]), tensor([[6, 7]]), tensor([[8, 9]]))
为list时: (tensor([[0, 1],
[2, 3]]), tensor([[4, 5],
[6, 7],
[8, 9]]))
张量索引
x = torch.randn(3, 4)
print(x)
indices = torch.tensor([0, 2])
torch.index_select(x, 1, indices)
tensor([[-0.3880, 0.3159, -1.5236, -1.0030],
[ 1.2080, 0.5500, 0.0146, -1.0543],
[-1.7705, 0.6398, 2.2544, -2.0102]])
tensor([[-0.3880, -1.5236],
[ 1.2080, 0.0146],
[-1.7705, 2.2544]])
x = torch.randn(3,4)
print(x)
mask = x.ge(0.5)
print(mask)
torch.masked_select(x, mask)
tensor([[ 0.0288, -0.0047, 1.6408, 0.2148],
[-0.7997, -0.3897, 1.1881, -0.5227],
[-0.5981, 1.8946, 0.1165, 0.7575]])
tensor([[False, False, True, False],
[False, False, True, False],
[False, True, False, True]])
tensor([1.6408, 1.1881, 1.8946, 0.7575])
张量变换
torch.arange(10).reshape(2,5)
tensor([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
x = torch.randint(1,10,(3,4))
print(x)
y = torch.transpose(x, 0, 1)
print(y)
tensor([[2, 1, 9, 7],
[9, 4, 5, 8],
[6, 9, 9, 8]])
tensor([[2, 9, 6],
[1, 4, 9],
[9, 5, 9],
[7, 8, 8]])
x = torch.zeros(2, 1, 2, 1, 2)
print(x.shape)
y = torch.squeeze(x)
print(y,'\n',y.shape)
torch.Size([2, 1, 2, 1, 2])
tensor([[[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.]]])
torch.Size([2, 2, 2])
x = torch.tensor([1, 2, 3, 4])
torch.unsqueeze(x, dim = 1)
tensor([[1],
[2],
[3],
[4]])
线性回归Demo
x = torch.rand(20,1) * 10
y = 2 * x + (torch.rand(20,1) + 6)
w = torch.randn((1), requires_grad=True)
b = torch.randn((1), requires_grad=True)
lr = 0.05
for iterator in range(100):
wx = torch.mul(w, x)
y_pre = torch.add(wx,b)
loss = (0.5 * (y - y_pre) ** 2).mean()
loss.backward()
w.data.sub_(lr * w.grad)
b.data.sub_(lr * b.grad)
w.grad.data.zero_()
b.grad.data.zero_()
print(w.data, b.data)
tensor([2.1755]) tensor([5.2428])