I have a Keras functional model defined as:
# Construct DNN
spec_input = keras.layers.Input(shape=(1, ctx, fft), name='spec')
x = keras.layers.Flatten(data_format)(spec_input)
for layer in range(len(args.dnn_struct)):
x = Dense(args.dnn_struct[layer])(x)
x = BatchNormalization()(x)
x = keras.layers.ReLU()(x)
out = Dense(fft, activation="sigmoid", name=f'spp')(x)
model = Model(inputs=spec_input, outputs=out)
I would like to get the output of each layer in the model for a given input and the answers given in Keras, How to get the output of each layer? do not work for a functional model. I am currently using Tensorflow 1.14
When I try using
from keras import backend as K
inp = model.input # input placeholder
outputs = [layer.output for layer in model.layers] # all layer outputs
functor = K.function([inp, K.learning_phase()], outputs ) # evaluation function
# Testing
test = np.random.random(input_shape)[np.newaxis,...]
layer_outs = functor([test, 1.])
print(layer_outs)
I get the error
Traceback (most recent call last):
File "/home/xyz/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1446, in __init__
session._session, options_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: spec:0 is both fed and fetched.