2016-05-14 18 views
5

kerasモデルをtheano関数に変えて、入力のグラデーションを計算できるようにしたいとします。私はこれがネットワークを視覚化するのにクールだと思った。私は、これらのグラデーションを使って、ニューラルネットワークが考えるものに基づいて元の画像の特徴を強化したいと考えています。私は次のコードで間違っていることを理解していません。kerasモデル全体をtheano関数に変換する方法

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) 

以下の例外があります。

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 "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed 
    File "/usr/local/lib/python3.5/dist-packages/keras/backend/__init__.py", line 51, in <module> 
    from .theano_backend import * 
    File "<frozen importlib._bootstrap>", line 969, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 673, in _load_unlocked 
    File "<frozen importlib._bootstrap_external>", line 662, in exec_module 
    File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed 
    File "/usr/local/lib/python3.5/dist-packages/keras/backend/theano_backend.py", line 13, in <module> 
    _LEARNING_PHASE = T.scalar(dtype='uint8', name='keras_learning_phase') # 0 = test, 1 = train 

答えて

2

FAQに続いて、試してみてください。Kerasの最新バージョン(1.0)については

from keras import backend as K 
get_last_layer_output = K.function([model.layers[0].input], 
            [model.layers[-1].output]) 

、例えば( "古い" kerasについて0.3.xを

from keras import backend as K 
get_last_layer_output = K.function([model.layers[0].input], 
            [model.layers[-1].get_output(train=False)]) 
0

を使用):

私はこのバージョンを使用しませんが、this oneのような例を使用してください。 "新しい" kerasについては

(1.0+):(。トレーニングのためのテスト、1 0)

あなたがDropout層を使用しているので、あなたは別の入力K.learning_phase()を追加し、値0を与える必要があります

コード:

from keras import backend as K 
K.function([model.layers[0].input, K.learning_phase()], [model.layers[-1].output]) 

参考:keras FAQ

関連する問題