I'm doing a TensorFlow tutorial, where they convert an array of the numbers [1,2,3] to a tensor like this:

const xs = tf.tensor2d([1, 2, 3], [3, 1])

The shape is [3,1] because there is one row with 3 numbers. My question is, why would they use a 2D tensor, isn't this just exactly the same as:

const xs = tf.tensor1d([1, 2, 3])
  • $\begingroup$ This seems to be just a programming question/issue. Am I right? If yes, then you should ask this question on Stack Overflow, as programming questions are generally off-topic here. Please, if you have some time, take a look at our on-topic page, where we describe what kind of question you can ask here. $\endgroup$ – nbro Apr 16 at 9:53
  • $\begingroup$ I posted it here because it's not really about code but about the difference in 1d or 2d tensors. Unless that is also considered code? $\endgroup$ – Kokodoko Apr 16 at 10:17
  • $\begingroup$ To give a proper answer, I think we will need more context, i.e. please provide the link to the tutorial where the first version of the code is used. $\endgroup$ – nbro Apr 16 at 10:53

The required shape of the tensor $T$ depends on the shape of other tensors that are involved in the same operations of that same tensor $T$ and the required/desired shape of the resulting tensor, in the same way that the number of columns of the matrix $M \in \mathbb{R}^{n \times m}$ needs to match the number of rows of the matrix $M' \in \mathbb{R}^{n' \times m'}$ when you perform the matrix multiplication $M M'$, i.e. in order for $M M'$ to be well-defined, $m = n'$.

So, in your case, although the tensors contain the same elements, it might not be possible to use both in the same operations. Without more context/details, I cannot specifically answer why the first tensor was used in the tutorial you're mentioning.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.