Pytorch学习笔记(2)——Tensor

Tensor的引入

对于学习和使用一个开源库/框架,开发者最关心的2个问题:如何传参数,如何调用API接口。 参数一般就是指数据类型/数据结构类型, API接口具体指可以调用的函数、方法。

Tensor是pytorh中的数据类型,表示一个N dims张量。在深度学习中,输入/输出数据,网络参数都用Tensor表示。

每种深度学习框架都有其数据结构:

Framework 数据类型 特点
Caffe Blob N维矩阵
Tensorflow Tensor N维矩阵
Pytorch Tensor N维矩阵

不同类型的Tensor

pytorch中的Tensor: torch.Tensor

torch定义了7中CPU类型的Tensor, 以及8种GPU类型的Tensor:

数据类型 CPU Tensor GPU Tensor
32bit float torch.FloatTensor torch.cunda.FloatTensor
64bit float torch.DoubleTensor torch.cunda.DoubleTensor
16bit float N/A torch.cuda.HalfTensor
8bit signed int torch.ByteTensor torch.cuda.ByteTensor
8bit unsigned int torch.CharTensor torch.cuda.CharTensor
16bit signed int torch.ShortTensor torch.cuda.ShortTensor
32bit signed int torch.IntTensor torch.cuda.IntTensor
64bit signed int torch.LongTensor torch.cuda.LongTensor

Tensor的创建以及常用方法

# 导入pytorch
import torch

# 创建一个3x3全1矩阵,Tensor
x1 = torch.ones(3, 3)
print(x1)

# 5x5 全0矩阵
x2 = torch.zeros(5, 5)
print(x2)

# 与x1同维0矩阵
x4 = torch.zeros_like(x1)
print(x4)

# 与x1同维全1矩阵
x5 = torch.ones_like(x1)
print(x5)

# 对角矩阵
x6 = torch.diag(torch.from_numpy(np.array([1, 2, 3, 4, 5])))
print(x6)

# 5x5 随机矩阵
x7 = torch.rand(5, 5)
print(x7)

# 5x5 norm分布矩阵
x8 = torch.randn(5, 5)
print(x8)

# 创建一个empty Tensor
x9 = torch.empty(3, 3)
print(x9)

# 创建一个Tensor,给定数值
x10 = torch.Tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
print(x10)

# 根据已有的Tensor创建一个新的Tensor
x11 = torch.rand_like(x10, dtype=torch.float)
print(x11)

# 获取Tensor的size,  Tensor.Size 实际上是一个Tuple
print(x11.size())

# Tensor的in place 操作,会改变Tensor本身
x12 = torch.rand(3, 3)
print(x12.t_())
print(x12.copy_(x11))

# Tensor  resize/reshape操作
x13 = torch.randn(4, 4)
x14 = x13.view(16)          #16*1
print(x14)
x15 = x13.view(-1, 8)       # -1, 此维度根据另一个维度计算得到
print(x15)

# 只有一个数的Tensor,  使用xxx.item() 得到python 数值
x16 = torch.rand(1)
print(x16)
print(x16.item())

# 获取Tensor中元素的个数
x17 = torch.randn(1, 2, 3, 4, 5)
print(torch.numel(x17))
print(torch.numel(torch.zeros(4, 5)))

# 判断一个对象是否是Tensor
print(torch.is_tensor(x17))
print(torch.is_tensor(np.array([1, 2, 3, 4])))

# 判断一个对象是否为Pytorch storage object
print(torch.is_storage(torch.empty(3, 3)))          #False???
print(torch.is_storage(np.zeros(shape=(3, 3))))     #False

# 设置Tensor的数据类型,初始默认为torch.float32
print(torch.Tensor([1.2, 3]).dtype)                 #float32
torch.set_default_dtype(torch.float64)
print(torch.Tensor([1.2, 3]).dtype)                 #float64

# 获取默认的Tensor数据类型
print(torch.get_default_dtype())                    #float64
torch.set_default_dtype(torch.float32)
print(torch.get_default_dtype())                    #float32

Tensor常见的操作

  • 加减乘除
  • 矩阵运算
  • 切片
from __future__ import print_function
import torch


'''
pytorch矩阵操作
var = torch.Tensor()  返回一个Tensor

tensor1 = torch.Tensor(3, 3)
tensor2 = torch.Tensor(3, 3)
 var2 = torch.add(tensor1, tensor2)     # 矩阵加
 var3 = torch.sub(tensor1, tensor2)     # 减
 var4 = torch.mul(tensor1, tensor2)     # 乘
 var5 = torch.div(tensor1, tensor2)     # 矩阵点除
 var6 = torch.mm(tensor1, tensor2)      # 矩阵乘
 
'''

print(dir(torch.Tensor))
print(help(torch.tensor))

x1 = torch.Tensor(5, 3)  # 构造一个5x3的Tensor
x2 = torch.rand(5, 3)    # 构造一个随机初始化的Tendor
print(x1.size())
print(x2.size())


# #####################Tensor相加################################
# 2个Tensor相加
y = torch.rand(5, 3)
var1 = torch.add(x1, y)

# 2个Tensor相加
var2 = x2 + y

var3 = torch.rand(5, 3)
# 2个Tensor相加
torch.add(x1, y, out=var3)
print(var3)

# Tensor相加,y.add_() 会改变y的值
y.add_(x2)
print(y)

# #####################Tensor相减###############################
x3 = torch.rand(5, 5)
x4 = torch.rand(5, 5)

y2 = x3 - x4
y3 = torch.sub(x3, x4)
print(x3.sub_(x4))

# ###################Tensor相乘################################
x5 = torch.rand(3, 3)
x6 = torch.rand(3, 3)
y4 = x5 * x6            # 矩阵元素点乘
y5 = torch.mul(x5, x6)
print(x5.mul_(x6))

# ###################Tensor相除################################
x7 = torch.rand(5, 5)
x8 = torch.rand(5, 5)
y6 = x7 / x8
y7 = torch.div(x7, x8)
print(x7.div_(x8))

# #################Tensor矩阵乘################################
x9 = torch.rand(3, 4)
x10 = torch.rand(4, 3)
y8 = torch.mm(x9, x10)
# print(x9.mm_(x10))    # 错误用法


# ###################矩阵切片###################################
# 矩阵切片
var4 = x2[:, 1]
print(var4)

Tensor与numpy.ndarray的转换

在python中,numpy的使用很广泛,numpy.ndarray表示一个高维的列表/容器。在数值计算中,numpy.ndarray表示一个高维的矩阵。与numpy相比,python中的Tensor既可以利用CPU计算,也可以利用GPU计算,然而numpy只能在CPU上计算

import numpy as np
import torch


'''
torch.Tensor与numpy.ndarray的相互转换

'''


x1 = torch.Tensor(5, 5)

# 将Tensor转为numpy的ndarray
var1 = x1.numpy()
print(var1)

# 将numpy的ndarray转为Tensor
x2 = np.ndarray(shape=(5, 5), dtype=np.int32)
var2 = torch.from_numpy(x2)
print(var2)

链接:来源:简书

你可能感兴趣的:(pytorch学习,pytorch,深度学习,python)