import torch
torch.__version__
'1.8.1+cpu'
文章目录
一、数据操作
- 标量 1d 例如:1.5
- 向量 2d 例如:[1,2]
- 矩阵 3d 例如:[[1,2][3,4]]
x = torch.arange(12)
x
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
x.shape
torch.Size([12])
x.reshape(3,4)
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
x.numel()
12
torch.zeros((12))
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
torch.zeros((1,2))
tensor([[0., 0.]])
torch.zeros((1,2,3))
tensor([[[0., 0., 0.],
[0., 0., 0.]]])
torch.ones((1,2,3))
tensor([[[1., 1., 1.],
[1., 1., 1.]]])
torch.randn((3,4))
tensor([[ 0.2384, -0.6686, -0.6075, 0.7796],
[-0.7097, -0.8295, -0.4758, 0.3790],
[-0.5237, 0.9664, -0.0939, -0.5642]])
torch.tensor([1,2,3])
tensor([1, 2, 3])
x = torch.tensor([1,2,3])
y = torch.tensor([2,2,2])
x+y,x-y,x*y,x/y,x**y
(tensor([3, 4, 5]),
tensor([-1, 0, 1]),
tensor([2, 4, 6]),
tensor([0.5000, 1.0000, 1.5000]),
tensor([1, 4, 9]))
torch.exp(x)
tensor([ 2.7183, 7.3891, 20.0855])
x = torch.arange(12,dtype=torch.float32).reshape((3,4))
y = torch.tensor([[1,2,3,4],[1,2,3,4],[1,2,3,4]])
torch.cat((x,y),dim=0)
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.],
[ 1., 2., 3., 4.]])
torch.cat((x,y),dim=1)
tensor([[ 0., 1., 2., 3., 1., 2., 3., 4.],
[ 4., 5., 6., 7., 1., 2., 3., 4.],
[ 8., 9., 10., 11., 1., 2., 3., 4.]])
x == y
tensor([[False, False, False, False],
[False, False, False, False],
[False, False, False, False]])
x.sum()
tensor(66.)
a = torch.arange(3).reshape((3,1))
b = torch.arange(2).reshape((1,2))
a,b
(tensor([[0],
[1],
[2]]),
tensor([[0, 1]]))
a+b
tensor([[0, 1],
[1, 2],
[2, 3]])
x[-1],x[1:3]
(tensor([ 8., 9., 10., 11.]),
tensor([[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]]))
x[1,2] = 9
x
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 9., 7.],
[ 8., 9., 10., 11.]])
x[0:2,:] = 12
x
tensor([[12., 12., 12., 12.],
[12., 12., 12., 12.],
[ 8., 9., 10., 11.]])
before = id(y)
y = y + x
id(y) == before
False
a = x.numpy()
a
array([[12., 12., 12., 12.],
[12., 12., 12., 12.],
[ 8., 9., 10., 11.]], dtype=float32)
b = torch.tensor(a)
b
tensor([[12., 12., 12., 12.],
[12., 12., 12., 12.],
[ 8., 9., 10., 11.]])
a = torch.tensor([3.5])
a,a.item(),float(a),int(a)
(tensor([3.5000]), 3.5, 3.5, 3)
二、数据预处理
import os
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)
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)
print(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
import torch
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]))