1

我正在使用 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() 时,会给出我在上一篇文章中提到的错误。我希望我能够明确我的意图。

4

1 回答 1

1

您根本不需要调用 eval(),您的 conversion_method 应该使用符号函数(来自 keras.backend 的函数)完成并且应该是可微的。

否则它将无法工作,网络将无法使用 Keras/Theano 进行训练。

于 2017-01-18T18:36:44.700 回答