InvalidArgumentError: input_1:0 已输入和提取
Posted
技术标签:
【中文标题】InvalidArgumentError: input_1:0 已输入和提取【英文标题】:InvalidArgumentError: input_1:0 is both fed and fetched 【发布时间】:2019-03-07 11:40:38 【问题描述】:我在 GitHub 上找到了关于 print activation maps 的示例。代码非常简单。我所做的只是复制粘贴该功能。
def get_activations(model, model_inputs, print_shape_only=False, layer_name=None):
print('----- activations -----')
activations = []
inp = model.input
model_multi_inputs_cond = True
if not isinstance(inp, list):
# only one input! let's wrap it in a list.
inp = [inp]
model_multi_inputs_cond = False
outputs = [layer.output for layer in model.layers if
layer.name == layer_name or layer_name is None] # all layer outputs
funcs = [K.function(inp + [K.learning_phase()], [out]) for out in outputs] # evaluation functions
if model_multi_inputs_cond:
list_inputs = []
list_inputs.extend(model_inputs)
list_inputs.append(0.)
else:
list_inputs = [model_inputs, 0.]
print list_inputs
layer_outputs = [func(list_inputs)[0] for func in funcs]
for layer_activations in layer_outputs:
activations.append(layer_activations)
if print_shape_only:
print(layer_activations.shape)
else:
print(layer_activations)
return activations
然后我传递了我的模型和输入。但是,它会产生此错误
Traceback (most recent call last):
File "test_cnn_128.py", line 80, in <module>
get_activations(model, test_x)
File "test_cnn_128.py", line 45, in get_activations
layer_outputs = [func(list_inputs)[0] for func in funcs]
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2666, in __call__
return self._call(inputs)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2635, in _call
session)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2587, in _make_callable
callable_fn = session._make_callable_from_options(callable_opts)
File "/home/fatima/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1414, in _make_callable_from_options
return BaseSession._Callable(self, callable_options)
File "/home/fatima/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1368, in __init__
session._session, options_ptr, status)
File "/home/fatima/.local/lib/python2.7/site-packages/tensorflow/python/framework/errors_impl.py", line 519, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: input_1:0 is both fed and fetched.
Exception tensorflow.python.framework.errors_impl.InvalidArgumentError: InvalidArgumentError() in <bound method _Callable.__del__ of <tensorflow.python.client.session._Callable object at 0x7f44de0cd210>> ignored
我不确定如何解决这个问题。
【问题讨论】:
你用的是什么 tensorflow 版本? @mnis 版本是 1.10.1 我认为问题可能在于您的第一层是代表输入的Input
层,您试图在同时提供它的同时提取它的值,这是 TensorFlow 不允许的(如果是这种情况,我不确定为什么它在引用的代码中有效,也许它们不是以Input
?...) 开头的。在outputs
的定义中,改用outputs = [layer.output for layer in model.layers[1:] if <etc...>]
,即获取除第一个以外的所有层输出,看看是否可行并产生所有隐藏层和输出层激活。
还是同样的错误@jdehesa
@NerdGiraffe 这很有趣。也许您可以尝试在循环中生成值,例如for func, layer in zip(funcs, model.layers):
,而不是列表理解,并在调用每个func
之前打印layer
,所以至少您知道是否可以评估以及失败的地方。
【参考方案1】:
正如我在线程 Keras, How to get the output of each layer? 上发布的那样,解决此问题的方法是替换该行
outputs = [
layer.output
for layer in model.layers
if layer.name == layer_name or layer_name is None
]
与
outputs = [
layer.output
for layer in model.layers
if layer.name == layer_name or layer_name is None
][1:]
...为了跳过输入层。
【讨论】:
【参考方案2】:从您的输出/获取层中排除输入层(这就是错误显示“fed & fetch”的原因 - 无法从输入层(提要)本身获取输入(提要)的输出(获取))
outputs = [layer.output for layer in model.layers if
layer.name == layer_name or layer_name is None][1:]
或者完全删除图层名称;
outputs = [layer.output for layer in model.layers][1:]
应该解决问题(注意 --> [1:]
在最后,这是解决问题的简单方法)
【讨论】:
以上是关于InvalidArgumentError: input_1:0 已输入和提取的主要内容,如果未能解决你的问题,请参考以下文章
InvalidArgumentError:输入必须是向量,得到形状:[]
Tensorflow 中带有 model.fit 的 InvalidArgumentError
InvalidArgumentError: input_1:0 已输入和提取
InvalidArgumentError:预期维度在 [-1, 1) 范围内,但得到 1
InvalidArgumentError:不兼容的形状:[29] 与 [29,7,7,2]
InvalidArgumentError: 预期 'tf.Tensor(False, shape=(), dtype=bool)' 为真