Creating tensors with null as batch dimension in tensorflow.js

Viewed 803

My model expects an input of [null,12] as suggested by the error. When i create a tensor

tf.tensor([12 values], [null, 12])

It throws this Error: Tensor must have a shape comprised of positive integers but got shape [,13]. How do i fix this ?

1 Answers

The model expects a tensor of shape [null, 12], null is there to indicate that there is a batch dimension which is a positive integer. A tensor itself cannot be created with one of its dimension value to be null.

The error can be fix by

tf.tensor([12 values], [1, 12])

There is 12 values - which can be seen as one batch of 12 elements. Consequently, The shape is [1, 12].

If the batch size is not known, the tensor can be created that way:

tf.tensor([12 values]).reshape([-1, 12])

In the latter case, the shape is saying: "I don't know how many elements there are, but it is a multiple of 12 - figure out the batch size by yourself". But if the values happens not to be a multiple of 12, it will throw an error when creating the tensor.

Related