Error while calling eval() on Tensor variable in keras

*爱你&永不变心* 提交于 2019-12-08 09:33:21

问题


I am using keras and using a layer output for some modifications. Before, using the output ( a tensor variable ) I am converting it to numpy array and thus calling eval() on it, as below:

def convert_output(orig_output):
    conv_output = invoke_modifications(orig_output.eval(), 8)

The code fails with following error:

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

I intend to invoke a conversion function on the output of the previous layer). The conversion function takes the input as a tensor variable and do the computation. However, I also want to visualize the data and I need to compute bit_length on each element of it.

For eg If layer A gives an output Y1. This output is used by Lambda layer L1 and it invokes the conversion method. Sample code:

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

I used similar scheme for converting the layer weights using model.get_weights(). The get_weights() method returns numpy array list and thus could easily iterate on each element and calc the bit length. But while converting the output is an issue as the output is a tensor variable and when i call eval() on it, gives the error I mentioned in previous post. I hope I was able to clear my intentions.


回答1:


You don't need to call eval() at all, your conversion_method should be done using symbolic functions (the ones from keras.backend) and should be differentiable.

It won't work otherwise, the network won't be able to be trained with Keras/Theano.



来源:https://stackoverflow.com/questions/41719812/error-while-calling-eval-on-tensor-variable-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!