Skip to content Skip to sidebar Skip to footer

Tensorflow Keras With Tf Dataset Input

I'm new to tensorflow keras and dataset. Can anyone help me understand why the following code doesn't work? import tensorflow as tf import tensorflow.keras as keras import numpy as

Solution 1:

To your original question as to why you're getting the error:

Errorwhen checking input: expected input_1 to have 2 dimensions, but got array with shape (32,)

The reason your code breaks is because you haven't applied the .batch() back to the dataset variable, like so:

dataset = dataset.batch(10)

You simply called dataset.batch().

This breaks because without the batch() the output tensors are not batched, i.e. you get shape (32,) instead of (1,32).

Solution 2:

You are missing defining an iterator which is the reason why there is an error.

data = np.random.random((1000,32))
labels = np.random.random((1000,10))
dataset = tf.data.Dataset.from_tensor_slices((data,labels))
dataset = dataset.batch(10).repeat()
inputs = Input(shape=(32,))  # Returns a placeholder tensor# A layer instance is callable on a tensor, and returns a tensor.
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# Instantiate the model given inputs and outputs.
model = keras.Model(inputs=inputs, outputs=predictions)

# The compile step specifies the training configuration.
model.compile(optimizer=tf.train.RMSPropOptimizer(0.001),
          loss='categorical_crossentropy',
          metrics=['accuracy'])

# Trains for 5 epochs
model.fit(dataset.make_one_shot_iterator(), epochs=5, steps_per_epoch=100)

Epoch 1/5 100/100 [==============================] - 1s 8ms/step - loss: 11.5787 - acc: 0.1010

Epoch 2/5 100/100 [==============================] - 0s 4ms/step - loss: 11.4846 - acc: 0.0990

Epoch 3/5 100/100 [==============================] - 0s 4ms/step - loss: 11.4690 - acc: 0.1270

Epoch 4/5 100/100 [==============================] - 0s 4ms/step - loss: 11.4611 - acc: 0.1300

Epoch 5/5 100/100 [==============================] - 0s 4ms/step - loss: 11.4546 - acc: 0.1360

This is the result on my system.

Post a Comment for "Tensorflow Keras With Tf Dataset Input"