Skip to content

Commit

Permalink
Trying example from Comet.ml (Keras)
Browse files Browse the repository at this point in the history
  • Loading branch information
srafay committed Apr 5, 2018
1 parent fc8095f commit 3320fc9
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import mean_squared_error

# Importing the dataset
dataset = pd.read_csv('50_Startups.csv')
Expand Down Expand Up @@ -57,4 +58,12 @@
regressor_OLS.summary()
X_opt = X[:, [0, 3]]
regressor_OLS = sm.OLS(endog = y, exog = X_opt).fit()
regressor_OLS.summary()
regressor_OLS.summary()

mse1 = mean_squared_error(y_test, y_pred)

X_train, X_test, y_train, y_test = train_test_split(X_opt, y, test_size = 0.2, random_state = 0)
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
mse2 = mean_squared_error(y_test, y_pred)
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,36 @@
# Installing Keras
# pip install --upgrade keras

from comet_ml import Experiment

#create an experiment with your api key
experiment = Experiment(api_key="Dz2W3DAahv0OvSAERUfhA5b7I",
project_name='general',
auto_param_logging=False)

batch_size = 10
epochs = 50
num_nodes = 6
activation = 'relu'
optimizer = 'adam'

#these will all get logged
params={'batch_size':batch_size,
'epochs':epochs,
'layer1_type':'Dense',
'layer1_num_nodes':num_nodes,
'layer1_activation':activation,
'optimizer':optimizer
}


# Part 1 - Data Preprocessing

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from keras.callbacks import EarlyStopping

# Importing the dataset
dataset = pd.read_csv('Churn_Modelling.csv')
Expand Down Expand Up @@ -60,18 +84,40 @@
# Adding the output layer
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 'sigmoid'))

#print model.summary() to preserve automatically in `Output` tab
print(classifier.summary())

# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)

#will log metrics with the prefix 'train_'
with experiment.train():
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train,
batch_size = batch_size,
nb_epoch = epochs,
verbose = 1,
callbacks=[EarlyStopping(monitor='val_loss', min_delta=1e-4,patience=3, verbose=1, mode='auto')])

# Part 3 - Making the predictions and evaluating the model

# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)

#will log metrics with the prefix 'test_'
with experiment.test():
loss, accuracy = classifier.evaluate(X_test, y_test)
metrics = {
'loss':loss,
'accuracy':accuracy
}
experiment.log_multiple_metrics(metrics)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)

experiment.log_multiple_params(params)
experiment.log_dataset_hash(X_train) #creates and logs a hash of your data
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from comet_ml import Experiment

#create an experiment with your api key
experiment = Experiment(api_key="Dz2W3DAahv0OvSAERUfhA5b7I",
project_name='general',
auto_param_logging=False)

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.callbacks import EarlyStopping

batch_size = 16
num_classes = 10
epochs = 5
num_nodes = 16
optimizer = 'adam'
activation = 'relu'

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')


# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

#these will all get logged
params={'batch_size':batch_size,
'epochs':epochs,
'layer1_type':'Dense',
'layer1_num_nodes':num_nodes,
'layer1_activation':activation,
'optimizer':optimizer
}
model = Sequential()
model.add(Dense(num_nodes, activation='relu', input_shape=(784,)))
#model.add(Dense(256, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

#print model.summary() to preserve automatically in `Output` tab
print(model.summary())

model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])

#will log metrics with the prefix 'train_'
with experiment.train():
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test),
callbacks=[EarlyStopping(monitor='val_loss', min_delta=1e-4,patience=3, verbose=1, mode='auto')])

#will log metrics with the prefix 'test_'
with experiment.test():
loss, accuracy = model.evaluate(x_test, y_test)
metrics = {
'loss':loss,
'accuracy':accuracy
}
experiment.log_multiple_metrics(metrics)

experiment.log_multiple_params(params)
experiment.log_dataset_hash(x_train) #creates and logs a hash of your data

0 comments on commit 3320fc9

Please sign in to comment.