Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import keras
from keras import models
from keras import layers
from sklearn.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder
import numpy as np
import keras.backend as K
# Main
np.random.seed(7)
network = models.Sequential()
# Load the dataset
dataset = np.loadtxt("data.csv", delimiter=",", dtype='float64')
# Prep the inputs
data = dataset[:,0:3]
labels = dataset[:,3]
label_encoder = LabelEncoder()
label_encoder.fit(labels)
data = data.astype(np.float64)
labels = labels.astype(np.float64).reshape((-1,1))
# Scale the data min max
min_max = MinMaxScaler(feature_range=(0, 1))
min_max_data = min_max.fit_transform(data)
# Scale the data scalar
scalar = StandardScaler()
scalar.fit(data)
std_scalar_data = scalar.transform(data)
# 3 layers with 3 nodes each
network.add(layers.Dense(3, input_dim=3, kernel_initializer='normal'))
#network.add(layers.Dense(3, activation="sigmoid"))
#network.add(layers.Dense(3, activation="sigmoid"))
network.add(layers.Dense(1, activation="sigmoid"))
network.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
# Set some parameters
epoch = 1000
batch = 10
keras.optimizers.Adam(lr=0.02, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
# Train the network
network.fit(min_max_data, labels, epochs=epoch, batch_size=batch, verbose=False)
# Get accuracy
scores = network.evaluate(min_max_data, labels)
print("\n%s: %.2f%%" % (network.metrics_names[1], scores[1]*100))
# Predict some hosts, last 2 are not sandbox inputs
sandbox = np.asarray([[44, 11, 4],[50, 12.5, 4], [32, 10.66, 3], [11, 2.75, 4], [142, 71, 2], [255, 85, 3]])
sandbox_predict_scalar = scalar.transform(sandbox)
sandbox_predict_min_max = min_max.transform(sandbox)
prediction_prob = network.predict(sandbox_predict_scalar)
print("\n---- probabilities ----")
for v in prediction_prob:
print("Prediction: {}".format(v[0]))
prediction_class = network.predict_classes(sandbox_predict_scalar, verbose=True)
print("\n---- binary classifications ----")
for i in prediction_class:
if i == 1:
print("Sandbox: {}".format(i[0]))
if i == 0:
print("Not a Sandbox: {}".format(i[0]))
# Print weights for the network
print("\n---- network weights ----")
for layer in network.layers:
print(layer.name)
print(layer.get_weights())
# Function to get activation values https://stackoverflow.com/questions/41711190/keras-how-to-get-the-output-of-each-layer
def get_activations(model, model_inputs, print_shape_only=False, layer_name=None):
print('\n----- activations -----')
activations = []
inp = model.input
model_multi_inputs_cond = True
if not isinstance(inp, list):
# only one input! let's wrap it in a list.
inp = [inp]
model_multi_inputs_cond = False
outputs = [layer.output for layer in model.layers if
layer.name == layer_name or layer_name is None] # all layer outputs
funcs = [K.function(inp + [K.learning_phase()], [out]) for out in outputs] # evaluation functions
if model_multi_inputs_cond:
list_inputs = []
list_inputs.extend(model_inputs)
list_inputs.append(0.)
else:
list_inputs = [model_inputs, 0.]
# Learning phase. 0 = Test mode (no dropout or batch normalization)
# layer_outputs = [func([model_inputs, 0.])[0] for func in funcs]
layer_outputs = [func(list_inputs)[0] for func in funcs]
for layer_activations in layer_outputs:
activations.append(layer_activations)
if print_shape_only:
print(layer_activations.shape)
else:
print(layer_activations)
return activations
sandbox_activation = np.asarray([[44, 11, 4]])
sandbox_predict_act = scalar.transform(sandbox_activation)
print(sandbox_predict_act)
get_activations(network, sandbox_predict_act)
sandbox_activation2 = np.asarray([[255, 85, 3]])
sandbox_predict_act2 = scalar.transform(sandbox_activation2)
print(sandbox_predict_act2)
get_activations(network, sandbox_predict_act2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.