Uncaught (in promise) Error: Argument tensors passed to stack must be a Tensor[] or TensorLike[]
TensorFlow.js version
2.0.0
Browser version
Microsoft Edge Version 83.0.478.58 (Official build) (64-bit)
Describe the problem or feature request
Uncaught (in promise) Error: Argument tensors passed to stack must be a Tensor[] or TensorLike[]
Error occurs every time the model is fit with tensor arrays or by zipping as dataset.
Error Stack :
tensor_util_env.js:125
Uncaught (in promise) Error: Argument tensors passed to stack must be a Tensor[] or TensorLike[]
at jg (tensor_util_env.js:125)
at stack_ (array_ops.js:134)
at stack (operation.js:45)
at Object.x (array_ops.js:186)
at tape.js:167
at engine.js:425
at t.e.scopedRun (engine.js:436)
at t.e.tidy (engine.js:423)
at engine.js:1005
at s (tape.js:167)
Code to reproduce the bug / link to feature request
<script>
async function trainmodel() {
const model = tf.sequential({
layers:[tf.layers.lstm({units:100,activation:'relu',returnSequences:true,inputShape:[1,1]}),
tf.layers.lstm({units:10,activation:'relu',returnSequences:true}),
tf.layers.dense({units:1})
]
});
const X = tf.tensor3d([0.1,0.4,0.7,0.5],[4,1,1]);
const y = tf.tensor3d([0.05,0.5,0.95,0.8],[4,1,1]);
function onBatchEnd(batch, logs) {
console.log('Loss', logs.acc);
}
model.compile({
optimizer: tf.train.sgd(0.01),
loss: 'meanSquaredError',
metrics: ['accuracy']
});
model.fit(X, y, {
epochs: 3,
callbacks: {onBatchEnd}
}).then(info => {
console.log('Final accuracy', info.history.acc);
});
}
trainmodel();
</script>
cc @caisq I was able to reproduce but I noticed this happens on these values for units on LSTM but not other ones (e.g. lower values), ccing you in case you have ideas of where this might lead?
@tafsiri Thanks for letting me know. I can reproduce it too. An additional interesting observation is that the model can always run predict() correctly even though its fit() method throws the error. This seems to indicate that the problem is somewhere in the gradient tape code related to the convertToTensorArray method in tfjs-core. This is consistent with the stack trace of the error.
In addition, I observed that if the input shape is [2, 1], instead of [1, 1], then the model trains without error. This again is a hint that the bug is an edge case in the gradient tape code.
cc @dsmilkov
More info: convertToTensorArray(), which is a part of the gradient tape mechanism, expects its argument to be an array of Tensor or TensorLike. But for some reason, when input shape of the LSTM is [1, 1], i.e., only one temporal step, convertToTensorArray() receives a single Tensor as argument. If the argument is an Array
Thanks for the pointers @caisq!
@caisq I am running into this too. I have implemented stateful lstm layers and removed all my timesteps (seconds dimension is now of length 1) which caused the error. I have found that moving my number of features from the third dimension to the second dimension no longer causes this error (shape=100,4,1 instead of 100,1,4). I hope that extra small bit of info is helpful in trouble shooting.
Side question: Is it valid to move variables to different dimensions in this sense, or do these lstm layers not treat each dimension identical? (does each dimension in a stateful lstm have specific purpuses)? In other words, is this a valid temporary fix for this bug, or would you not expect it to predict properly?
I was getting the same error and solved it by placing the tensor argument into an array in the following function:
` @tensorflow/tfjs/dist/tf.node.js /**
- Stacks a list of rank-
Rtf.Tensors into one rank-(R+1)tf.Tensor. * -
- const a = tf.tensor1d([1, 2]);
- const b = tf.tensor1d([3, 4]);
- const c = tf.tensor1d([5, 6]);
- tf.stack([a, b, c]).print();
-
*
- @param tensors A list of tensor objects with the same shape and dtype.
- @param axis The axis to stack along. Defaults to 0 (the first dim). *
- @doc {heading: 'Tensors', subheading: 'Slicing and Joining'} */ function stack_(tensors, axis = 0) { const $tensors = convertToTensorArray([tensors], 'tensors', 'stack', 'string_or_numeric');
`
Ran into the same issue. It seems that the RNN layer expects an input with at least 2 time steps when training. If that is the case, I think the documentation should mention that.
Why would we need to pass into the RNN layer a sequence with just one element? From my understanding, when using stateful RNN, that might make sense (correct me if I'm wrong).