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
def get_heatmap(model, layer_name, matrix, y_labels): | |
# obtain probability of the label with the highest certainty | |
network_output = model.get_output_at(0)[:, np.argmax(y_labels)] | |
# obtain the output vector and its dimension of the convolutional layer we want to visualize | |
conv_layer, layer_output_dim = get_conv_layer(model, layer_name) | |
# Setting up the calculation of the gradients between the output and the conv layer. Will be executed in the iteration step | |
grads = K.gradients(network_output, conv_layer.output)[0] | |
# average the gradients across our samples (one sample) and all filters |
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
x = tf.keras.layers.Dense(256, activation='relu')(pooled_output) | |
dense = tf.keras.layers.Dense(64, activation='relu')(x) | |
pred = tf.keras.layers.Dense(1, activation='sigmoid')(dense) | |
model = tf.keras.Model( | |
inputs=[inputs['input_word_ids'], | |
inputs['input_mask'], | |
inputs['input_type_ids']], | |
outputs=pred | |
) |
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
bert_layer = load_bert_layer() | |
pooled_output, _ = bert_layer( | |
[input_word_ids, | |
input_mask, | |
input_type_ids | |
] | |
) |
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
feature_spec = tf_transform_output.transformed_feature_spec() | |
feature_spec.pop(_LABEL_KEY) | |
inputs = { | |
key: tf.keras.layers.Input( | |
shape=(max_seq_length), | |
name=key, | |
dtype=tf.int32) | |
for key in feature_spec.keys() | |
} |
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
feature_spec = tf_transform_output.transformed_feature_spec() | |
feature_spec.pop(_LABEL_KEY) | |
inputs = {key: tf.keras.layers.Input(shape=(max_seq_length), | |
name=key, dtype=tf.int32) | |
for key in feature_spec.keys()} | |
input_word_ids = tf.cast(inputs["input_word_ids"], dtype=tf.int32) | |
input_mask = tf.cast(inputs["input_mask"], dtype=tf.int32) | |
input_type_ids = tf.cast(inputs["input_type_ids"], dtype=tf.int32) |
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
tf_transform_output = tft.TFTransformOutput(fn_args.transform_output) | |
train_dataset = _input_fn(fn_args.train_files, tf_transform_output, 32) | |
eval_dataset = _input_fn(fn_args.eval_files, tf_transform_output, 32) | |
... | |
model.fit( | |
train_dataset, | |
validation_data=eval_dataset, | |
... | |
) |
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
{ | |
'input_mask': array( | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), | |
'input_type_ids': array( | |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), | |
'input_word_ids': array( | |
[ 101, 2023, 3319, 3397, 27594, 2545, 2005, 2216, 2040, ..., 2014, 102]), | |
'label': array([0]) | |
} |
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
mirrored_strategy = tf.distribute.MirroredStrategy() | |
with mirrored_strategy.scope(): | |
model = get_model(tf_transform_output=tf_transform_output) |
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
def run_fn(fn_args: TrainerFnArgs): | |
tf_transform_output = tft.TFTransformOutput(fn_args.transform_output) | |
train_dataset = _input_fn( | |
fn_args.train_files, tf_transform_output, 32) | |
eval_dataset = _input_fn( | |
fn_args.eval_files, tf_transform_output, 32) | |
mirrored_strategy = tf.distribute.MirroredStrategy() | |
with mirrored_strategy.scope(): | |
model = get_model(tf_transform_output=tf_transform_output) |
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
input_word_ids = tf.cast(inputs["input_word_ids"], dtype=tf.int32) | |
input_mask = tf.cast(inputs["input_mask"], dtype=tf.int32) | |
input_type_ids = tf.cast(inputs["input_type_ids"], dtype=tf.int32) |