问题
Example below works in 2.2; K.function
is changed significantly in 2.3, now building a Model
in Eager execution, so we're passing Model(inputs=[learning_phase,...])
.
I do have a workaround in mind, but it's hackish, and lot more complex than K.function
; if none can show a simple approach, I'll post mine.
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.python.keras import backend as K
import numpy as np
ipt = Input((16,))
x = Dense(16)(ipt)
out = Dense(16)(x)
model = Model(ipt, out)
model.compile('sgd', 'mse')
outs_fn = K.function([model.input, K.symbolic_learning_phase()],
[model.layers[1].output]) # error
x = np.random.randn(32, 16)
print(outs_fn([x, True]))
>>> ValueError: Input tensors to a Functional must come from `tf.keras.Input`.
Received: Tensor("keras_learning_phase:0", shape=(), dtype=bool)
(missing previous layer metadata).
回答1:
For fetching output of an intermediate layer in eager mode it's not necessary to build a K.function
and use learning phase. Instead, we can build a model to achieve that:
partial_model = Model(model.inputs, model.layers[1].output)
x = np.random.rand(...)
output_train = partial_model([x], training=True) # runs the model in training mode
output_test = partial_model([x], training=False) # runs the model in test mode
Alternatively, if you insist on using K.function
and want to toggle learning phase in eager mode, you can use eager_learning_phase_scope
from tensorflow.python.keras.backend
(note that this module is a superset of tensorflow.keras.backend
and contains internal functions, such as the mentioned one, which may change in future versions):
from tensorflow.python.keras.backend import eager_learning_phase_scope
fn = K.function([model.input], [model.layers[1].output])
# run in test mode, i.e. 0 means test
with eager_learning_phase_scope(value=0):
output_test = fn([x])
# run in training mode, i.e. 1 means training
with eager_learning_phase_scope(value=1):
output_train = fn([x])
来源:https://stackoverflow.com/questions/63238203/how-to-get-intermediate-outputs-in-tf-2-3-eager-with-learning-phase