java调用keras theano模型,如何将整个keras模型转换为theano函数

I want to turn my keras model into a theano function so that I can compute the gradients on the inputs. I thought this might be cool for visualizing the network. I want to use these gradients to enhance features in the original image based on what the neural network thinks they are. I do not understand what I am doing wrong with the following code.

model = Sequential()

model.add(InputLayer((3, H, W)))

model.add(GaussianNoise(0.03))

model.add(Flatten())

model.add(Dense(512, activation = 'relu', name = 'dense'))

model.add(Dropout(0.2))

model.add(Dense(20, activation = 'relu'))

model.add(Dense(C, activation = 'softmax', W_regularizer = l2()))

...

f = theano.function([model.input], model.output)

I get the following exception.

theano.gof.fg.MissingInputError: A variable that is an input to the graph was neither provided as an input to the function nor given a value. A chain of variables leading from this input to an output is [keras_learning_phase, DimShuffle{x,x}.0, Elemwise{switch,no_inplace}.0, dot.0, Elemwise{add,no_inplace}.0, Elemwise{add,no_inplace}.0, Elemwise{mul,no_inplace}.0, dot.0, Elemwise{add,no_inplace}.0, Softmax.0]. This chain may not be unique

Backtrace when the variable is created:

File "", line 222, in _call_with_frames_removed

File "/usr/local/lib/python3.5/dist-packages/keras/backend/__init__.py", line 51, in

from .theano_backend import *

File "", line 969, in _find_and_load

File "", line 958, in _find_and_load_unlocked

File "", line 673, in _load_unlocked

File "", line 662, in exec_module

File "", line 222, in _call_with_frames_removed

File "/usr/local/lib/python3.5/dist-packages/keras/backend/theano_backend.py", line 13, in

_LEARNING_PHASE = T.scalar(dtype='uint8', name='keras_learning_phase') # 0 = test, 1 = train

解决方案

Following the FAQ, try:

from keras import backend as K

get_last_layer_output = K.function([model.layers[0].input],

[model.layers[-1].output])

For the most recent version of Keras (1.0), use

from keras import backend as K

get_last_layer_output = K.function([model.layers[0].input],

[model.layers[-1].get_output(train=False)])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值