N
N
Nikita Kargin2021-10-30 19:52:28
Neural networks
Nikita Kargin, 2021-10-30 19:52:28

Why does the Shapes (None, 1) and (None, 8) are incompatible error occur?

I have a model that needs to handle streaming video where each frame is represented as an array of 63 numbers, each array's shape is (63,). I wrote a model in TensorFlow. Here is the code:

def define_model():
    inputs = Input(shape=(63,))
    f1 = Embedding(8, 256, mask_zero=True)(inputs)
    f2 = Dropout(0.5)(f1)
    f3 = LSTM(256)(f2)
    f4 = Dense(256, activation='relu')(f3)
    outputs = Dense(8, activation='softmax')(f4)
    model = Model(inputs=inputs, outputs=outputs)
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    # summarize model
    print(model.summary())
    plot_model(model, to_file='model.png', show_shapes=True)
    return model
model=define_model()
epochs=10
dataset='C:/Users/Admin/Documents/Volume/Dataset'
steps=len(os.listdir(dataset))
for i in range(epochs):
    generator = generate(landmarks)
    model.fit(generator, epochs=1, steps_per_epoch= steps, verbose=1)
    model.save("models/model_" + str(i) + ".h5")

When I try to run this code, the following error is thrown:
ValueError: in user code:

    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\engine\training.py:853 train_function  *
        return step_function(self, iterator)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\engine\training.py:842 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1286 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2849 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3632 _call_for_each_replica
        return fn(*args, **kwargs)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\engine\training.py:835 run_step  **
        outputs = model.train_step(data)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\engine\training.py:788 train_step
        loss = self.compiled_loss(
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\engine\compile_utils.py:201 __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\losses.py:141 __call__
        losses = call_fn(y_true, y_pred)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\losses.py:245 call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\tensorflow\python\util\dispatch.py:206 wrapper
        return target(*args, **kwargs)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\losses.py:1665 categorical_crossentropy
        return backend.categorical_crossentropy(
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\tensorflow\python\util\dispatch.py:206 wrapper
        return target(*args, **kwargs)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\backend.py:4839 categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)
    c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\tensorflow\python\framework\tensor_shape.py:1161 assert_is_compatible_with
        raise ValueError("Shapes %s and %s are incompatible" % (self, other))

    ValueError: Shapes (None, 1) and (None, 8) are incompatible

I see that the shape (None, 8) is in the output layer, but where does the shape (None,1) come from?
Here is my model:
617d783f2653e661164227.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question