Closed dazzle-me closed 1 year ago
Yes, there are more than one solutions. In your particular situation, it suffices to just replace L with -1. Also, specify input_shapes
with dynamic dimensions set to None (since v0.2.0):
inp = torch.rand(L, emb_dim)
keras_model = nobuco.pytorch_to_keras(
model, args=[inp],
input_shapes={inp: (None, emb_dim)}
)
But suppose the solution above doesn't fit you. Then it gets much more interesting. In pytorch, tensor shape is a tuple of regular integers, not scalar tensors, and it's quite difficult to track them. There's a workaround, though. You can do this:
L, dim = nobuco.shape(x)
This function returns tensors, much like tf.shape
does.
@traceable
def shape(x):
return tuple(torch.tensor(d, dtype=torch.int32) for d in x.shape)
Take a look at examples/dynamic_shape to see how it works.
Works like a charm, thank you!
Consider the following model graph, - simple MHSA layer It is, however, depended on the length of the passed sequence.
This code produces the keras model that is capable of running fixed-shape input with fixed length L. However, when I try to run it with new sequence length, the model forward pass fails with the error
Error:
It looks like that model recorded the static shape of the input and don't support varied-length input, I'm new to keras and I want to ask if there any possible solution?