Closed victorconan closed 3 years ago
Thanks @victorconan for raising the issue and for the detailed reproduction instructions.
AdversarialRegularization
expects Sequential
base models to provide a list of feature names in order to distinguish input features and target labels, but unfortunately the feature names specified in feature columns (the DenseFeatures
layer) aren't populated to the model.
Here's a workaround using a Keras functional model and explicit Input
layers:
def build_base_model(hparams):
feature_cols = [tf.feature_column.numeric_column(name) for name in hparams.feature_names]
feature_layer = tf.keras.layers.DenseFeatures(feature_cols)
x_input = [tf.keras.Input(shape=[], name=name, dtype=tf.float32) for name in hparams.feature_names]
x_dict = dict(zip(hparams.feature_names, x_input))
x_dense = feature_layer(x_dict)
for num_units in hparams.layers:
x_dense = tf.keras.layers.Dense(num_units, activation='relu')(x_dense)
pred = tf.keras.layers.Dense(hparams.num_classes, activation='sigmoid')(x_dense)
return tf.keras.Model(inputs=x_input, outputs=pred)
Please let me know whether this works for you.
Thanks! It's working!
I have a base model using tf.feature_column:
And I have parser function to feed data from TFRecords (there are 500 feature columns in the file):
My base model can run successfully when uncommenting the
target
. When I tried to run the adversarial regularization:I got error:
I don't understand this error. Is it because my sequential model does not build until training is running? Any suggestions for workarounds?