说您有布尔tf.placeholder
,并且您想在调用Model.fit
时使用它。你会怎么做?下面是一些可运行的伪代码,用于说明问题。
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input, Flatten
from tensorflow.keras.models import Model
# A boolean value that should have some effect of something
do_stuff = tf.placeholder(tf.bool)
# If do_stuff is true, return tf.ones else tf.zeros, and a 1 or 0 label
if_dostuff = lambda: [tf.ones((5, 5)), tf.constant(1)]
if_not_dostuff = lambda: [tf.zeros((5, 5)), tf.constant(0)]
X, Y_true = tf.cond(do_stuff, if_dostuff, if_not_dostuff)
# Make some dummy labels
# Do some random model operation
X_input = Input(shape=(5, 5))
layer_mod = Flatten()(X_input)
layer_mod = Dense(1)(layer_mod)
out_model = Model(inputs=[X_input], outputs=[layer_mod])
# Compile model
out_model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.metrics.binary_crossentropy
)
### Other ops with other models and summaries etc. ###
out_model.fit(...) # What do I do at this point?
请记住,布尔值只是为了使事情保持简单。实际上,我有一些需要迭代的字符串作为迭代器句柄(基于我要训练的数据集)。
如何使用这种布局来制作keras令人惊叹的model.fit
界面?
我可以在this question
中询问