问题描述
下面的示例在2.2中工作; K.function
在2.3中发生了显着变化,现在在Eager执行中建立一个Model
,所以我们要传递Model(inputs=[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,...])
.
我确实有一个解决方法,但是它有点黑,并且比K.function
复杂得多;如果没有一种方法可以显示出简单的方法,我将发布我的方法.
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).
推荐答案
对于以渴望模式获取中间层的输出,无需构建K.function
并使用学习阶段.相反,我们可以构建一个模型来实现这一目标:
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
或者,如果您坚持使用K.function
并希望在急切模式下切换学习阶段,则可以使用tensorflow.python.keras.backend
中的eager_learning_phase_scope
(请注意,此模块是tensorflow.keras.backend
的超集,并且包含内部函数,例如上述内容,可能会在以后的版本中进行更改):
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])
这篇关于如何使用learning_phase在TF 2.3 Eager中获得中间输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!