deep-learning-models icon indicating copy to clipboard operation
deep-learning-models copied to clipboard

How to use model.summary() when using placeholder instead of Input(keras)

Open ymcasky opened this issue 7 years ago • 4 comments

Dear all, I follow post in "https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html" The little modified code I use is:

import tensorflow as tf from tensorflow.python.keras.layers import Dense from tensorflow.python.keras.backend import categorical_crossentropy from tensorflow.examples.tutorials.mnist import input_data from tensorflow.python.keras.models import Model

sess = tf.Session() img = tf.placeholder(tf.float32, shape=(None, 784)) x = Dense(128, activation='relu')(img) # fully-connected layer with 128 units and ReLU activation x = Dense(128, activation='relu')(x) preds = Dense(10, activation='softmax')(x) # output layer with 10 units and a softmax activation

labels = tf.placeholder(tf.float32, shape=(None, 10)) loss = tf.reduce_mean(categorical_crossentropy(labels, preds)) mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

init_op = tf.global_variables_initializer() sess.run(init_op) with sess.as_default(): for i in range(100): batch = mnist_data.train.next_batch(50) train_step.run(feed_dict={img: batch[0], labels: batch[1]})


It work fine until I use model.summary :

model = Model(inputs=img, outputs=preds)

The error message show" Input tensors to a Model must come from tf.layers.Input" I can use tf.layers.Input to solve this problem. But I really want to use tf.placeholder so I can feed data as I like. Can anyone help me? Thanks!!

ymcasky avatar Jan 31 '18 10:01 ymcasky

I solve my problem!! Just use inputs = Input(tensor=img) everything will be fine : )

ymcasky avatar Feb 01 '18 01:02 ymcasky

I have one more question, the code I use is:

import tensorflow as tf from tensorflow.python.keras.layers import Dense from tensorflow.python.keras.backend import categorical_crossentropy from tensorflow.examples.tutorials.mnist import input_data from tensorflow.python.keras.models import Model from tensorflow.python.keras.layers import Input mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True) img_size_flat = 28*28 img = tf.placeholder(tf.float32, shape=(None, 784)) inputs = Input(tensor=img) # tf_input #inputs = Input(shape=(img_size_flat,)) #keras_input x = Dense(128, activation='relu')(inputs) # fully-connected layer with 128 units and ReLU activation x = Dense(128, activation='relu')(x) preds = Dense(10, activation='softmax')(x) # output layer with 10 units and a softmax activation model = Model(inputs=inputs, outputs=preds) #%% model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(x=mnist_data.train.images, y=mnist_data.train.labels, epochs=1, batch_size=128)

The error message shows ValueError: ('Error when checking model input: expected no data, but got:', array([[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], ..., [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32)) But when I commnet # tf_input and uncomment # keras_input It work fine. How can I use #tf_input for this?

ymcasky avatar Feb 01 '18 02:02 ymcasky

same issue with me

vardhanshah avatar Mar 30 '19 16:03 vardhanshah

The problem can be explained in this way:

When you use keras/tf.keras, at each step, the model expects to see a layer rather than a tensor. In fact, even you defined the placeholder; however in model.fit step, this placeholder is not used at all. That is why passing a keras layer works fine but a tensor fails.

bastia0321 avatar Apr 10 '19 21:04 bastia0321