CNTK
CNTK copied to clipboard
ops.to_sequence raises error if no sequence length is the max length
If none of the values given to the sequence_lengths parameter matches the dimension of the most significant static axis, to_sequence raises an error.
The following works as expected:
import cntk as C
import numpy as np
static = C.input_variable((10, 5))
lengths = C.input_variable(())
seq = C.to_sequence(static, lengths)
static_data = np.random.rand(2, 10, 5).astype(np.float32)
lengths_data = np.array([3, 10], dtype=np.float32) # One length is 10
res = seq(static_data, lengths_data)
[r.shape for r in res]
# [(3, 5), (10, 5)]
When no length is 10 (max), an error is raised:
[...]
static_data = np.random.rand(2, 10, 5).astype(np.float32)
lengths_data = np.array([3, 7], dtype=np.float32) # No length is 10
res = seq(static_data, lengths_data)
[...]
ValueError: Invalid Value object: data and mask are incompatible. The leading dimensions of the data with shape '[2 x 10 x 5]' do not match the dimensions of the mask with shape '[2 x 5]
[...]
That's expected, otherwise it would generate data with wasted padding in memory and computation. If you are converting from other toolkits, I'd suggest using jagged numpy array as input, instead of using to_sequence()
I see.
I ran into it when I was doing a fixed-length workaround for a nested sequential model and was able to solve my problem using sequence.gather instead.