2

I'm having difficulty training my TensorFlow model using a tf.Dataset rather than, say, a pd.DataFrame (which works fine).

I have created a dummy example below that I would expect to work given what I have read online/on the TensorFlow website.

!pip install tensorflow==2.0.0 > /dev/null

import numpy as np
import tensorflow as tf

features, target = np.random.rand(100, 30), np.random.randint(0, 2, 100)
dataset = tf.data.Dataset.from_tensor_slices((features, target))

model = tf.keras.Sequential([
    tf.keras.layers.Dense(30, activation='relu', input_shape=(30,)),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.5),

    tf.keras.layers.Dense(30, activation='relu'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.5),

    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(
    optimizer='adam',
    loss='binary_crossentropy',
    metrics=['accuracy']
)

model.fit(
    dataset, 
    epochs=10,
)

which returns the following error message

...

ValueError: Error when checking input: expected dense_input to have shape (30,) but got array with shape (1,)

Is there anything obviously wrong in the above? Why is TensorFlow grabbing an input with shape (1,)?

1 Answer 1

1

Try to use tf.data.Dataset.from_tensors instead tf.data.Dataset.from_tensor_slices

The difference explained here: https://stackoverflow.com/a/55370549/10418812

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.