Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot take the length of Shape with unknown rank

I have a neural network, from a tf.data data generator and a tf.keras model, as follows (a simplified version-because it would be too long):

dataset = ...

A tf.data.Dataset object that with the next_x method calls the get_next for the x_train iterator and for the next_y method calls the get_next for the y_train iterator. Each label is a (1, 67) array in one-hot form.

Layers:

input_tensor = tf.keras.layers.Input(shape=(240, 240, 3))  # dim of x
output = tf.keras.layers.Flatten()(input_tensor)
output= tf.keras.Dense(67, activation='softmax')(output)  # 67 is the number of classes

Model:

model = tf.keras.models.Model(inputs=input_tensor, outputs=prediction)
model.compile(optimizer=tf.train.AdamOptimizer(), loss=tf.losses.softmax_cross_entropy, metrics=['accuracy'])
model.fit_generator(gen(dataset.next_x(), dataset.next_y()), steps_per_epochs=100)

gen is defined like this:

def gen(x, y):
    while True:
        yield(x, y)

My problem is that when I try to run it, I get an error in the model.fit part:

ValueError: Cannot take the length of Shape with unknown rank.

Any ideas are appreciated!

like image 379
Cna Avatar asked Dec 03 '18 16:12

Cna


1 Answers

Could you post a longer stack-trace? I think your problem might be related to this recent tensorflow issue:

https://github.com/tensorflow/tensorflow/issues/24520

There's also a simple PR that fixes it (not yet merged). Maybe try it out yourself?

EDIT

Here is the PR: open tensorflow/python/keras/engine/training_utils.py

replace the following (line 232 at the moment):

  if (x.shape is not None
      and len(x.shape) == 1

with this:

  if tensor_util.is_tensor(x):
    x_shape_ndims = x.shape.ndims if x.shape is not None else None
  else:
    x_shape_ndims = len(x.shape)

  if (x_shape_ndims == 1
like image 135
Ciprian Avatar answered Sep 28 '22 21:09

Ciprian