A tensor is just an n-dimensional array. It can contain any of the numeric data types that Python supports, including 8 bit, 16 bit, 32 bit and 64 bit integers and floats, and also Booleans and chars.
We create a tensor like this:
import torch a = torch.tensor(4)
This will create a one dimensional tensor with a single value
Typically tensors contain arrays. We might pass an array like this:
import torch a = torch.tensor([1, 2, 3])
We can print the output:
…which gives us:
tensor([1, 2, 3])
This becomes useful when we start doing maths. We can add together tensors
a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6])
print(a + b)
We can multiply them:
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) print(a \* b) # => tensor([ 4, 10, 18])
tensors can be different shapes, provided those shapes are compatible:
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor() print(a \* b) # => tensor([ 4, 8, 12])
You can specify a datatype by passing a dtype parameter, in which case you’ll get an error if you insert a value of the wrong datatype:
a = torch.tensor(, dtype=torch.int32)
or you can use one of the tensor types, like so:
a = torch.IntTensor([1, 2, 3])
You can make a tensor from a number, from an array, or from a range
torch.tensor(1) torch.tensor([1, 2]) torch.tensor(range(1000))
You can create one with incrementing numbers, like a range, but without having to make a range:
You can make an n-dimensional tensor with random numbers
a = torch.randn(10) a = torch.randn(10, 10) a = torch.randn(10, 10, 10)
or full of zeros
a = torch.zeros(10) a = torch.zeros(10, 10) a = torch.zeros(10, 10, 10)
a = torch.ones(100)
So far so nice, but why would we want this? We could do the same thing with Numpy easily enough. One answer is speed. Pytorch is going to hand over this entire array manipulation to a C backend, which will attempty to parallelize the operation. Later, when we get to CUDA, we’ll see how we can send our data to a GPU for a massive speed boost.
Here I multiply 100,000,000 integers by each other.
a = torch.IntTensor(range(100000000)) b = torch.IntTensor(range(100000000))
a * b
On my plain M1 2021 Macbook Pro, this takes about seven seconds.