基础操作
# 注意import的是torch
import torch
x = torch.arange(12)
print(x) # 输出是tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
x.shape # 输出是torch.Size([12])
# number of element
x.numel() # 输出是12
四则运算
# 创建tensor
a = torch.rand(5, 5)
b = torch.rand(5, 5)
# 加法
a + b
torch.add(a, b)
a.add_(b)
# 减法
a - b
torch.sub(a, b)
a.sub_(b)
# 乘法
a * b
torch.mul(a, b)
a.mul_(b)
# 除法
a / b
torch.div(a, b)
a.div_(b)
数据预处理
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:
# col name
f.write('num_room,alley,price\n')
# 每行表示一个data sample
f.write('NA,pave, 127500\n')
# samples
f.write('2,NA,106000\n')
f.write('4,NA,178100\n')
f.write('NA,NA,140000\n')
# 读取csv
df = pd.read_csv(data_file)
inputs, outputs = df.iloc[:, 0:2], df.iloc[:, 2]
# deal with Numeric NA data (mean)
inputs = inputs.fillna(inputs.mean())
# dummy
inputs = pd.get_dummies(inputs, dummy_na=True)
# 已解决空值问题, 并把numeric及category数据转为数值
# to be the tensor format
x = torch.tensor(inputs.values)
y = torch.tensor(outputs.values)