Skip to content

Instantly share code, notes, and snippets.

View nitin-bommi's full-sized avatar
:octocat:
Epoch: 22/IDK

Nitin Sai Bommi nitin-bommi

:octocat:
Epoch: 22/IDK
View GitHub Profile
# training the model
history = model.fit(train_generator,
steps_per_epoch = 1000, # len(train) / BS
epochs = 100,
validation_data = validation_generator,
validation_steps = 500, # len(test) / BS
verbose = 2
)
# visualising the loss and accuracy
class myCallBack(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs['accuracy']>0.99):
print("\n\nAccuracy greater than 99%. Stopping the training")
self.model.stop_training = True
callbacks = myCallBack()
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
# importing the library
import tensorflow as tf
# generating new images
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255,
rotation_range = 60,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
@nitin-bommi
nitin-bommi / roc.py
Last active June 18, 2020 14:49
roc curve
# To compute the ROC curve and the area under the curve.
from sklearn.metrics import roc_curve, roc_auc_score
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_train_2, y_scores)
def plot_roc_curve(fpr, tpr, label=None):
plt.plot(fpr, tpr, linewidth=2, label=label)
plt.plot([0,1], [0,1], 'k--')
plt.axis([0,1,0,1])
plt.xlabel("False Positive Rate", fontsize=16)
# To compute the F1 score, simply call the f1_score() function:
from sklearn.metrics import f1_score
f1_score(y_train_5, y_train_pred)
@nitin-bommi
nitin-bommi / precision_recall.py
Created June 18, 2020 14:28
precision and recall
# Finding precision and recall
from sklearn.metrics import precision_score, recall_score
precision_score(y_train_5, y_train_pred)
recall_score(y_train_5, y_train_pred)
@nitin-bommi
nitin-bommi / confusion_matrix.py
Created June 18, 2020 14:17
interpreting confusion matrix
# Creating some predictions.
from sklearn.model_selection import cross_val_predict
y_train_pred = cross_val_predict(sgd_clf, X_train, y_train_5, cv=3)
"""
You could make predictions on the test set, but use the test set only at the very end of your project, once you have a classifier that you are ready to launch.
"""
# Constructing the confusion matrix.
from sklearn.metrics import confusion_matrix
@nitin-bommi
nitin-bommi / dumb_classifier.py
Created June 18, 2020 14:15
training a dumb classifier
# Importing the dataset.
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1)
# Creating independent and dependent variables.
X, y = mnist['data'], mnist['target']
# Splitting the data into training set and test set.
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]