I have trained a CNN model using Keras with the TensorFlow backend. After training the model. I am trying to get a subset of the output of layer n. I can access the layer n output by using:
Model.layers[n].output
which is
<tf.Tensor 'dense_2_1/Identity:0' shape=(None, 64) dtype=float32>
and I can get the subset continuous range of it by a command like this:
Model.layers[n].output[...,1:5]
Now, I am trying to subset the tensor considering only a few indexes out of the 64 (for instance 1,5,10)
Any Idea how can I do that?
Here is the code for the reference :
n = 15
sub_indexes = [1,5,10]
final_fmap_index = 10
penultimate_output = Model.layers[final_fmap_index].output
layer_input = Model.input
loss = Model.layers[n].output[...,sub_indexes]
grad_wrt_fmap = K.gradients(loss,penultimate_output)[0]
grad_wrt_fmap_fn = K.function([layer_input,K.learning_phase()],
[penultimate_output,grad_wrt_fmap])
which gives me this error:
TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got [1, 5, 10]