2017-01-18 41 views
0

我正在使用keras並使用圖層輸出進行一些修改。之前,使用我將其轉換爲numpy的陣列的輸出(一個張量變量),並且因此在其上調用的eval(),如下:在keras的張量變量上調用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()時,會給出我在前一篇文章中提到的錯誤。我希望我能夠清除我的意圖。

回答

1

根本不需要調用eval(),您的conversion_method應該使用符號函數(來自keras.backend的函數)來完成,並且應該是可區分的。

否則無法工作,網絡將無法接受Keras/Theano培訓。

+0

感謝您的回覆。我不完全確定如何獲得這張張變量裏面的元素的位長。如果可能的話,你能舉一個例子嗎? – blackbug

+0

@blackbug然後您應該提供calc_bit_lenth方法,以便我們可以找到它的符號表示形式。 –

+0

我已經用上述方法更新了問題。其實很簡單。 – blackbug