Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

python - How to get intermediate outputs in TF 2.3 Eager with learning_phase?

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).
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...