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.