Skip to content

Instantly share code, notes, and snippets.

View TheLoneNut's full-sized avatar

Pascal Potvin TheLoneNut

View GitHub Profile
database = {}
database['normal'] = traffic_to_encoding(get_example_label(train_cases_df, df_lens, 0), base_network)
database['error2'] = traffic_to_encoding(get_example_label(train_cases_df, df_lens, 1), base_network)
# Prediction on traffic
identify_traffic(x, database, base_network)
def identify_traffic(x, database, model):
    """
    Implements traffic recognition.
    Arguments:
    x -- the traffic to identify
    database -- database containing recognized traffic encodings
    model -- the encoding model
    Returns:
def traffic_to_encoding(x, model):
    return model.predict(np.array([x]))
# Training the model
model.fit(train_data, y_dummie, batch_size=256, epochs=10)
in_dims = (N_MINS, n_feat)
out_dims = N_FACTORS
# Network definition
with tf.device(tf_device):
    # Create the 3 inputs
    anchor_in = Input(shape=in_dims)
    pos_in = Input(shape=in_dims)
    neg_in = Input(shape=in_dims)
def create_base_network(in_dims, out_dims):
    """
    Base network to be shared.
    """
    model = Sequential()
    model.add(BatchNormalization(input_shape=in_dims))
    model.add(LSTM(512, return_sequences=True, dropout=0.2, recurrent_dropout=0.2, implementation=2))
    model.add(LSTM(512, return_sequences=False, dropout=0.2, recurrent_dropout=0.2, implementation=2))
    model.add(BatchNormalization())
    model.add(Dense(512, activation='relu'))
def triplet_loss(y_true, y_pred, alpha = 0.2):
    """
    Implementation of the triplet loss function
    Arguments:
    y_true -- true labels, required when you define a loss in Keras, not used in this function.
    y_pred -- python list containing three objects:
            anchor:   the encodings for the anchor data
            positive: the encodings for the positive data (similar to anchor)
            negative: the encodings for the negative data (different from anchor)
@TheLoneNut
TheLoneNut / new_triplet_loss.py
Last active February 15, 2018 15:52
Siamese Network
def triplet_loss(y_true, y_pred, alpha = ALPHA):
"""
Implementation of the triplet loss function
Arguments:
y_true -- true labels, required when you define a loss in Keras, you don't need it in this function.
y_pred -- python list containing three objects:
anchor -- the encodings for the anchor data
positive -- the encodings for the positive data (similar to anchor)
negative -- the encodings for the negative data (different from anchor)