Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lecture #234 - fails with 'ValueError: logits and labels must have the same shape, received ((None, 15, 1) vs (None,)' #514

Open
biologiccarbon opened this issue Feb 19, 2023 · 2 comments

Comments

@biologiccarbon
Copy link

using tensorflow 2.11.0
the lecture titled " Model 1: Building, fitting and evaluating our first deep model on text data"

fitting the 'feed forward neural network' fails with error

ValueError: logitsandlabels must have the same shape, received ((None, 15, 1) vs (None,)

if appears you now must have the following line (as per your notes but not shown in lecture)

x = layers.GlobalAveragePooling1D()(x)

@liltimtim
Copy link

Can also confirm the error received as well.

tf.__version__ #yields 2.15.0

Code Before Change

from tensorflow.keras import layers
inputs = layers.Input(shape=(1,), dtype="string")
x = text_vectorizer(inputs) # string -> number
x = embedding(x) # number -> dense vector
outputs = layers.Dense(1, activation="sigmoid")(x)

#compile the model
model_1 = tf.keras.Model(inputs, outputs)
model_1.compile(loss="binary_crossentropy",
                optimizer=tf.keras.optimizers.Adam(),
                metrics=["accuracy"])

# fit the model
history_1 = model_1.fit(x=train_sentences, y=train_labels, epochs=5, validation_data=(val_sentences, val_labels))

Working Code with Change

from tensorflow.keras import layers
inputs = layers.Input(shape=(1,), dtype="string")
x = text_vectorizer(inputs) # string -> number
x = embedding(x) # number -> dense vector
x = layers.GlobalAveragePooling1D()(x)
outputs = layers.Dense(1, activation="sigmoid")(x)

#compile the model
model_1 = tf.keras.Model(inputs, outputs)
model_1.compile(loss="binary_crossentropy",
                optimizer=tf.keras.optimizers.Adam(),
                metrics=["accuracy"])

# fit the model
history_1 = model_1.fit(x=train_sentences, y=train_labels, epochs=5, validation_data=(val_sentences, val_labels))

Error you receive from colab

Epoch 1/5

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

[<ipython-input-118-2ee86a25223c>](https://localhost:8080/#) in <cell line: 15>()
     13 
     14 # fit the model
---> 15 history_1 = model_1.fit(x=train_sentences, y=train_labels, epochs=5, validation_data=(val_sentences, val_labels))

1 frames

[/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py](https://localhost:8080/#) in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

ValueError: in user code:

    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1401, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1384, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1373, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1151, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1209, in compute_loss
        return self.compiled_loss(
    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/compile_utils.py", line 277, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/losses.py", line 143, in __call__
        losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/losses.py", line 270, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/losses.py", line 2532, in binary_crossentropy
        backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
    File "/usr/local/lib/python3.10/dist-packages/keras/src/backend.py", line 5822, in binary_crossentropy
        return tf.nn.sigmoid_cross_entropy_with_logits(

    ValueError: `logits` and `labels` must have the same shape, received ((None, 15, 1) vs (None,)).

@Friend09
Copy link

Friend09 commented May 8, 2024

my working solution: I used a flatten layer before the Dense layer and it worked


# inputs = layers.Input(shape=(1,), dtype="string")
# x = text_vectorizer(inputs)
# x = embedding(x)
# # x = layers.GlobalAveragePooling1D()(x)
# x = layers.Flatten()(x)
# outputs = layers.Dense(1, activation="sigmoid")(x)

# model_1 = tf.keras.Model(inputs, outputs, name="model_1_dense")

# model_1.compile(loss="binary_crossentropy", optimizer=tf.keras.optimizers.Adam(),metrics=["accuracy"])
# model_1.summary()```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants