简体   繁体   中英

What should be the input array shape for training models with Tensorflow

I was following tutorial steps for training a neural network with Tensorflow as given on https://www.tensorflow.org/alpha/tutorials/keras/overfit_and_underfit

while running the fitting function on the training data ValueError with problem with the input shape was encountered.

Model architechture:

NUM_WORDS = 10000
baseline_model = keras.Sequential([
    keras.layers.Dense(16, activation = 'relu',input_shape(NUM_WORDS,)),
    keras.layers.Dense(16, activation = 'relu'),
    keras.layers.Dense(1, activation = 'sigmoid')
])

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

在此输入图像描述

baseline_history = baseline_model.fit(train_data, 
                                      train_labels, 
                                      epochs=20, 
                                      batch_size=512, 
                                      validation_data=(test_data, test_labels), 
                                      verbose=2)

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) in 4 batch_size=512, 5 validation_data=(test_data, test_labels), ----> 6 verbose=2)

~/env_tensorflow2_alpha/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs) 816 batch_size=batch_size, 817 steps=validation_steps, --> 818 steps_name='validation_steps') 819 elif validation_split and 0. < validation_split < 1.: 820 if training_utils.has_symbolic_tensors(x):

~/env_tensorflow2_alpha/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset) 2594 feed_input_shapes,
2595 check_batch_axis=False, # Don't enforce the batch size. -> 2596 exception_prefix='input') 2597 2598 if y is not None:

~/env_tensorflow2_alpha/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix) 347 ': expected ' + names[i] + ' to have shape ' + 348 str(shape) + ' but got array with shape ' + --> 349 str(data_shape)) 350 return data 351

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

train_data and train_labels have the following shape:

print("Train data shape: ", train_data.shape)
print("Train label shape: ", train_labels.shape)

Train data shape: (25000, 10000) Train label shape: (25000,)

Why am I getting an error here, and do I need to accommodate another dimension for the batch_size in the input array?

I am working with Tensorflow version: 2.0.0-alpha0 Thanks in advance!!

The code looks perfectly fine to me except for a small bug in the line,

keras.layers.Dense(16, activation = 'relu',input_shape(NUM_WORDS,))

where you missed a = so it should have been,

keras.layers.Dense(16, activation = 'relu',input_shape=(NUM_WORDS,))

I was able to run the same code in google colab space with no error.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM