Wednesday, 19 December 2018

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!



from Cannot take the length of Shape with unknown rank

1 comment:

  1. Hi,
    Please try setting the output shape in the tf dataset. That would solve the problem.


    If you could post the dataset code, I could probably look into it.

    Thanks

    ReplyDelete