search
HomeBackend DevelopmentPython TutorialDetailed example of tensor data structure in Pytorch

【Related recommendations: Python3 video tutorial

torch.Tensor

torch.Tensor is a multidimensional matrix containing elements of a single data type, similar to numpy's array. Tensor can be generated using torch.tensor() to convert Python's list or
sequence data. The generated one is dtype and the default is torch.FloatTensor.

Note

torch.tensor() always copies data. If you have a Tensor data and just want to change its requires_grad properties, use requires_grad_() or detach() to avoid copying. If you have a numpy array and want to avoid a copy, use torch.as_tensor().

1. A Tensor of a specified data type can be generated by passing parameters

torch.dtype and/or torch.device to the constructor:

Note that in order to change the torch.device and/or torch.dtype of an existing tensor, consider using the

to() method.

>>> torch.ones([2,3], dtype=torch.float64, device="cuda:0")
tensor([[1., 1., 1.],
        [1., 1., 1.]], device='cuda:0', dtype=torch.float64)
>>> torch.ones([2,3], dtype=torch.float32)
tensor([[1., 1., 1.],
        [1., 1., 1.]])
2, Tensor Content can be accessed and modified through Python indexing or slicing:

>>> matrix = torch.tensor([[2,3,4],[5,6,7]])
>>> print(matrix[1][2])
tensor(7)
>>> matrix[1][2] = 9
>>> print(matrix)
tensor([[2, 3, 4],
        [5, 6, 9]])

3, using

torch.Tensor.item() or int() methods from only Get the Python Number from the Tensor of a value:

>>> x = torch.tensor([[4.5]])
>>> x
tensor([[4.5000]])
>>> x.item()
4.5
>>> int(x)
4

4. Tensor can be created with the parameter

requires_grad=True, so torch.autograd will record the relevant The operation realizes automatic derivation:

>>> x = torch.tensor([[1., -1.], [1., 1.]], requires_grad=True)
>>> out = x.pow(2).sum()
>>> out.backward()
>>> x.grad
tensor([[ 2.0000, -2.0000],
 [ 2.0000,  2.0000]])

5, each tensor has a corresponding

torch.Storage to save its data. The tensor class provides a multidimensional, strided view and defines numerical operations.

Tensor data type

Torch defines seven CPU tensor types and eight GPU tensor types:

Detailed example of tensor data structure in Pytorch

torch. Tensor is the abbreviation of the default tensor type (torch.FloatTensor), which is the 32 bit floating point data type.

Attributes of Tensor

Tensor has many attributes, including data type, Tensor dimension, and Tensor size.

    Data type: Different tensor data types can be set by changing the dtype parameter value of the torch.tensor() method.
  • Dimension: Different types of data can be represented by tensors of different dimensions. A scalar is a 0-dimensional tensor, a vector is a 1-dimensional tensor, and a matrix is ​​a 2-dimensional tensor. Color images have three channels, rgb, and can be represented as a 3-dimensional tensor. Video also has a time dimension, which can be expressed as a 4-dimensional tensor with several square brackets [the number of dimensions. The dimensions of a tensor can be obtained using the dim() method.
  • Size: You can use the shape attribute or the size() method to view the length of the tensor in each dimension, and you can use the view() method or the reshape() method to change the size of the tensor.
The sample code is as follows:

matrix = torch.tensor([[[1,2,3,4],[5,6,7,8]],
                       [[5,4,6,7], [5,6,8,9]]], dtype = torch.float64)
print(matrix)               # 打印 tensor
print(matrix.dtype)     # 打印 tensor 数据类型
print(matrix.dim())     # 打印 tensor 维度
print(matrix.size())     # 打印 tensor 尺寸
print(matrix.shape)    # 打印 tensor 尺寸
matrix2 = matrix.view(4, 2, 2) # 改变 tensor 尺寸
print(matrix2)

The program output is as follows:

Detailed example of tensor data structure in Pytorch


The difference between view and reshape

Both methods are used to change the shape of tensor. view() is only suitable for operating tensors that meet the continuity condition (

contiguous), while reshape() ) At the same time, you can also operate on tensors that do not meet the continuity conditions. When the tensor continuity condition (contiguous) is satisfied, the result returned by a.reshape() is the same as a.view(), and no new memory space will be opened; if contiguous is not satisfied When using the view() method directly, it will fail. reshape() is still useful, but it will re-open the memory space and will not share the memory with the previous tensor, that is, it will return a "copy" (Equivalent to calling the contiguous() method first and then using the view() method). For more understanding, refer to this article

Tensor and ndarray

1, tensor and numpy array. You can use the

.numpy() method to get a numpy array from a Tensor, or you can use torch.from_numpy to get a Tensor from a numpy array. The Tensor and numpy arrays associated with these two methods share data memory. You can use the clone method of the tensor to copy the tensor to break this association.

arr = np.random.rand(4,5)
print(type(arr))
tensor1 = torch.from_numpy(arr)
print(type(tensor1))
arr1 = tensor1.numpy()
print(type(arr1))
"""
<class &#39;numpy.ndarray&#39;>
<class &#39;torch.Tensor&#39;>
<class &#39;numpy.ndarray&#39;>
"""

2,

item() method and tolist() method can convert tensors into Python numbers and lists of numbers

# item方法和tolist方法可以将张量转换成Python数值和数值列表
scalar = torch.tensor(5)  # 标量
s = scalar.item()
print(s)
print(type(s))

tensor = torch.rand(3,2)  # 矩阵
t = tensor.tolist()
print(t)
print(type(t))
"""
1.0
<class &#39;float&#39;>
[[0.8211846351623535, 0.20020723342895508], [0.011571824550628662, 0.2906131148338318]]
<class &#39;list&#39;>
"""

创建 Tensor

创建 tensor ,可以传入数据或者维度,torch.tensor() 方法只能传入数据,torch.Tensor() 方法既可以传入数据也可以传维度,强烈建议 tensor() 传数据,Tensor() 传维度,否则易搞混。

传入维度的方法

方法名 方法功能 备注
torch.rand(*sizes, out=None) → Tensor 返回一个张量,包含了从区间 [0, 1)均匀分布中抽取的一组随机数。张量的形状由参数sizes定义。 推荐
torch.randn(*sizes, out=None) → Tensor 返回一个张量,包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数。张量的形状由参数sizes定义。 不推荐
torch.normal(means, std, out=None) → Tensor 返回一个张量,包含了从指定均值 means 和标准差 std 的离散正态分布中抽取的一组随机数。标准差 std 是一个张量,包含每个输出元素相关的正态分布标准差。 多种形式,建议看源码
torch.rand_like(a) 根据数据 a 的 shape 来生成随机数据 不常用
torch.randint(low=0, high, size) 生成指定范围(low, hight)和 size 的随机整数数据 常用
torch.full([2, 2], 4) 生成给定维度,全部数据相等的数据 不常用
torch.arange(start=0, end, step=1, *, out=None) 生成指定间隔的数据 易用常用
torch.ones(*size, *, out=None) 生成给定 size 且值全为1 的矩阵数据 简单
zeros()/zeros_like()/eye() 0 的 tensor 和 对角矩阵 简单

样例代码:

>>> torch.rand([1,1,3,3])
tensor([[[[0.3005, 0.6891, 0.4628],
          [0.4808, 0.8968, 0.5237],
          [0.4417, 0.2479, 0.0175]]]])
>>> torch.normal(2, 3, size=(1, 4))
tensor([[3.6851, 3.2853, 1.8538, 3.5181]])
>>> torch.full([2, 2], 4)
tensor([[4, 4],
        [4, 4]])
>>> torch.arange(0,10,2)
tensor([0, 2, 4, 6, 8])
>>> torch.eye(3,3)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

【相关推荐:Python3视频教程

The above is the detailed content of Detailed example of tensor data structure in Pytorch. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6?What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6?Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.