This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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:] |