TF 1.12:
Trying to convert Pre-canned estimator to Keras with tf.keras.layers:
estimator = tf.estimator.DNNClassifier(
model_dir='/tmp/keras',
feature_columns=deep_columns,
hidden_units = [100, 75, 50, 25],
config=run_config)
to a Keras model using tf.keras.layers:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(100, activation=tf.nn.relu, input_shape=(14,)))
model.add(tf.keras.layers.Dense(75))
model.add(tf.keras.layers.Dense(50))
model.add(tf.keras.layers.Dense(25))
model.add(tf.keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.compile(optimizer=tf.keras.optimizers.RMSprop(), loss=tf.keras.losses.binary_crossentropy, metrics=['accuracy'])
model.summary()
estimator = tf.keras.estimator.model_to_estimator(model, model_dir='/tmp/keras', config=run_config)
When I run the Keras model I get:
for n in range(40 // 2):
estimator.train(input_fn=train_input_fn)
results = estimator.evaluate(input_fn=eval_input_fn)
# Display evaluation metrics
tf.logging.info('Results at epoch %d / %d', (n + 1) * 2, 40)
tf.logging.info('-' * 60)
When I train it I get this error:
Main code: https://github.com/tensorflow/models/blob/master/official/wide_deep/census_main.py
KeyError: "The dictionary passed into features does not have the expected inputs keys defined in the keras model.\n\tExpected keys: {'dense_50_input'}\n\tfeatures keys: {'workclass', 'occupation', 'hours_per_week', 'marital_status', 'relationship', 'race', 'fnlwgt', 'education', 'gender', 'capital_loss', 'capital_gain', 'age', 'education_num', 'native_country'}\n\tDifference: {'workclass', 'occupation', 'hours_per_week', 'marital_status', 'relationship', 'dense_50_input', 'race', 'fnlwgt', 'education', 'gender', 'capital_loss', 'capital_gain', 'age', 'education_num', 'native_country'}"
This is my input_fn:
def input_fn(data_file, num_epochs, shuffle, batch_size):
"""Generate an input function for the Estimator."""
assert tf.gfile.Exists(data_file), (
'%s not found. Please make sure you have run census_dataset.py and '
'set the --data_dir argument to the correct path.' % data_file)
def parse_csv(value):
tf.logging.info('Parsing {}'.format(data_file))
columns = tf.decode_csv(value, record_defaults=_CSV_COLUMN_DEFAULTS)
features = dict(zip(_CSV_COLUMNS, columns))
labels = features.pop('income_bracket')
classes = tf.equal(labels, '>50K') # binary classification
return features, classes
# Extract lines from input files using the Dataset API.
dataset = tf.data.TextLineDataset(data_file)
if shuffle:
dataset = dataset.shuffle(buffer_size=_NUM_EXAMPLES['train'])
dataset = dataset.map(parse_csv, num_parallel_calls=5)
# We call repeat after shuffling, rather than before, to prevent separate
# epochs from blending together.
dataset = dataset.repeat(num_epochs)
dataset = dataset.batch(batch_size)
return dataset
def train_input_fn():
return input_fn(train_file, 2, True, 40)
def eval_input_fn():
return input_fn(test_file, 1, False, 40)
model.fit(train_x, train_y)somewhere in your code? - BigD