PyTorch入门(一)——Numpy vs Torch

非常抱歉,前几个月写论文,强化学习内容一直没更新。以后一定坚持更

下面开一篇新文:PyTorch入门,coding...

import torch
import numpy as np

np_data = np.arange(6).reshape((2, 3))
torch_data = torch.from_numpy(np_data)
tensor2array = torch_data.numpy()

print('\nnumpy', np_data)
print('\ntorch', torch_data)
print('\ntensor2array', tensor2array)

PyTorch入门(一)——Numpy vs Torch_第1张图片

# abs
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 32bit

print('\nabs', np.abs(data))
print('\ntorch', torch.abs(tensor))

PyTorch入门(一)——Numpy vs Torch_第2张图片

print('\nsin', np.sin(data))
print('\ntorch', torch.sin(tensor))

PyTorch入门(一)——Numpy vs Torch_第3张图片

data = [1, 2]
tensor = torch.FloatTensor(data) # 32bit
print('\nmean', np.mean(data))
print('\ntorch', torch.mean(tensor))

PyTorch入门(一)——Numpy vs Torch_第4张图片

data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data)
data = np.array(data)
print('\nnumpy', np.matmul(data, data))
print('\nnumpy', data.dot(data))
print('\ntensor', torch.mm(tensor, tensor))

PyTorch入门(一)——Numpy vs Torch_第5张图片

你可能感兴趣的:(python,pytorch)