Pytorch基础入门——Tensor的概基础知识
最近在研究舆情监测,在做自然语言处理部分的时候需要用到深度学习的方法进行特征提取和建模预测,因此在这里学习了下Pytorch。之后整个监测系统做好之后发布到博客里和大家学习交流一下。
1.Tensor张量概念
首先来介绍下Pytorch的入门知识,这部分概念参考自https://www.jianshu.com/p/5ae644748f21,感兴趣的小伙伴可以去看作者的原文。
要介绍Tensor这个数据类型,我觉得有必要扯一下数学。
我们都知道:
标量(Scalar)是只有大小,没有方向的量,如1,2,3等
向量(Vector)是有大小和方向的量,其实就是一串数字,如(1,2)
矩阵(Matrix)是好几个向量拍成一排合并而成的一堆数字,如[1,2;3,4]

那么张量(Tensor)是什么呢?呵呵呵呵!大家估计也能猜出来!是按照三维排列的一堆数字?
是的。但是也不完全正确。
其实标量,向量,矩阵它们三个也是张量,标量是零维的张量,向量是一维的张量,矩阵是二维的张量。

除此之外,张量还可以是四维的、五维的、。。。等等。
2.Tensor和Numpy
讲到Tensor的时候需要再提到一下Numpy,二者在进行数据科学处理的时候各有所长,可以完成不同的任务。
1)首先构造一个numpy array(以下代码部分来源于https://www.kaggle.com/kanncaa1/pytorch-tutorial-for-deep-learning-lovers),感兴趣的小伙伴可以去看作者的原文。
# import numpy library
import numpy as np
# numpy array
array = [[1,2,3],[4,5,6]]
first_array = np.array(array) # 2x3 array
print("Array Type: {}".format(type(first_array))) # type
print("Array Shape: {}".format(np.shape(first_array))) # shape
print(first_array)
得到的结果为:
Array Type: <class 'numpy.ndarray'> Array Shape: (2, 3) [[1 2 3] [4 5 6]]
2) 接着构造一个Tensor
# import pytorch library
import torch
# pytorch array
tensor = torch.Tensor(array)
print("Array Type: {}".format(tensor.type)) # type
print("Array Shape: {}".format(tensor.shape)) # shape
print(tensor)
得到的结果为:
Array Type: <built-in method type of Tensor object at 0x7fde8c4685a0>
Array Shape: torch.Size([2, 3])
tensor([[1., 2., 3.],
[4., 5., 6.]])
3)Tensor与Numpy对比
例子1:
# numpy ones
print("Numpy {}\n".format(np.ones((2,3))))
# pytorch ones
print(torch.ones((2,3)))
得到的结果为:
Numpy [[1. 1. 1.]
[1. 1. 1.]]
tensor([[1., 1., 1.],
[1., 1., 1.]])
例子2:
# numpy random
print("Numpy {}\n".format(np.random.rand(2,3)))
# pytorch random
print(torch.rand(2,3))
得到的结果为:
Numpy [[0.9917393 0.10286286 0.61706085]
[0.03963639 0.01170117 0.35111965]]
tensor([[0.1462, 0.7302, 0.8815],
[0.5960, 0.1152, 0.6170]])
4)Tensor与Numpy的转化
4-1)Numpy转Tensor
torch.from_numpy()
4-2)Tensor转Numpy
numpy()
例子:
# random numpy array
array = np.random.rand(2,2)
print("{} {}\n".format(type(array),array))
# from numpy to tensor
from_numpy_to_tensor = torch.from_numpy(array)
print("{}\n".format(from_numpy_to_tensor))
# from tensor to numpy
tensor = from_numpy_to_tensor
from_tensor_to_numpy = tensor.numpy()
print("{} {}\n".format(type(from_tensor_to_numpy),from_tensor_to_numpy))
得到的结果为:
<class 'numpy.ndarray'> [[0.27035945 0.97245412]
[0.63432523 0.94168862]]
tensor([[0.2704, 0.9725],
[0.6343, 0.9417]], dtype=torch.float64)
<class 'numpy.ndarray'> [[0.27035945 0.97245412]
[0.63432523 0.94168862]]
5)Tensor基础数学计算
常见的计算方式如下所示:
Resize: view()
a and b are tensor.
Addition: torch.add(a,b) = a + b
Subtraction: a.sub(b) = a - b
Element wise multiplication: torch.mul(a,b) = a * b
Element wise division: torch.div(a,b) = a / b
Mean: a.mean()
Standart Deviation (std): a.std()
例子:
# create tensor
tensor = torch.ones(3,3)
print("\n",tensor)
# Resize
print("{}{}\n".format(tensor.view(9).shape,tensor.view(9)))
# Addition
print("Addition: {}\n".format(torch.add(tensor,tensor)))
# Subtraction
print("Subtraction: {}\n".format(tensor.sub(tensor)))
# Element wise multiplication
print("Element wise multiplication: {}\n".format(torch.mul(tensor,tensor)))
# Element wise division
print("Element wise division: {}\n".format(torch.div(tensor,tensor)))
# Mean
tensor = torch.Tensor([1,2,3,4,5])
print("Mean: {}".format(tensor.mean()))
# Standart deviation (std)
print("std: {}".format(tensor.std()))
得到的结果为:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
torch.Size([9])tensor([1., 1., 1., 1., 1., 1., 1., 1., 1.])
Addition: tensor([[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.]])
Subtraction: tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
Element wise multiplication: tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
Element wise division: tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
Mean: 3.0
std: 1.5811388492584229
浙公网安备 33010602011771号