PyTorch(1):安装和基本函数介绍
PyTorch学习笔记。
内容包含神经网络的介绍,PyTorch的安装、基本函数等内容。
PyTorch安装
PyTorch官网:https://pytorch.org/ (2020/6/22)
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
Torch vs Numpy
torch ≈ 神经网络中的numpy
np_data = np.arange(6).reshape((2, 3))
torch_data = torch.from_numpy(np_data) # np 转化为 torch
tensor2array = torch_data.numpy() # torch 转化为 np
# abs
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 32-bit floating point
print(
'\nabs',
'\nnumpy: ', np.abs(data), # [1 2 1 2]
'\ntorch: ', torch.abs(tensor) # [1 2 1 2]
)
# sin
print(
'\nsin',
'\nnumpy: ', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743]
'\ntorch: ', torch.sin(tensor) # [-0.8415 -0.9093 0.8415 0.9093]
)
# mean
print(
'\nmean',
'\nnumpy: ', np.mean(data)