A tensor is a multi-dimensional array. It is a key entity in many computational libraries, such as tensorflow, torch, theano, caffe, mxnet, used for machine learning tasks. If you use this tag, also tag the question with the corresponding library.
Questions tagged [tensor]
2802 questions
414
votes
3 answers
Keras input explanation: input_shape, units, batch_size, dim, etc
For any Keras layer (Layer class), can someone explain how to understand the difference between input_shape, units, dim, etc.?
For example the doc says units specify the output shape of a layer.
In the image of the neural net below hidden layer1…

scarecrow
- 6,624
- 5
- 20
- 39
376
votes
10 answers
How do I save a trained model in PyTorch?
How do I save a trained model in PyTorch? I have read that:
torch.save()/torch.load() is for saving/loading a serializable object.
model.state_dict()/model.load_state_dict() is for saving/loading model state.

Wasi Ahmad
- 35,739
- 32
- 114
- 161
333
votes
9 answers
What does .view() do in PyTorch?
What does .view() do to a tensor x? What do negative values mean?
x = x.view(-1, 16 * 5 * 5)

Wasi Ahmad
- 35,739
- 32
- 114
- 161
311
votes
24 answers
How to print the value of a Tensor object in TensorFlow?
I have been using the introductory example of matrix multiplication in TensorFlow.
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
When I print the product, it is displaying it as a Tensor…

Dawny33
- 10,543
- 21
- 82
- 134
172
votes
5 answers
Why do we "pack" the sequences in PyTorch?
I was trying to replicate How to use packing for variable-length sequence inputs for rnn but I guess I first need to understand why we need to "pack" the sequence.
I understand why we "pad" them but why is "packing" (via pack_padded_sequence)…

aerin
- 20,607
- 28
- 102
- 140
163
votes
4 answers
PyTorch preferred way to copy a tensor
There seems to be several ways to create a copy of a tensor in PyTorch, including
y = tensor.new_tensor(x) #a
y = x.clone().detach() #b
y = torch.empty_like(x).copy_(x) #c
y = torch.tensor(x) #d
b is explicitly preferred over a and d according…

dkv
- 6,602
- 10
- 34
- 54
109
votes
5 answers
What's the difference between "hidden" and "output" in PyTorch LSTM?
I'm having trouble understanding the documentation for PyTorch's LSTM module (and also RNN and GRU, which are similar). Regarding the outputs, it says:
Outputs: output, (h_n, c_n)
output (seq_len, batch, hidden_size * num_directions): tensor…

N. Virgo
- 7,970
- 11
- 44
- 65
101
votes
4 answers
How do I get the value of a tensor in PyTorch?
Printing a tensor x gives:
>>> x = torch.tensor([3])
>>> print(x)
tensor([3])
Indexing x.data gives:
>>> x.data[0]
tensor(3)
How do I get just a regular non-tensor value 3?

apostofes
- 2,959
- 5
- 16
- 31
98
votes
9 answers
AttributeError: 'Tensor' object has no attribute 'numpy'
How can I fix this error I downloaded this code from GitHub.
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].numpy()
throws the error
AttributeError: 'Tensor' object has no attribute 'numpy'
Please help me fix this!
I…

Frieder Hannenheim
- 1,144
- 1
- 7
- 11
83
votes
4 answers
PyTorch: How to get the shape of a Tensor as a list of int
In numpy, V.shape gives a tuple of ints of dimensions of V.
In tensorflow V.get_shape().as_list() gives a list of integers of the dimensions of V.
In pytorch, V.size() gives a size object, but how do I convert it to ints?

patapouf_ai
- 17,605
- 13
- 92
- 132
82
votes
6 answers
What is an intuitive explanation of np.unravel_index?
I have read the documentation for np.unravel_index and played around with the function, but I can't figure out what it is doing.

austinkjensen
- 956
- 1
- 8
- 11
75
votes
11 answers
PyTorch reshape tensor dimension
I want to reshape a vector of shape (5,) into a matrix of shape (1, 5).
With numpy, I can do:
>>> import numpy as np
>>> a = np.array([1, 2, 3, 4, 5])
>>> a.shape
(5,)
>>> a = np.reshape(a, (1, 5))
>>> a.shape
(1, 5)
>>> a
array([[1, 2, 3, 4,…

Haha TTpro
- 5,137
- 6
- 45
- 71
65
votes
6 answers
tf.shape() get wrong shape in tensorflow
I define a tensor like this:
x = tf.get_variable("x", [100])
But when I try to print shape of tensor :
print( tf.shape(x) )
I get Tensor("Shape:0", shape=(1,), dtype=int32), why the result of output should not be shape=(100)

Nils Cao
- 1,409
- 2
- 15
- 23
61
votes
4 answers
Understanding tensordot
After I learned how to use einsum, I am now trying to understand how np.tensordot works.
However, I am a little bit lost especially regarding the various possibilities for the parameter axes.
To understand it, as I have never practiced tensor…

floflo29
- 2,261
- 2
- 22
- 45
59
votes
1 answer
What is a batch in TensorFlow?
The introductory documentation, which I am reading (TOC here) uses the term "batch" (for instance here) without having defined it.

Jeffrey Benjamin Brown
- 3,427
- 2
- 28
- 40