如何在Pytorch中使用一维张量

204 阅读7分钟

PyTorch是一个基于Python语言的开源深度学习框架。它允许你建立、训练和部署深度学习模型,提供了很多通用性和效率。

PyTorch主要专注于张量操作,而张量可以是数字、矩阵或多维数组。

在本教程中,我们将对一维张量进行一些基本操作,因为它们是复杂的数学对象,是PyTorch库的重要组成部分。因此,在进入细节和更高级的概念之前,应该先了解基础知识。

通过本教程后,您将

  • 了解PyTorch中一维张量操作的基础知识。
  • 了解张量的类型和形状,并进行张量的切片和索引操作。
  • 能够在张量对象上应用一些方法,如平均值、标准差、加法、乘法等。

一维张量的类型和形状

首先,让我们导入一些我们在本教程中要使用的库。

import torch
import numpy as np 
import pandas as pd

如果你有其他编程语言的经验,理解张量的最简单方法是把它看作一个多维数组。因此,一维张量只是一个一维数组,或一个矢量。为了将一个整数列表转换为张量,应用torch.tensor() 构造函数。例如,我们将采取一个整数列表并将其转换为各种张量对象。

int_to_tensor = torch.tensor([10, 11, 12, 13])
print("Tensor object type after conversion: ", int_to_tensor.dtype)
print("Tensor object type after conversion: ", int_to_tensor.type())
Tensor object type after conversion:  torch.int64
Tensor object type after conversion:  torch.LongTensor

另外,你也可以应用同样的方法torch.tensor()将一个浮动列表转换为一个浮动张量。

float_to_tensor = torch.tensor([10.0, 11.0, 12.0, 13.0])
print("Tensor object type after conversion: ", float_to_tensor.dtype)
print("Tensor object type after conversion: ", float_to_tensor.type())
Tensor object type after conversion:  torch.float32
Tensor object type after conversion:  torch.FloatTensor

注意,需要转换为张量的列表的元素必须具有相同的类型。此外,如果你想把一个列表转换为某个张量类型,torch也允许你这样做。例如,下面的代码行将把一个整数列表转换为一个浮动张量。

int_list_to_float_tensor = torch.FloatTensor([10, 11, 12, 13])
int_list_to_float_tensor.type()
print("Tensor  type after conversion: ", int_list_to_float_tensor.type())
Tensor  type after conversion:  torch.FloatTensor

同样,size()ndimension() 方法允许你找到一个张量对象的大小和尺寸。

print("Size of the int_list_to_float_tensor: ", int_list_to_float_tensor.size())
print("Dimensions of the int_list_to_float_tensor: ",int_list_to_float_tensor.ndimension())
Size of the int_list_to_float_tensor:  torch.Size([4])
Dimensions of the int_list_to_float_tensor:  1

对于重塑一个张量对象,可以应用view() 方法。它以rowscolumns 为参数。作为一个例子,让我们使用这个方法来重塑int_list_to_float_tensor

reshaped_tensor = int_list_to_float_tensor.view(4, 1)
print("Original Size of the tensor: ", reshaped_tensor)
print("New size of the tensor: ", reshaped_tensor)
Original Size of the tensor:  tensor([[10.],
        [11.],
        [12.],
        [13.]])
New size of the tensor:  tensor([[10.],
        [11.],
        [12.],
        [13.]])

正如你所看到的,view() 方法已经将张量的大小改为torch.Size([4, 1]) ,有4行和1列。

虽然在应用view() 方法后,张量对象中的元素数量应该保持不变,但你可以使用-1 (如 reshaped_tensor**.**view(-1, 1))来重塑一个动态大小的张量。

将Numpy数组转换为张量

Pytorch也允许你将NumPy数组转换为张量。你可以使用torch.from_numpy 来进行这个操作。让我们取一个NumPy数组并应用该操作。

numpy_arr = np.array([10.0, 11.0, 12.0, 13.0])
from_numpy_to_tensor = torch.from_numpy(numpy_arr)

print("dtype of the tensor: ", from_numpy_to_tensor.dtype)
print("type of the tensor: ", from_numpy_to_tensor.type())
dtype of the tensor:  torch.float64
type of the tensor:  torch.DoubleTensor

同样地,你可以将张量对象转换回NumPy数组。让我们用前面的例子来展示它是如何实现的。

tensor_to_numpy = from_numpy_to_tensor.numpy()
print("back to numpy from tensor: ", tensor_to_numpy)
print("dtype of converted numpy array: ", tensor_to_numpy.dtype)
back to numpy from tensor:  [10. 11. 12. 13.]
dtype of converted numpy array:  float64

将潘达斯系列转换为张量

你也可以将pandas系列转换为张量。为此,首先你需要用NumPy数组将pandas数列与values() 函数进行存储。

pandas_series=pd.Series([1, 0.2, 3, 13.1])
store_with_numpy=torch.from_numpy(pandas_series.values)
print("Stored tensor in numpy array: ", store_with_numpy)
print("dtype of stored tensor: ", store_with_numpy.dtype)
print("type of stored tensor: ", store_with_numpy.type())
Stored tensor in numpy array:  tensor([ 1.0000,  0.2000,  3.0000, 13.1000], dtype=torch.float64)
dtype of stored tensor:  torch.float64
type of stored tensor:  torch.DoubleTensor

此外,Pytorch框架允许我们对张量做很多事情,比如它的item() 方法从张量中返回一个python数字,tolist() 方法返回一个列表。

new_tensor=torch.tensor([10, 11, 12, 13]) 
print("the second item is",new_tensor[1].item())
tensor_to_list=new_tensor.tolist()
print('tensor:', new_tensor,"\nlist:",tensor_to_list)
the second item is 11
tensor: tensor([10, 11, 12, 13])
list: [10, 11, 12, 13]

一维张量的索引和切分

索引和切片操作在Pytorch中与python几乎相同。因此,第一个索引总是从0开始,最后一个索引小于张量的总长度。使用方括号来访问张量中的任何数字。

tensor_index = torch.tensor([0, 1, 2, 3])
print("Check value at index 0:",tensor_index[0])
print("Check value at index 3:",tensor_index[3])
Check value at index 0: tensor(0)
Check value at index 3: tensor(3)

像python中的列表一样,你也可以对张量中的值进行切片操作。此外,Pytorch库也允许你改变张量中的某些值。

让我们举个例子来看看这些操作是如何应用的。

example_tensor = torch.tensor([50, 11, 22, 33, 44])
sclicing_tensor = example_tensor[1:4]
print("example tensor : ", example_tensor)
print("subset of example tensor:", sclicing_tensor)
example tensor :  tensor([50, 11, 22, 33, 44])
subset of example tensor: tensor([11, 22, 33])

现在,让我们改变example_tensor 中索引3的值。

print("value at index 3 of example tensor:", example_tensor[3])
example_tensor[3] = 0
print("new tensor:", example_tensor)
value at index 3 of example tensor: tensor(0)
new tensor: tensor([50, 11, 22,  0, 44])

一些应用于一维张量的函数

在本节中,我们将回顾一些可以应用于张量对象的统计方法。

最小和最大函数

这两种有用的方法被用来寻找张量中的最小值和最大值。下面是它们的工作原理。

我们将使用一个sample_tensor 作为例子来应用这些方法。

sample_tensor = torch.tensor([5, 4, 3, 2, 1])
min_value = sample_tensor.min()
max_value = sample_tensor.max()
print("check minimum value in the tensor: ", min_value)
print("check maximum value in the tensor: ", max_value)
check minimum value in the tensor:  tensor(1)
check maximum value in the tensor:  tensor(5)

平均数和标准差

在对张量进行统计操作时,经常使用平均数和标准差。你可以使用Pytorch中的.mean().std() 函数来应用这两个度量。

让我们用一个例子来看看这两个度量是如何计算的。

mean_std_tensor = torch.tensor([-1.0, 2.0, 1, -2])
Mean = mean_std_tensor.mean()
print("mean of mean_std_tensor: ", Mean)
std_dev = mean_std_tensor.std()
print("standard deviation of mean_std_tensor: ", std_dev)
mean of mean_std_tensor:  tensor(0.)
standard deviation of mean_std_tensor:  tensor(1.8257)

一维张量上的简单加法和乘法操作

在Pytorch中,加法和乘法运算可以很容易地应用于张量。在本节中,我们将创建两个一维的张量来演示如何使用这些操作。

a = torch.tensor([1, 1])
b = torch.tensor([2, 2])

add = a + b
multiply = a * b

print("addition of two tensors: ", add)
print("multiplication of two tensors: ", multiply)
addition of two tensors:  tensor([3, 3])
multiplication of two tensors:  tensor([2, 2])

为了您的方便,下面是上面所有的例子串联起来的,您可以一次尝试。

import torch
import numpy as np
import pandas as pd

int_to_tensor = torch.tensor([10, 11, 12, 13])
print("Tensor object type after conversion: ", int_to_tensor.dtype)
print("Tensor object type after conversion: ", int_to_tensor.type())

float_to_tensor = torch.tensor([10.0, 11.0, 12.0, 13.0])
print("Tensor object type after conversion: ", float_to_tensor.dtype)
print("Tensor object type after conversion: ", float_to_tensor.type())

int_list_to_float_tensor = torch.FloatTensor([10, 11, 12, 13])
int_list_to_float_tensor.type()
print("Tensor  type after conversion: ", int_list_to_float_tensor.type())

print("Size of the int_list_to_float_tensor: ", int_list_to_float_tensor.size())
print("Dimensions of the int_list_to_float_tensor: ",int_list_to_float_tensor.ndimension())

reshaped_tensor = int_list_to_float_tensor.view(4, 1)
print("Original Size of the tensor: ", reshaped_tensor)
print("New size of the tensor: ", reshaped_tensor)

numpy_arr = np.array([10.0, 11.0, 12.0, 13.0])
from_numpy_to_tensor = torch.from_numpy(numpy_arr)
print("dtype of the tensor: ", from_numpy_to_tensor.dtype)
print("type of the tensor: ", from_numpy_to_tensor.type())

tensor_to_numpy = from_numpy_to_tensor.numpy()
print("back to numpy from tensor: ", tensor_to_numpy)
print("dtype of converted numpy array: ", tensor_to_numpy.dtype)

pandas_series=pd.Series([1, 0.2, 3, 13.1])
store_with_numpy=torch.from_numpy(pandas_series.values)
print("Stored tensor in numpy array: ", store_with_numpy)
print("dtype of stored tensor: ", store_with_numpy.dtype)
print("type of stored tensor: ", store_with_numpy.type())

new_tensor=torch.tensor([10, 11, 12, 13]) 
print("the second item is",new_tensor[1].item())
tensor_to_list=new_tensor.tolist()
print('tensor:', new_tensor,"\nlist:",tensor_to_list)

tensor_index = torch.tensor([0, 1, 2, 3])
print("Check value at index 0:",tensor_index[0])
print("Check value at index 3:",tensor_index[3])

example_tensor = torch.tensor([50, 11, 22, 33, 44])
sclicing_tensor = example_tensor[1:4]
print("example tensor : ", example_tensor)
print("subset of example tensor:", sclicing_tensor)

print("value at index 3 of example tensor:", example_tensor[3])
example_tensor[3] = 0
print("new tensor:", example_tensor)

sample_tensor = torch.tensor([5, 4, 3, 2, 1])
min_value = sample_tensor.min()
max_value = sample_tensor.max()
print("check minimum value in the tensor: ", min_value)
print("check maximum value in the tensor: ", max_value)

mean_std_tensor = torch.tensor([-1.0, 2.0, 1, -2])
Mean = mean_std_tensor.mean()
print("mean of mean_std_tensor: ", Mean)
std_dev = mean_std_tensor.std()
print("standard deviation of mean_std_tensor: ", std_dev)

a = torch.tensor([1, 1])
b = torch.tensor([2, 2])
add = a + b
multiply = a * b
print("addition of two tensors: ", add)
print("multiplication of two tensors: ", multiply)

总结

在本教程中,你已经发现了如何在Pytorch中使用一维张量。

具体来说,你学到了。

  • PyTorch中一维张量操作的基础知识
  • 关于张量类型和形状以及如何进行张量的切片和索引操作
  • 如何在张量对象上应用一些方法,如平均值、标准差、加法和乘法等