我已经为这个问题困扰了几天。这里是我的简单代码:我试图使用lambda层将3D张量采样成2D张量,但每次都从tensorflow后端得到错误。以下是我简化后的代码,错误发生在这一点上:
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):
# 帮助函数,从概率数组中采样一个索引
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__':
# 模型定义
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()
# 测试输入
noise = K.random_uniform(shape=(2, 15, 38), maxval=1, minval=0, dtype="float32", seed=42)
print(model.predict_on_batch(noise))
这是错误的输出
2017-11-15 11:44:58.285872: I tensorflow/core/platform/cpu_feature_guard.cc:137] 您的CPU支持的指令未在编译此TensorFlow二进制文件时使用: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: 您必须为占位符张量 'input_1' 提供值,其数据类型为float,形状为[?,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): 您必须为占位符张量 'input_1' 提供值,其数据类型为float,形状为[?,15,38]
[[Node: input_1 = Placeholder[dtype=DT_FLOAT, shape=[?,15,38], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
如果我像这样向输入层提供一个张量:
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))
模型可以工作,但使用我给输入层提供的零张量,返回一个全为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.]
如果我尝试使用K.placeholder对象,也会得到同样的错误。我该如何解决这个问题?我已经尝试将学习阶段设置为0或1,但没有任何变化。
回答:
解决方案非常简单:在张量操作过程中不要使用numpy,只使用Keras后端函数,而且显然我没有理解lambda层的运作方式。如果其他人也遇到了这个问题,以下是修正后的代码:
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
########### 模型
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("噪声输入")
print(noise)
decoded = model.predict_on_batch(noise)
print("\n\n采样输出")
print(decoded)
#################
以下是预期的输出
使用TensorFlow后端。
2017-11-16 18:26:51.398569: I tensorflow/core/platform/cpu_feature_guard.cc:137] 您的CPU支持的指令未在编译此TensorFlow二进制文件时使用: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
_________________________________________________________________
噪声输入
[[[ 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]] ...