<jupyter_start><jupyter_text>Import Libraries<jupyter_code>import csv
import numpy as np
import tensorflow as tf
import datetime
import os
from tensorflow.keras import backend as K<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>attack_set_1_raw = np.genfromtxt('zodiac_attacked.csv', 
                             delimiter=',',
                             skip_header=2)

no_attack_set_1_raw = np.genfromtxt('zodiac_not_attacked.csv', 
                             delimiter=',',
                             skip_header=2)<jupyter_output><empty_output><jupyter_text>Clean up data (Remove flow count from data)<jupyter_code>attack_set_1 = np.take(attack_set_1_raw, [1,2,3,4,15], axis=1)
no_attack_set_1 = np.take(no_attack_set_1_raw, [1,2,3,4,15], axis=1)
training_set = np.concatenate((attack_set_1, no_attack_set_1), axis=0)
attack_set_1<jupyter_output><empty_output><jupyter_text>Create output data<jupyter_code>zeroes_set = np.zeros((len(no_attack_set_1),1))
ones_set = np.ones((len(attack_set_1),1))
target_set = np.concatenate((ones_set, zeroes_set), axis=0)<jupyter_output><empty_output><jupyter_text>Create type of model<jupyter_code># Default Model
tf.keras.backend.clear_session()

input_size = 5
output_size = 1

model = tf.keras.Sequential([
    tf.keras.layers.Dense(
        5, 
        activation='relu', 
        input_shape=(input_size,),
        kernel_initializer='glorot_normal',
    ),
#     tf.keras.layers.Dense(
#         5, 
#         activation='relu',
#     ),
    tf.keras.layers.Dense(1, activation='sigmoid'),
])

model.compile(optimizer=tf.keras.optimizers.Adam(), 
                loss='binary_crossentropy', 
                metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Callbacks for model customization<jupyter_code>modelStartedTime = datetime.datetime.now().strftime("%Y_%m_%dt%H_%M_%S")

log_dir = os.path.join(
    "logs",
    "adam_optimizer",
    "loss_binary_crossentropy"
    "one_hidden_layer",
#   "two_hidden_layers",
    "validation_split_0D1",
    modelStartedTime,
)

# log_dir = os.path.join(
#     "logs",
#     "test",
#     modelStartedTime,
# )

callbacks = [
    # tf.keras.callbacks.EarlyStopping(patience=50, monitor='val_loss'),
    tf.keras.callbacks.TensorBoard(
        log_dir=log_dir,
        
        #update_freq='epoch'        
    )
]<jupyter_output><empty_output><jupyter_text>Train Model<jupyter_code>model.fit(
    training_set, 
    target_set, 
    initial_epoch=0,
    # initial_epoch=init_epoch-epoch_count,
    epochs=100,
    validation_split=0.1, 
    # batch_size=20, 
    verbose=2, 
    shuffle=True,
    callbacks=callbacks, 
)
model.save('port_model.h5')<jupyter_output><empty_output><jupyter_text>Get Weights<jupyter_code>model.layers[0].get_weights()<jupyter_output><empty_output><jupyter_text>Get output of model Save Machine Learning Model<jupyter_code>tf.keras.experimental.export_saved_model(model, 'best_model_v2' + modelStartedTime + '.h5')
model.save('port_model.h5')
loaded_model = tf.keras.models.load_model('best_model_2019_10_20t21_32_39.h5')
loaded_model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 5)                 30        
_________________________________________________________________
dense_1 (Dense)              (None, 5)                 30        
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 6         
=================================================================
Total params: 66
Trainable params: 66
Non-trainable params: 0
_________________________________________________________________
{'fraction_non_alphanumeric': 0.14263485477178423, 'fraction_numerical': 0.021784232365145227, 'mean_word_length': 3.970360824742268, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '142828', 'license': 'MIT', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1268, 'n_words': 164, 'path': 'port_model/sdn_machine_learning.ipynb', 'repo_name': 'chrissayon/sdn_machine_learning', 'size': '3856'}