0

Say you have e.g. a boolean tf.placeholder, and you want to feed it when you call Model.fit. How would you do it? Below is some runnable dummy code that illustrates the problem.

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?

Keep in mind that the boolean is just to keep things simple. In reality I have strings that are iterator handles that needs to be fed (based on what dataset I want to train).

How can I do keras' amazing model.fit interface with this sort of layout?

An alternative would be what I ask in this question

  • Where do you want to use your boolean? Is it in the loss? – Anakin Apr 15 '19 at 11:35
  • @Anakin Please read the text just below the code :) Also -- here it is used when getting `X,Y_true`. – Andreas Storvik Strauman Apr 15 '19 at 11:47
  • What I do not understand is...if your boolean value is independent of the model prediction, you can already get your `X, Y_true` according to the boolean and pass it to your `fit` function. So, why do you need the boolean in the `fit`? And if it is dependent on the training, how? – Anakin Apr 15 '19 at 11:53
  • @Anakin I appreciate you're trying to help. I am not using a boolean. Using boolean is just to keep things simple. In reality I have strings that are iterator handles that needs to be fed (based on what dataset I want to train). This is also what I have written right below the code. The model I'm working on does depend on training, since the values in the iterators are undetermined. – Andreas Storvik Strauman Apr 15 '19 at 11:54

0 Answers0