我正在使用keras并使用图层输出进行一些修改。之前,使用输出(张量变量)我将它转换为numpy数组,从而在其上调用eval(),如下所示:
def convert_output(orig_output):
conv_output = invoke_modifications(orig_output.eval(), 8)
代码失败并出现以下错误:
File "<ipython-input-11-df86946997d5>", line 1, in <module>
orig_output.eval()
File "C:\ENV\p34\lib\site-packages\theano-0.9.0.dev4-py3.4.egg\theano\gof\graph.py", line 516, in eval
self._fn_cache[inputs] = theano.function(inputs, self)
File "C:\ENV\p34\lib\site-packages\theano-0.9.0.dev4-py3.4.egg\theano\compile\function.py", line 326, in function
output_keys=output_keys)
File "C:\ENV\p34\lib\site-packages\theano-0.9.0.dev4-py3.4.egg\theano\compile\pfunc.py", line 486, in pfunc
output_keys=output_keys)
File "C:\ENV\p34\lib\site-packages\theano-0.9.0.dev4-py3.4.egg\theano\compile\function_module.py", line 1783, in orig_function
output_keys=output_keys).create(
File "C:\ENV\p34\lib\site-packages\theano-0.9.0.dev4-py3.4.egg\theano\compile\function_module.py", line 1437, in __init__
accept_inplace)
File "C:\ENV\p34\lib\site-packages\theano-0.9.0.dev4-py3.4.egg\theano\compile\function_module.py", line 176, in std_fgraph
update_mapping=update_mapping)
File "C:\ENV\p34\lib\site-packages\theano-0.9.0.dev4-py3.4.egg\theano\gof\fg.py", line 180, in __init__
self.__import_r__(output, reason="init")
File "C:\ENV\p34\lib\site-packages\theano-0.9.0.dev4-py3.4.egg\theano\gof\fg.py", line 351, in __import_r__
self.__import__(variable.owner, reason=reason)
File "C:\ENV\p34\lib\site-packages\theano-0.9.0.dev4-py3.4.egg\theano\gof\fg.py", line 396, in __import__
variable=r)
theano.gof.fg.MissingInputError: An input of the graph, used to compute InplaceDimShuffle{x,x,x,x}(keras_learning_phase), was not provided and not given a value.Use the Theano flag exception_verbosity='high',for more information on this error.
Backtrace when the variable is created:
File "C:\Users\kak7lr\AppData\Roaming\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\_pydev_bundle\pydev_monkey_qt.py", line 71, in patched_import
return original_import(name, *args, **kwargs)
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\ENV\p34\lib\site-packages\keras\backend\theano_backend.py", line 23, in <module>
_LEARNING_PHASE = T.scalar(dtype='uint8', name='keras_learning_phase') # 0 = test, 1 = train
我打算在前一层的输出上调用转换函数。转换函数将输入作为张量变量并进行计算。但是,我也希望可视化数据,我需要计算每个元素的bit_length。
例如,如果A层给出输出Y1。此输出由Lambda层L1使用,并调用转换方法。示例代码:
Y1 = layer A output
Lambda( lambda x: conversion_method(x))(Y1)
def conversion_method( input_tensor ):
# do the conversion and also calc bit length
calc_integer_bits( input_tensor )
def calc_integer_bits(X):
noib_list = list()
for pos, each in enumerate(X):
in_range = int(round(abs(each .max() - each .min())))
bit_length = in_range.bit_length()
noib_list.append(bit_length)
return noib_list
我使用类似的方案使用model.get_weights()转换图层权重。 get_weights()方法返回numpy数组列表,因此可以轻松地迭代每个元素并计算位长度。但是转换输出是一个问题,因为输出是一个张量变量,当我在它上面调用eval()时,给出了我在前一篇文章中提到的错误。我希望能够清除我的意图。
答案 0 :(得分:1)
您根本不需要调用eval(),您的conversion_method应该使用符号函数(来自keras.backend的函数)来完成,并且应该是可区分的。
否则无法正常工作,网络将无法接受Keras / Theano的培训。