- N-dimensional array
It is the main data structure of machine learning and neural networks.
- Creating an array requires
Shape: such as 3*4 matrix
The type of each element: e.g. 32-bit float
The value of each element: for example, all 0. or random numbers
- Data operations
First, import torch
A tensor represents an array of values. This array may have multiple dimensions.
The shape of a tensor and the total number of elements in the tensor can be accessed through the tensor's shape property.
If you want to change the shape of a tensor without changing the number of elements and element values, you can call the reshape function
Use all 0s, all 1s, other constants, or numbers randomly sampled from a specific distribution
Two floors, three rows, four columns
Give each element in the desired tensor a definite value by providing a Python list (or nested list) containing the numeric value
Common standard arithmetic operators (+, -, *, /, **) can be upgraded to element-wise operations
Multiple tensors can be concatenated together
0 represents splicing in the y direction, 1 represents splicing in the x direction
Construct binary vectors via logical operators
Summing all elements in a tensor produces a tensor with only one element 5
Even if the shapes are different, we can still perform element-wise operations by calling the broadcast mechanism
Copy first, then add
element access
You can use [-1] to select the last element, and [1:3] to select the second and third elements.
It is also possible to write elements to a matrix by specifying the index
To assign the same value to multiple elements, we just need to index all elements and assign values to them
Running some operations may cause memory to be allocated for new results
Perform in-place operations
x is not reused in subsequent calculations. We can also use x[:]=x+y or x+=y to reduce the memory overhead of the operation.
Convert to NumPy tensor
Convert tensor of size 1 to Python scalar
- Data preprocessing
Create an artificial dataset and store it in a csv (comma separated values) file
Load original dataset from created csv file
To handle missing data, typical methods include interpolation and deletion
For categorical or discrete values in inputs, we treat 'NAN' as a category