ndarray = np.array(list)
# 具体例子如下
import numpy as np
a_list = [1, 2, 3]
a_np = np.array(a_list)
list = ndarray.tolist()
#具体例子如下所示
import numpy as np
a_list = [1, 2, 3]
a_np = np.array(a_list)
a_to_list = a_np.tolist()
tensor = torch.from_numpy(np.zeros(r, c))
row = 2
column = 3
a_tensor = torch.from_numpy(np.zeros((row,column)))
## 将创建好的tensor分配到指定的设备中
os.environ['CUDA_VISIBLE_DEVICES'] = '0, 1'
device = 'cuda' if torch.cuda.is_available() else 'cpu'
a_tensor_device = a_tensora_tensor.to(device)
# 此处是将a_tensor分配到cuda 0 和1
tensor=torch.Tensor(list)
a_tensor =torch.Tensor(a_list)
Tensor不可直接转换为list , 需要先转换为numpy,然后在转换为list
list = tensor.numpy().tolist()
a_list = a_tensor.numpy().tolist()
ndarray = tensor.numpy()
gpu上的tensor不能直接转为numpy
ndarray = tensor.cpu().numpy()
# 普通的tensor转换为numpy
a_np = a_tensor.numpy()
# GPU上的tensor转换为numpy
a_np_gpu = a_tensor.cpu().numpy()
import torch
a = [1, 2, 3, 5]
b = [1.2, 3.3, 5.6]
da = torch.tensor(a) # 此时da的数据是tensor.int64的类型,此时是默认的数据类型
db =torch.tensor(b) # 此时db的数据是tensor.float32的类型,此时是默认的数据类型
daa = torch.tensor(a, dtype = torch.long) # 此时da的数据是tensor.int64的类型,这是指定的数据类型
# 在指定数据类型时要注意:
# 1. 若是原本数据是整数,则指定为整数型和浮点型都没问题
# 2. 若是原本数据是小数,则指定为浮点型数据时没有问题,但是在指定为整数型会出现问题,直接将数字的小数部分直接丢掉,而只保留整数部分
# 具体例子如下所示:
import torch
a = torch.rand(1,2)
a
# output: tensor([[0.7504, 0.5853]])
b = torch.randn(1,2)
b
#output: tensor([[-0.3798, 0.0115]])
# 具体例子如下所示
torch.normal(2, 3, size=(1, 4))
# 2:表示mean
# 3:表示std
# size=(1, 4):表示输出tensor的维度大小
# 具体例子如下所示:
import torch
x=torch.full((2,3),1.2,dtype=torch.float)
x
# output: tensor([[1.2000, 1.2000, 1.2000],
[1.2000, 1.2000, 1.2000]])
# (2,3):表示指定生成tensor的维度,这里只是二维,当然可以更高维
# 1.2:表示指定的值
# dtype=torch.float:表示生成的数据的类型
单个元素tensor取值可以使用Tensor.item()
import torch
x = torch.randn(1)
y = x.item()
print('x: {}'.format(x))
print('y: {}'.format(y))
# output
# x: tensor([-1.4665])
# y: -1.4664647579193115
torch.max, torch.min不仅会返回最大值、最小值,而且在指定的维度会返回其对应的位置,但是不指定维度直接使用只会返回最大值、最小值。torch.max和torch.min用法一样,只是一个是求最大值,一个是求最小值。
# 具体以torch.max为例
import torch
a = torch.randn(2,10)
a
# output:
# tensor([[ 1.3100e+00, -3.4182e-01, -5.7281e-02, -4.9519e-04, 6.6183e-01,
# -5.5695e-01, -9.3556e-01, 3.4168e-01, 8.3933e-01, -9.3372e-01],
# [-6.6425e-01, 3.1085e-01, 1.9820e+00, -3.6192e-01, 2.3477e-01,
# 5.9145e-02, 7.1136e-01, 6.0131e-01, 1.9997e+00, 2.6330e+00]])
torch.max(a)
# output:
# tensor(2.6330)
a = torch.randn(2,3,4,3) # 2*3*4*3: N*C*H*W
a
torch.max(a, 0) # 在N这个维度上进行比较大小去最大值,并且返回其位置(是N中的哪一个)
# output:
# torch.return_types.max(
# values=tensor([[[-0.0233, 0.6172, 1.1725],
# [-0.0720, -0.7014, 0.2533],
# [ 0.5254, 1.6286, -0.6081],
# [ 0.7030, -0.7195, -0.3218]],
# [[-0.2194, -0.5759, 1.6942],
# [-0.1742, -0.2742, 1.3223],
# [ 2.0272, 0.9716, 2.2349],
# [-0.6161, 0.6193, 1.1204]],
# [[ 0.3826, -0.4263, 0.4860],
# [ 0.1900, -0.2935, 0.2704],
# [ 0.4831, 0.6423, 1.3739],
# [-0.1392, -0.3983, 0.8989]]]),
# indices=tensor([[[0, 0, 0],
# [0, 0, 1],
# [1, 0, 0],
# [1, 0, 0]],
# [[1, 1, 0],
# [1, 0, 1],
# [0, 1, 0],
# [1, 1, 1]],
# [[1, 0, 1],
# [0, 1, 0],
# [1, 1, 1],
# [1, 0, 1]]]))
torch.max(a, 1) # 在C这个维度上进行比较大小去最大值,并且返回其位置(是C中的哪一个,每个N中分别比较)
# output:
# torch.return_types.max(
# values=tensor([[[-0.0233, 0.6172, 1.6942],
# [ 0.1900, -0.2742, 0.2704],
# [ 2.0272, 1.6286, 2.2349],
# [-1.3326, -0.3983, 0.4357]],
# [[ 0.3826, -0.2523, 0.4860],
# [-0.1742, -0.2935, 1.3223],
# [ 0.5254, 0.9716, 1.3739],
# [ 0.7030, 0.6193, 1.1204]]]),
# indices=tensor([[[0, 0, 1],
# [2, 1, 2],
# [1, 0, 1],
# [2, 2, 1]],
# [[2, 0, 2],
# [1, 2, 1],
# [0, 1, 2],
# [0, 1, 1]]]))
torch.max(a, 2) # 在H这个维度上进行比较大小去最大值,并且返回其位置(是H中的哪一个,每个N中分别比较)
# output:
# torch.return_types.max(
# values=tensor([[[-0.0233, 1.6286, 1.1725],
# [ 2.0272, -0.2742, 2.2349],
# [ 0.1900, -0.3983, 0.2704]],
# [[ 0.7030, -0.2523, 0.2625],
# [-0.1742, 0.9716, 1.3223],
# [ 0.4831, 0.6423, 1.3739]]]),
# indices=tensor([[[0, 2, 0],
# [2, 1, 2],
# [1, 3, 1]],
# [[3, 0, 0],
# [1, 2, 1],
# [2, 2, 2]]]))
torch.max(a, 3) # 在W这个维度上进行比较大小去最大值,并且返回其位置(是W中的哪一个,每个N中分别比较)
# output:
# torch.return_types.max(
# values=tensor([[[ 1.1725, -0.0720, 1.6286, -0.3218],
# [ 1.6942, -0.2742, 2.2349, 0.4357],
# [-0.4263, 0.2704, 0.1508, -0.3983]],
# [[ 0.2625, 0.2533, 0.5254, 0.7030],
# [ 0.3899, 1.3223, 1.0412, 1.1204],
# [ 0.4860, -0.2935, 1.3739, 0.8989]]]),
# indices=tensor([[[2, 0, 1, 2],
# [2, 1, 2, 2],
# [1, 2, 0, 1]],
# [[2, 2, 0, 0],
# [2, 2, 2, 2],
# [2, 1, 2, 2]]]))
此处我就不写了,有份资料写的很详细:Pytorch张量高阶操作
注意:
直接使用torch.add(1)函数,张量的值会加1,但张量本身不会发生改变。而使用torch.add_(1)是直接在张量上进行加1操作,会改变张量的值。
tensor的切片和索引与numpy相同,可以参考:NumPy 切片和索引
以下的几份资料可以用作pytorch撰写神经网络模型的入门材料: