Keras InvalidArgumentError: You must feed a value for placeholder tensor

Viewed 2677

I'm stuck on this problem since days. here's my simple code: I'm trying to sample a 3D tensor into a 2D tensor with a lambda layer but every time i get an error from tensorflow backend. here's my code, reduced to the point where the error occours:

import numpy as np
import tensorflow as tf
from keras import backend as K
from keras.layers import Lambda, Input
from keras.models import Model

def sampling(x):
    def get_sample(preds, temperature=1.0):
        # helper function to sample an index from a probability array
        preds = K.log(preds) / temperature
        exp_preds = K.exp(preds)
        preds = exp_preds / K.sum(exp_preds)
        probas = np.random.multinomial(1, K.eval(preds), 1)
        return K.expand_dims(K.variable(np.argmax(probas)))

    b = K.int_shape(x)[1]
    for i in range(1):
        result = get_sample(x[i, 0, :])
        for j in range(b):
            if j == 0:
                continue
            c = get_sample(x[i, j, :])
            result = K.concatenate([result, c], axis=0)

    return result


if __name__ == '__main__':
    # model definition
    inp = Input(shape=(15, 38))
    exa = Lambda(lambda x: sampling(x), input_shape=(15, 38), output_shape=(15,))(inp)
    model = Model(inputs=inp, outputs=exa)
    model.summary()

    # test input
    noise = K.random_uniform(shape=(2, 15, 38), maxval=1, minval=0, dtype="float32", seed=42)

    print(model.predict_on_batch(noise))

here's the error dump

2017-11-15 11:44:58.285872: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
Traceback (most recent call last):
  File "/home/archeffect/PycharmProjects/adversarial_DGA/sample_layer.py", line 37, in <module>
    exa = Lambda(lambda x: sampling(x), input_shape=(15, 38), output_shape=(15,))(inp)
  File "/usr/lib/python2.7/site-packages/keras/engine/topology.py", line 603, in __call__
    output = self.call(inputs, **kwargs)
  File "/usr/lib/python2.7/site-packages/keras/layers/core.py", line 651, in call
    return self.function(inputs, **arguments)
  File "/home/archeffect/PycharmProjects/adversarial_DGA/sample_layer.py", line 37, in <lambda>
    exa = Lambda(lambda x: sampling(x), input_shape=(15, 38), output_shape=(15,))(inp)
  File "/home/archeffect/PycharmProjects/adversarial_DGA/sample_layer.py", line 24, in sampling
    result = get_sample(x[i, 0, :])
  File "/home/archeffect/PycharmProjects/adversarial_DGA/sample_layer.py", line 19, in get_sample
    probas = np.random.multinomial(1, K.eval(preds), 1)
  File "/usr/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 644, in eval
    return to_dense(x).eval(session=get_session())
  File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 570, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 4455, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 889, in run
    run_metadata_ptr)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1120, in _run
    feed_dict_tensor, options, run_metadata)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1317, in _do_run
    options, run_metadata)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1336, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,15,38]
     [[Node: input_1 = Placeholder[dtype=DT_FLOAT, shape=[?,15,38], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Caused by op u'input_1', defined at:
  File "/home/archeffect/PycharmProjects/adversarial_DGA/sample_layer.py", line 36, in <module>
    inp = Input(shape=(15, 38))
  File "/usr/lib/python2.7/site-packages/keras/engine/topology.py", line 1439, in Input
    input_tensor=tensor)
  File "/usr/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/keras/engine/topology.py", line 1348, in __init__
    name=self.name)
  File "/usr/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 488, in placeholder
    x = tf.placeholder(dtype, shape=shape, name=name)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1599, in placeholder
    return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3091, in _placeholder
    "Placeholder", dtype=dtype, shape=shape, name=name)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
    op_def=op_def)
  File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,15,38]
     [[Node: input_1 = Placeholder[dtype=DT_FLOAT, shape=[?,15,38], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

if I feed a tensor to the input layer like this:

if __name__ == '__main__':
    tens = K.zeros(shape=(2, 15, 38), dtype="float32")
    inp = Input(shape=(15, 38), tensor=tens)
    exa = Lambda(lambda x: sampling(x), input_shape=(15, 38), output_shape=(15,))(inp)
    model = Model(inputs=inp, outputs=exa)
    model.summary()

    noise = K.random_uniform(shape=(2, 15, 38), maxval=1, minval=0, dtype="float32", seed=42)

    print(model.predict_on_batch(noise))

I the model works but using the zeroes tensor I gave to the input layer, returning an array of 1.

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 15, 38)            0         
_________________________________________________________________
lambda_1 (Lambda)            (None, 15)                0         
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]

If I try to put a K.placeholder object I get the same error as well. How can I resolve this? I already tried setting the learning phase to 0 or 1 but nothing changes.

1 Answers

The solution was very simple: do not use numpy during tensor manipulation, only Keras backend functions and clearly I did not understand how lambda layers work. here's the corrected code if anyone else get stuck by this problem.

import numpy as np
from keras import backend as K
from keras.layers import Input
from keras.layers import Lambda
from keras.models import Model


def sampling(preds, temperature=1.0):
    preds = K.log(preds) / temperature
    exp_preds = K.exp(preds)
    preds = exp_preds / K.sum(exp_preds)
    return K.argmax(preds, axis=2)


if __name__ == '__main__':
    batch_size = 10

    ########### MODEL
    inp = Input(shape=(15, 38))
    exa = Lambda(lambda x: sampling(x), output_shape=(15,))(inp)
    model = Model(inputs=inp, outputs=exa)
    model.summary()

    noise = np.random.uniform(0, 1, size=(batch_size, 15, 38))
    print("NOISE INPUT")
    print(noise)
    decoded = model.predict_on_batch(noise)
    print("\n\nSAMPLED OUTPUT")
    print(decoded)
    #################

and here's the expected output

Using TensorFlow backend.
2017-11-16 18:26:51.398569: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 15, 38)            0         
_________________________________________________________________
lambda_1 (Lambda)            (None, 15)                0         
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
NOISE INPUT
[[[ 0.30891316  0.15221787  0.5038829  ...,  0.8848083   0.08956024
    0.3606536 ]
  [ 0.44807492  0.23133616  0.70644087 ...,  0.27361268  0.65620316
    0.85804126]
  [ 0.72102694  0.09523426  0.16791966 ...,  0.66441456  0.80056542
    0.86870569]
  ..., 
  [ 0.98217747  0.77091951  0.59332161 ...,  0.79585449  0.73915857
    0.46059018]
  [ 0.88017517  0.17193309  0.51066406 ...,  0.78946729  0.88111187
    0.26728708]
  [ 0.34269771  0.62430603  0.84418251 ...,  0.80441201  0.54334108
    0.98493448]]

 [[ 0.8669257   0.82135396  0.10069927 ...,  0.90542972  0.67465482
    0.55842347]
  [ 0.72956178  0.08731971  0.95467123 ...,  0.95472095  0.88684042
    0.37218502]
  [ 0.46721374  0.96649476  0.1933913  ...,  0.06071162  0.14450657
    0.90962683]
  ..., 
  [ 0.12344553  0.96051246  0.68679955 ...,  0.99269888  0.20232171
    0.67095509]
  [ 0.68686574  0.94280854  0.80347876 ...,  0.04743072  0.95419244
    0.85568141]
  [ 0.38312468  0.51157601  0.71631078 ...,  0.26982261  0.94738435
    0.01725109]]

 [[ 0.60993944  0.78323704  0.44851841 ...,  0.88221101  0.32755589
    0.82010709]
  [ 0.83341658  0.55235978  0.03581224 ...,  0.03711514  0.82935275
    0.1729678 ]
  [ 0.78978104  0.2181397   0.49759489 ...,  0.20598122  0.87555217
    0.1310053 ]
  ..., 
  [ 0.0136351   0.1470119   0.55496631 ...,  0.93888501  0.29122596
    0.73619966]
  [ 0.15711732  0.99471253  0.35647437 ...,  0.90060837  0.75228682
    0.3414452 ]
  [ 0.35007325  0.54940108  0.60591077 ...,  0.90813329  0.04030722
    0.09562064]]

 ..., 
 [[ 0.02168732  0.95698804  0.09280446 ...,  0.71586791  0.00920231
    0.38907889]
  [ 0.76853587  0.25528251  0.61563489 ...,  0.97880311  0.88628481
    0.30254836]
  [ 0.75778522  0.27014167  0.86857289 ...,  0.7196309   0.47614798
    0.50452127]
  ..., 
  [ 0.38104386  0.95507951  0.8337219  ...,  0.86317049  0.56698557
    0.67915409]
  [ 0.2014119   0.18607705  0.11220879 ...,  0.11445029  0.54958169
    0.332358  ]
  [ 0.26280992  0.73237823  0.72684251 ...,  0.43947476  0.56091593
    0.32524556]]

 [[ 0.96829533  0.68190408  0.3739879  ...,  0.9606717   0.22280759
    0.3031305 ]
  [ 0.82955492  0.04815685  0.43250177 ...,  0.58623864  0.37036309
    0.23000677]
  [ 0.47910544  0.44406259  0.22996476 ...,  0.05613431  0.77214874
    0.95063867]
  ..., 
  [ 0.60491445  0.33447544  0.22093127 ...,  0.82935465  0.08349322
    0.2298346 ]
  [ 0.81513182  0.27917361  0.34890291 ...,  0.4860525   0.72408671
    0.91163499]
  [ 0.87716181  0.90365287  0.12285839 ...,  0.9010089   0.05510581
    0.61629317]]

 [[ 0.80663206  0.35334483  0.81901437 ...,  0.55569487  0.15872563
    0.71420715]
  [ 0.78043711  0.37595655  0.7021172  ...,  0.57781578  0.68621989
    0.0617801 ]
  [ 0.74502178  0.51097854  0.35836054 ...,  0.39165413  0.63403207
    0.1674981 ]
  ..., 
  [ 0.87651597  0.34229628  0.99998675 ...,  0.64886858  0.87177124
    0.83341321]
  [ 0.037897    0.00723697  0.05077544 ...,  0.54169736  0.18409638
    0.1442172 ]
  [ 0.10817155  0.82998391  0.1356064  ...,  0.89285585  0.7613262
    0.33972169]]]


SAMPLED OUTPUT
[[18 12 30  3 15  9 28 36 29 14 15  5 33  4 28]
 [ 3 35  5 10 23 16 10 11  3 15  6  1 29 30 23]
 [27 17 11 32 14 19  4 20  7 20 29 19 29  1 26]
 [ 3 27  5 11 21 21 11 27  0 21 21 34 15 24  0]
 [ 5  5 21  0  1 10 21  5 19 19 13 25 37 28 33]
 [ 7 34 11  3 18 24 18 32 11 21  5 32 25  0 16]
 [ 0 25 29 21 15 15 35 23  0 18  6 27  4 11 21]
 [27 35 29 10 11 11 18  6 29 35 36 29 34 21 33]
 [ 0 29  8 11 30 21 32 27 15 10 16 15 13 34 27]
 [17 26  3 20 10 13 27 35  4 10 10 20  2 15 34]]
Related