Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly feed specific tensor to keras model

To allow using Keras model as part of standard tensorflow operations, I create a model using specific placeholder for the input.

However, when trying to do model.predict, I get an error:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [100,84,84,4]
 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[100,84,84,4], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

My code is given below:

from keras.layers import Convolution2D, Dense, Input
from keras.models import Model
from keras.optimizers import Nadam
from keras.losses import mean_absolute_error
from keras.activations import relu
import tensorflow as tf
import numpy as np
import gym

state_size = [100, 84, 84, 4]

input_tensor = tf.placeholder(dtype=tf.float32, shape=state_size)

inputL = Input(tensor=input_tensor)
h1 = Convolution2D(filters=32, kernel_size=(5,5), strides=(4,4), activation=relu) (inputL)
h2 = Convolution2D(filters=64, kernel_size=(3,3), strides=(2,2), activation=relu) (h1)
h3 = Convolution2D(filters=64, kernel_size=(3,3), activation=relu) (h2)
h4 = Dense(512, activation=relu) (h3)
out = Dense(18) (h4)

model = Model(inputL, out)

opt = Nadam()


disc_rate=0.99

sess = tf.Session()
dummy_input = np.ones(shape=state_size)

model.compile(opt, mean_absolute_error)

writer = tf.summary.FileWriter('./my_graph', sess.graph)
writer.close()

print(out)

print(model.predict({input_tensor: dummy_input}))

I have also trying feeding the input directly(no dictionary, just the value) - same exception. I can, however, get the model to work like:

print(sess.run( model.output, {input_tensor: dummy_input }))

Is there a way for me to still use normal Keras .predict method?

like image 453
ikamen Avatar asked Nov 21 '17 19:11

ikamen


People also ask

Does Keras use tensor?

Used in the notebooksA Keras tensor is a symbolic tensor-like object, which we augment with certain attributes that allow us to build a Keras model just by knowing the inputs and outputs of the model.

What is verbose in model fit?

verbose = 2, one line per epoch i.e. epoch no./total no. of epochs. Follow this answer to receive notifications.

What is model fit ()?

What is Model Fitting? Model fitting is a measure of how well a machine learning model generalizes to similar data to that on which it was trained. A model that is well-fitted produces more accurate outcomes. A model that is overfitted matches the data too closely.


1 Answers

The following works (we need to initialize global variables):

sess.run(tf.global_variables_initializer()) # initialize 
print(sess.run([model.output], feed_dict={input_tensor: dummy_input}))
like image 121
Sandipan Dey Avatar answered Sep 20 '22 23:09

Sandipan Dey