-
Notifications
You must be signed in to change notification settings - Fork 115
Model Definition Summary
brightcoder01 edited this page Dec 13, 2019
·
14 revisions
CATEGORICAL_FEATURE_KEYS = [
"workclass",
"education",
"marital-status",
"occupation",
"relationship",
"race",
"sex",
"native-country",
]
NUMERIC_FEATURE_KEYS = [
"age",
"capital-gain",
"capital-loss",
"hours-per-week",
]
OPTIONAL_NUMERIC_FEATURE_KEYS = [
"education-num",
]
LABEL_KEY = "label"
def get_feature_columns():
feature_columns = []
for numeric_feature_key in NUMERIC_FEATURE_KEYS:
numeric_feature = tf.feature_column.numeric_column(numeric_feature_key)
feature_columns.append(numeric_feature)
for categorical_feature_key in CATEGORICAL_FEATURE_KEYS:
embedding_feature = tf.feature_column.embedding_column(
tf.feature_column.categorical_column_with_hash_bucket(categorical_feature_key, hash_bucket_size=64),
dimension=16
)
feature_columns.append(embedding_feature)
return feature_columns
def get_feature_columns_and_inputs():
feature_columns = []
feature_input_layers = {}
for numeric_feature_key in NUMERIC_FEATURE_KEYS:
numeric_feature = tf.feature_column.numeric_column(numeric_feature_key)
feature_columns.append(numeric_feature)
feature_input_layers[numeric_feature_key] = tf.keras.Input(
shape=(1,), name=numeric_feature_key, dtype=tf.float32
)
for categorical_feature_key in CATEGORICAL_FEATURE_KEYS:
embedding_feature = tf.feature_column.embedding_column(
tf.feature_column.categorical_column_with_hash_bucket(
categorical_feature_key, hash_bucket_size=64
),
dimension=16,
)
feature_columns.append(embedding_feature)
feature_input_layers[categorical_feature_key] = tf.keras.Input(
shape=(1,), name=categorical_feature_key, dtype=tf.string
)
return feature_columns, feature_input_layers
def custom_model(feature_columns):
model = tf.keras.Sequential([
tf.keras.layers.DenseFeatures(feature_columns=feature_columns),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
return model
model = custom_model(feature_columns)
def custom_model(feature_columns, feature_inputs):
feature_layer = tf.keras.layers.DenseFeatures(feature_columns)
x = feature_layer(feature_inputs)
x = tf.keras.layers.Dense(16, activation="relu")(x)
x = tf.keras.layers.Dense(16, activation="relu")(x)
y = tf.keras.layers.Dense(1, activation="sigmoid")(x)
model = tf.keras.Model(inputs=feature_inputs, outputs=y)
return model
feature_columns, feature_inputs = get_feature_columns_and_inputs()
model = custom_model(feature_columns, feature_inputs)
class CustomModel(tf.keras.Model):
def __init__(self, feature_columns, input_layers):
super(CustomModel, self).__init__(name="census_model")
self.dense_features = tf.keras.layers.DenseFeatures(feature_columns)
self.input_layers = input_layers
self.dense_1 = tf.keras.layers.Dense(16, activation="relu")
self.dense_2 = tf.keras.layers.Dense(16, activation="relu")
self.dense_3 = tf.keras.layers.Dense(1, activation="sigmoid")
def call(self, inputs, training=False):
x = self.dense_features(inputs)
x = self.dense_1(x)
x = self.dense_2(x)
x = self.dense_3(x)
return x
feature_columns, feature_inputs = get_feature_columns_and_inputs()
model = CustomModel(feature_columns, feature_inputs)