Created
August 22, 2018 21:50
-
-
Save zmjjmz/3f621aaafc5683238ade6224e5dedcb6 to your computer and use it in GitHub Desktop.
Keras unconsumed output failure reproduction script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import tensorflow | |
print("Tensorflow version: {0}".format(tensorflow.VERSION)) | |
DATA_SIZE = 1024 | |
BATCH_SIZE = 32 | |
N_EPOCHS = 1 | |
EMBED_DIM = 100 | |
DATA_DICT = {'input': numpy.random.rand( | |
DATA_SIZE, EMBED_DIM).astype('float32')} | |
LABELS = tensorflow.keras.utils.to_categorical( | |
numpy.random.randint(2, size=(DATA_SIZE,)), num_classes=2) | |
def get_input_fn(incl_reporting=False): | |
y_dict = {'classes':LABELS} | |
if incl_reporting: | |
y_dict['reporting'] = LABELS | |
return tensorflow.estimator.inputs.numpy_input_fn( | |
x=DATA_DICT, y=y_dict, | |
shuffle=True, | |
batch_size=BATCH_SIZE, | |
num_epochs=N_EPOCHS, | |
) | |
def create_keras_model(): | |
inp = tensorflow.keras.layers.Input(shape=(EMBED_DIM,), name='input') | |
class_layer = tensorflow.keras.layers.Dense( | |
2, input_shape=(EMBED_DIM,), activation='softmax', name='classes')(inp) | |
reporting_layer = tensorflow.keras.layers.ELU(alpha=0.5, name='reporting')(inp) | |
model = tensorflow.keras.models.Model( | |
inputs=[inp], outputs=[class_layer, reporting_layer]) | |
model.compile(optimizer='sgd', | |
loss={'classes': 'categorical_crossentropy'}, | |
metrics={'classes': 'accuracy'}) | |
return model | |
if __name__ == "__main__": | |
keras_model = create_keras_model() | |
print("Attempting to train with keras Model fit method") | |
keras_model.fit(x=DATA_DICT, y={'classes':LABELS}, | |
batch_size=BATCH_SIZE, epochs=N_EPOCHS) | |
print("Converting keras model to Estimator") | |
train_input_fn = get_input_fn(incl_reporting=True) | |
eval_input_fn = get_input_fn(incl_reporting=True) | |
evalspec = tensorflow.estimator.EvalSpec(eval_input_fn, steps=None, | |
start_delay_secs=0, throttle_secs=1) | |
steps = ((DATA_SIZE // BATCH_SIZE) + 1) * N_EPOCHS | |
trainspec = tensorflow.estimator.TrainSpec(train_input_fn, max_steps=steps) | |
estimator = tensorflow.keras.estimator.model_to_estimator(keras_model) | |
print("Attempting to train with Estimator train_and_evaluate") | |
tensorflow.estimator.train_and_evaluate( | |
estimator=estimator, train_spec=trainspec, eval_spec=evalspec) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What happens if this is run without providing the fake data to
train_input_fn
(i.e. settingincl_reporting
to False on line 53):Note that it failed in
_train_model
specifically.