import torch
x = torch.arange(12)
x
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
x.shape
torch.Size([12])
x.numel()
12
x=x.reshape(3,4)
x
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
torch.zeros((2,3,4))
tensor([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])、
torch.ones((2,3,4))
tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])
torch.tensor([[[2,1,3,4],[1,1,1,1]]])
tensor([[[2, 1, 3, 4],
[1, 1, 1, 1]]])
torch.tensor([[[2,1,3,4],[1,1,1,1]]]).shape
torch.Size([1, 2, 4])
逻辑运算
x= torch.tensor([1.0,2,4,8])
y=torch.tensor([2])
x+y,x-y,x*y,x/y,x**y
(tensor([ 3., 4., 6., 10.]),
tensor([-1., 0., 2., 6.]),
tensor([ 2., 4., 8., 16.]),
tensor([0.5000, 1.0000, 2.0000, 4.0000]),
tensor([ 1., 4., 16., 64.]))
torch.exp(x)
tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])
X=torch.arange(12,dtype = torch.float32).reshape(3,4)
X
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
Y = torch.tensor([[2.0,1,4,3],[1,2,3,4],[1,1,1,1]])
Y
tensor([[2., 1., 4., 3.],
[1., 2., 3., 4.],
[1., 1., 1., 1.]])
torch.cat((X,Y),dim = 0)
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 2., 1., 4., 3.],
[ 1., 2., 3., 4.],
[ 1., 1., 1., 1.]])
torch.cat((X,Y),dim = 1)
tensor([[ 0., 1., 2., 3., 2., 1., 4., 3.],
[ 4., 5., 6., 7., 1., 2., 3., 4.],
[ 8., 9., 10., 11., 1., 1., 1., 1.]])
逻辑运算
X ==Y
tensor([[False, True, False, True],
[False, False, False, False],
[False, False, False, False]])
X.sum()
tensor(66.)
a = torch.arange(3)
a
tensor([0, 1, 2])
a = torch.arange(3).reshape((3,1))
a
tensor([[0],
[1],
[2]])
b=torch.arange(2).reshape((1,2))
b
tensor([[0, 1]])
a + b
tensor([[0, 1],
[1, 2],
[2, 3]])`
a = torch.arange(12,dtype = torch.float32).reshape((3,4))
a
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
取
a[-1]
tensor([ 8., 9., 10., 11.])
a[1:2]
tensor([[4., 5., 6., 7.]])
a[1:]
tensor([[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
a
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
赋值
a[1,2] =9
a
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 9., 7.],
[ 8., 9., 10., 11.]])
a[0:2,:] = 12
a
tensor([[12., 12., 12., 12.],
[12., 12., 12., 12.],
[ 8., 9., 10., 11.]])
拷贝问题
before =id(Y)
before
2220309986560
Y = Y+X
id(Y)
2220310052992
z = torch .zeros_like(Y)
print('id(z):',id(z))
z[:] = X+Y
print('id(z):',id(z))
id(z): 2220310114112
id(z): 2220310114112
z = torch .zeros_like(Y)
print('id(z):',id(z))
z = X+Y
print('id(z):',id(z))
id(z): 2220310111488
id(z): 2220310111680
A =X.numpy()
B= torch.tensor(A)
type(A),type(B)
(numpy.ndarray, torch.Tensor)
单个值
a = torch.tensor([3.5])
a
tensor([3.5000])
a.item()
3.5
float(a)
3.5
int(a)
3
创建文件
import os
# os.makedirs() 方法用于递归创建目录
os.makedirs(os.path.join('..', 'data'), exist_ok=True)
data_file = os.path.join('..', 'data', 'house_tiny.csv')
with open(data_file, 'w') as f:
f.write('NumRooms,Alley,Price\n') # 列名
f.write('NA,Pave,127500\n') # 每行表示一个数据样本
f.write('2,NA,106000\n')
f.write('4,NA,178100\n')
f.write('NA,NA,140000\n')
import pandas as pd
data =pd.read_csv(data_file)
print(data)
data
NumRooms Alley Price
0 NaN Pave 127500
1 2.0 NaN 106000
2 4.0 NaN 178100
3 NaN NaN 140000
NumRooms Alley Price
0 NaN Pave 127500
1 2.0 NaN 106000
2 4.0 NaN 178100
3 NaN NaN 140000
inputs,outputs = data.iloc[:,0:2],data.iloc[:,2]
inputs = inputs.fillna(inputs.mean())
print(inputs)
NumRooms Alley
0 3.0 Pave
1 2.0 NaN
2 4.0 NaN
3 3.0 NaN
inputs =pd.get_dummies(inputs,dummy_na =True)
inputs
NumRooms Alley_Pave Alley_nan
0 3.0 1 0
1 2.0 0 1
2 4.0 0 1
3 3.0 0 1
X,Y = torch.tensor(inputs.values),torch.tensor(outputs.values)
X,Y
(tensor([[3., 1., 0.],
[2., 0., 1.],
[4., 0., 1.],
[3., 0., 1.]], dtype=torch.float64),
tensor([127500, 106000, 178100, 140000]))
a = torch.arange(12)
b=a.reshape((3,4))
b[:] = 2
a
tensor([2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
A = torch.arange(20.0*2).reshape((2,4,5))
A
tensor([[[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[10., 11., 12., 13., 14.],
[15., 16., 17., 18., 19.]],
[[20., 21., 22., 23., 24.],
[25., 26., 27., 28., 29.],
[30., 31., 32., 33., 34.],
[35., 36., 37., 38., 39.]]])
a1 = A.sum(axis = 0)
a1,a1.shape
(tensor([[20., 22., 24., 26., 28.],
[30., 32., 34., 36., 38.],
[40., 42., 44., 46., 48.],
[50., 52., 54., 56., 58.]]),
torch.Size([4, 5]))
a1 = A.sum(axis = [0,1])
a1,a1.shape
(tensor([140., 148., 156., 164., 172.]), torch.Size([5]))
a1 = A.sum(axis = [1,0])
a1,a1.shape
(tensor([140., 148., 156., 164., 172.]), torch.Size([5]))
a1 = A.sum(axis = 1)
a1,a1.shape
(tensor([[ 30., 34., 38., 42., 46.],
[110., 114., 118., 122., 126.]]),
torch.Size([2, 5]))
a1 = A.sum(axis = [1,2])
a1,a1.shape
(tensor([190., 590.]), torch.Size([2]))
a1 = A.sum(axis = [0,1,2])
a1,a1.shape
(tensor(780.), torch.Size([]))
A.mean()
tensor(19.5000)
A.sum()/A.numel()
tensor(19.5000)
A.mean(axis = 0),A,A.sum(axis = 0)/A.shape[0]
(tensor([[10., 11., 12., 13., 14.],
[15., 16., 17., 18., 19.],
[20., 21., 22., 23., 24.],
[25., 26., 27., 28., 29.]]),
tensor([[[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[10., 11., 12., 13., 14.],
[15., 16., 17., 18., 19.]],
[[20., 21., 22., 23., 24.],
[25., 26., 27., 28., 29.],
[30., 31., 32., 33., 34.],
[35., 36., 37., 38., 39.]]]),
tensor([[10., 11., 12., 13., 14.],
[15., 16., 17., 18., 19.],
[20., 21., 22., 23., 24.],
[25., 26., 27., 28., 29.]]))
sum_A = A.sum(axis = 1,keepdims =True)
sum_A
tensor([[[ 30., 34., 38., 42., 46.]],
[[110., 114., 118., 122., 126.]]])
A/sum_A
tensor([[[0.0000, 0.0294, 0.0526, 0.0714, 0.0870],
[0.1667, 0.1765, 0.1842, 0.1905, 0.1957],
[0.3333, 0.3235, 0.3158, 0.3095, 0.3043],
[0.5000, 0.4706, 0.4474, 0.4286, 0.4130]],
[[0.1818, 0.1842, 0.1864, 0.1885, 0.1905],
[0.2273, 0.2281, 0.2288, 0.2295, 0.2302],
[0.2727, 0.2719, 0.2712, 0.2705, 0.2698],
[0.3182, 0.3158, 0.3136, 0.3115, 0.3095]]])
A.cumsum(axis = 0)
tensor([[[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[10., 11., 12., 13., 14.],
[15., 16., 17., 18., 19.]],
[[20., 22., 24., 26., 28.],
[30., 32., 34., 36., 38.],
[40., 42., 44., 46., 48.],
[50., 52., 54., 56., 58.]]])
u =torch.tensor([3.0,-4.0])
torch.norm(u)
tensor(5.)
torch.abs(u).sum()
tensor(7.)
torch.norm(torch.ones((4,9)))
tensor(6.)