我可以获取所有输出的keras图层吗

时间:2019-11-12 10:01:44

标签: tensorflow keras deep-learning google-colaboratory keras-layer

我刚开始进行深度学习,我想实时获取每一层的输入/输出。我正在使用带有tensorflow 2和python 3的google colab。我试图获取像这样的图层,但是由于某种原因,我不明白这是行不通的。任何帮助将不胜感激。

# Here are imports 

from __future__ import absolute_import, division, print_function, unicode_literals

try:
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:
  pass
import tensorflow as tf

from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt


from tensorflow.keras import backend as K



# I am using CIFAR10 dataset

(train_images, train_labels), (test_images, test_labels) = 
datasets.cifar10.load_data()

Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0

# Here is the model

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

# Compilation of the model 

model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                validation_data=(test_images, test_labels))
# Based on 
https://stackoverflow.com/questions/41711190/keras-how-to-get-the-output-of-each-layer

# I tried this 

tf.compat.v1.disable_eager_execution()
inp = model.input                                    # input placeholder
outputs = [layer.output for layer in model.layers]     # all layer outputs
functors = [K.function([inp, K.learning_phase()], [out]) for out in outputs]    # evaluation functions

Testing
test = np.random.random(input_shape)[np.newaxis,...]
layer_outs = [func([test, 1.]) for func in functors]
print(layer_outs)


#The error appear at line 
functors = [K.function([inp, K.learning_phase()], [out]) for out in outputs] 


#I got this error message
Tensor Tensor("conv2d/Identity:0", shape=(None, 30, 30, 32), dtype=float32) is not an element of this graph.

1 个答案:

答案 0 :(得分:1)

此错误基本上告诉您要在编译后更改图形。调用compile时,TF将静态定义所有操作。您必须将定义functors的代码片段移到compile方法上方。只需将最后几行替换为这些行:

tf.compat.v1.disable_eager_execution()
inp = model.input                                    # input placeholder
outputs = [layer.output for layer in model.layers]     # all layer outputs
functors = [K.function([inp, K.learning_phase()], [out]) for out in outputs]    # evaluation functions



model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=1, 
                validation_data=(test_images, test_labels))

#Testing
input_shape = [1] + list(model.input_shape[1:])
test = np.random.random(input_shape)
layer_outs = [func([test, 1.]) for func in functors]
print(layer_outs)