Experiment 1:
Implement multilayer perceptron algorithm for MNIST Hand written Digit Classification.
Program:
import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np
from [Link] import mnist
objects=mnist
(train_img,train_lab),(test_img,test_lab)=objects.load_data()
for i in range(20):
[Link](4,5,i+1)
[Link](train_img[i],cmap='gray_r')
[Link]("Digit : {}".format(train_lab[i]))
plt.subplots_adjust(hspace=0.5)
[Link]('off')
print('Training images shape : ',train_img.shape)
print('Testing images shape : ',test_img.shape)
print('How image looks like : ')
print(train_img[0])
[Link](train_img[0].reshape(784),facecolor='orange')
[Link]('Pixel vs its intensity',fontsize=16)
[Link]('PIXEL')
[Link]('Intensity')
train_img=train_img/255.0
test_img=test_img/255.0
print('How image looks like after normalising: ')
print(train_img[0])
[Link](train_img[0].reshape(784),facecolor='orange')
[Link]('Pixel vs its intensity',fontsize=16)
[Link]('PIXEL')
[Link]('Intensity')
from [Link] import Sequential
from [Link] import Flatten,Dense
model=Sequential()
input_layer= Flatten(input_shape=(28,28))
[Link](input_layer)
hidden_layer1=Dense(512,activation='relu')
[Link](hidden_layer1)
hidden_layer2=Dense(512,activation='relu')
[Link](hidden_layer2)
output_layer=Dense(10,activation='softmax')
[Link](output_layer)
#compiling the sequential model
[Link](optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
[Link](train_img,train_lab,epochs=100)
[Link]('project.h5')
loss_and_acc=[Link](test_img,test_lab,verbose=2)
print("Test Loss", loss_and_acc[0])
print("Test Accuracy", loss_and_acc[1])
[Link](test_img[0],cmap='gray_r')
[Link]('Actual Value: {}'.format(test_lab[0]))
prediction=[Link](test_img)
[Link]('off')
print('Predicted Value: ',[Link](prediction[0]))
if(test_lab[0]==([Link](prediction[0]))):
print('Successful prediction')
else:
print('Unsuccessful prediction')
[Link](test_img[1],cmap='gray_r')
[Link]('Actual Value: {}'.format(test_lab[1]))
prediction=[Link](test_img)
[Link]('off')
print('Predicted Value: ',[Link](prediction[1]))
if(test_lab[1]==([Link](prediction[1]))):
print('Successful prediction')
else:
print('Unsuccessful prediction')
[Link](test_img[2],cmap='gray_r')
[Link]('Actual Value: {}'.format(test_lab[2]))
prediction=[Link](test_img)
[Link]('off')
print('Predicted Value: ',[Link](prediction[2]))
if(test_lab[2]==([Link](prediction[2]))):
print('Successful prediction')
else:
print('Unsuccessful prediction')
# make a prediction for a new image.
from keras_preprocessing.image import load_img
from keras_preprocessing.image import img_to_array
from [Link] import load_model
# load and prepare the image
def load_image(filename):
# load the image
img = load_img(filename, grayscale=True, target_size=(28, 28))
# convert to array
img = img_to_array(img)
# reshape into a single sample with 1 channel
img = [Link](1, 28, 28)
# prepare pixel data
img = [Link]('float32')
img = img / 255.0
return img
from [Link] import files
uploaded = [Link]()
from [Link] import Image
Image('[Link]',width=250,height=250)
img = load_image('[Link]')
digit= [Link](img)
print('Predicted value : ',[Link](digit))
Experiment 2:
Design a neural network for classifying movie reviews (Binary Classification) using IMDB
dataset.
Program:
import keras
keras.__version__
from [Link] import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
train_data[0]
train_labels[0]
max([max(sequence) for sequence in train_data])
# word_index is a dictionary mapping words to an integer index
word_index = imdb.get_word_index()
# We reverse it, mapping integer indices to words
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
# We decode the review; note that our indices were offset by 3
# because 0, 1 and 2 are reserved indices for "padding", "start of sequence", and "unknown".
decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])
decoded_review
import numpy as np
def vectorize_sequences(sequences, dimension=10000):
# Create an all-zero matrix of shape (len(sequences), dimension)
results = [Link]((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1. # set specific indices of results[i] to 1s
return results
# Our vectorized training data
x_train = vectorize_sequences(train_data)
# Our vectorized test data
x_test = vectorize_sequences(test_data)
x_train[0]
# Our vectorized labels
y_train = [Link](train_labels).astype('float32')
y_test = [Link](test_labels).astype('float32')
from keras import models
from keras import layers
model = [Link]()
[Link]([Link](16, activation='relu', input_shape=(10000,)))
[Link]([Link](16, activation='relu'))
[Link]([Link](1, activation='sigmoid'))
[Link](optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
from keras import optimizers
[Link](optimizer=[Link](lr=0.001),
loss='binary_crossentropy',
metrics=['accuracy'])
from keras import losses
from keras import metrics
[Link](optimizer=[Link](lr=0.001),
loss=losses.binary_crossentropy,
metrics=[metrics.binary_accuracy])
x_val = x_train[:10000]
partial_x_train = x_train[10000:]
y_val = y_train[:10000]
partial_y_train = y_train[10000:]
history = [Link](partial_x_train,
partial_y_train,
epochs=20,
batch_size=512,
validation_data=(x_val, y_val))
history_dict = [Link]
history_dict.keys()
import [Link] as plt
acc = [Link]['binary_accuracy']
val_acc = [Link]['val_binary_accuracy']
loss = [Link]['loss']
val_loss = [Link]['val_loss']
epochs = range(1, len(acc) + 1)
# "bo" is for "blue dot"
[Link](epochs, loss, 'bo', label='Training loss')
# b is for "solid blue line"
[Link](epochs, val_loss, 'b', label='Validation loss')
[Link]('Training and validation loss')
[Link]('Epochs')
[Link]('Loss')
[Link]()
[Link]()
[Link]() # clear figure
acc_values = history_dict['binary_accuracy ']
val_acc_values = history_dict['val_binary_accuracy']
[Link](epochs, acc, 'bo', label='Training acc')
[Link](epochs, val_acc, 'b', label='Validation acc')
[Link]('Training and validation accuracy')
[Link]('Epochs')
[Link]('Loss')
[Link]()
[Link]()
model = [Link]()
[Link]([Link](16, activation='relu', input_shape=(10000,)))
[Link]([Link](16, activation='relu'))
[Link]([Link](1, activation='sigmoid')
[Link](optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
[Link](x_train, y_train, epochs=4, batch_size=512)
results = [Link](x_test, y_test)
results
[Link](x_test)
Experiment 3:
Design a neural Network for classifying news wires (Multi class classification) using Reuters dataset.
Program:
import keras
keras.__version__
from [Link] import reuters
(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)
len(train_data)
len(test_data)
train_data[10]
word_index = reuters.get_word_index()
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
# Note that our indices were offset by 3
# because 0, 1 and 2 are reserved indices for "padding", "start of sequence", and "unknown".
decoded_newswire = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])
decoded_newswire
train_labels[10]
import numpy as np
def vectorize_sequences(sequences, dimension=10000):
results = [Link]((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1.
return results
# Our vectorized training data
x_train = vectorize_sequences(train_data)
# Our vectorized test data
x_test = vectorize_sequences(test_data)
def to_one_hot(labels, dimension=46):
results = [Link]((len(labels), dimension))
for i, label in enumerate(labels):
results[i, label] = 1.
return results
# Our vectorized training labels
one_hot_train_labels = to_one_hot(train_labels)
# Our vectorized test labels
one_hot_test_labels = to_one_hot(test_labels
from [Link].np_utils import to_categorical
one_hot_train_labels = to_categorical(train_labels)
one_hot_test_labels = to_categorical(test_labels)
from keras import models
from keras import layers
model = [Link]()
[Link]([Link](64, activation='relu', input_shape=(10000,)))
[Link]([Link](64, activation='relu'))
[Link]([Link](46, activation='softmax'))
[Link](optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
x_val = x_train[:1000]
partial_x_train = x_train[1000:]
y_val = one_hot_train_labels[:1000]
partial_y_train = one_hot_train_labels[1000:]
history = [Link](partial_x_train,
partial_y_train,
epochs=20,
batch_size=512,
validation_data=(x_val, y_val))
import [Link] as plt
loss = [Link]['loss']
val_loss = [Link]['val_loss']
epochs = range(1, len(loss) + 1)
[Link](epochs, loss, 'bo', label='Training loss')
[Link](epochs, val_loss, 'b', label='Validation loss')
[Link]('Training and validation loss')
[Link]('Epochs')
[Link]('Loss')
[Link]()
[Link]()
[Link]() # clear figure
acc = [Link]['accuracy']
val_acc = [Link]['val_accuracy']
[Link](epochs, acc, 'bo', label='Training acc')
[Link](epochs, val_acc, 'b', label='Validation acc')
[Link]('Training and validation accuracy')
[Link]('Epochs')
[Link]('Loss')
[Link]()
[Link]()
model = [Link]()
[Link]([Link](64, activation='relu', input_shape=(10000,)))
[Link]([Link](64, activation='relu'))
[Link]([Link](46, activation='softmax'))
[Link](optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
[Link](partial_x_train,
partial_y_train,
epochs=8,
batch_size=512,
validation_data=(x_val, y_val))
results = [Link](x_test, one_hot_test_labels)
results
predictions = [Link](x_test)
predictions[0].shape
[Link](predictions[0])
[Link](predictions[0])
y_train = [Link](train_labels)
y_test = [Link](test_labels)
[Link](optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['acc'])
model = [Link]()
[Link]([Link](64, activation='relu', input_shape=(10000,)))
[Link]([Link](4, activation='relu'))
[Link]([Link](46, activation='softmax'))
[Link](optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
[Link](partial_x_train,
partial_y_train,
epochs=20,
batch_size=128,
validation_data=(x_val, y_val))
Experiment 4:
Design a neural network for predicting house prices using Boston Housing Price dataset.
Program:
from [Link] import load_boston
import pandas as pd
boston_dataset = load_boston()
df = [Link](boston_dataset.data, columns=boston_dataset.feature_name
s)
df['MEDV'] = boston_dataset.target
[Link](n=10)
from [Link] import make_subplots
import plotly.graph_objects as go
import math
total_items = len([Link])
items_per_row = 3
total_rows = [Link](total_items / items_per_row)
fig = make_subplots(rows=total_rows, cols=items_per_row)
cur_row = 1
cur_col = 1
for index, column in enumerate([Link]):
fig.add_trace([Link](y=df[column], name=column), row=cur_row, col=cur_
col)
if cur_col % items_per_row == 0:
cur_col = 1
cur_row = cur_row + 1
else:
cur_col = cur_col + 1
fig.update_layout(height=1000, width=550, showlegend=False)
[Link]()
from [Link] import make_subplots
import plotly.graph_objects as go
import math
import numpy as np
total_items = len([Link])
items_per_row = 3
total_rows = [Link](total_items / items_per_row)
fig = make_subplots(rows=total_rows, cols=items_per_row, subplot_titles=df
.columns)
cur_row = 1
cur_col = 1
for index, column in enumerate([Link]):
fig.add_trace([Link](x=df[column],
y=df['MEDV'],
mode="markers",
marker=dict(size=3)),
row=cur_row,
col=cur_col)
intercept = np.poly1d([Link](df[column], df['MEDV'], 1))
([Link](df[column]))
fig.add_trace([Link](x=[Link](df[column]),
y=intercept,
line=dict(color='red', width=1)),
row=cur_row,
col=cur_col)
if cur_col % items_per_row == 0:
cur_col = 1
cur_row = cur_row + 1
else:
cur_col = cur_col + 1
fig.update_layout(height=1000, width=550, showlegend=False)
[Link]()
from sklearn.model_selection import train_test_split
X = [Link][:, [Link] != 'MEDV']
y = [Link][:, [Link] == 'MEDV']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, r
andom_state=123)
mean = X_train.mean(axis=0)
std = X_train.std(axis=0)
X_train = (X_train - mean) / std
X_test = (X_test - mean) / std
from [Link] import Sequential
from [Link] import Dense
model = Sequential()
[Link](Dense(128, input_shape=(13, ), activation='relu', name='dense_1'
))
[Link](Dense(64, activation='relu', name='dense_2'))
[Link](Dense(1, activation='linear', name='dense_output'))
[Link](optimizer='adam', loss='mse', metrics=['mae'])
[Link]()
history = [Link](X_train, y_train, epochs=100, validation_split=0.05)
fig = [Link]()
fig.add_trace([Link](y=[Link]['loss'],
name='Train'))
fig.add_trace([Link](y=[Link]['val_loss'],
name='Valid'))
fig.update_layout(height=500, width=700,
xaxis_title='Epoch',
yaxis_title='Loss')
[Link]()
fig = [Link]()
fig.add_trace([Link](y=[Link]['mae'],
name='Train'))
fig.add_trace([Link](y=[Link]['val_mae'],
name='Valid'))
fig.update_layout(height=500, width=700,
xaxis_title='Epoch',
yaxis_title='Mean Absolute Error')
[Link]()
mse_nn, mae_nn = [Link](X_test, y_test)
print('Mean squared error on test data: ', mse_nn)
print('Mean absolute error on test data: ', mae_nn)
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error
from [Link] import mean_absolute_error
lr_model = LinearRegression()
lr_model.fit(X_train, y_train)
y_pred_lr = lr_model.predict(X_test)
mse_lr = mean_squared_error(y_test, y_pred_lr)
mae_lr = mean_absolute_error(y_test, y_pred_lr)
print('Mean squared error on test data: ', mse_lr)
print('Mean absolute error on test data: ', mae_lr)
from [Link] import DecisionTreeRegressor
from [Link] import mean_squared_error
from [Link] import mean_absolute_error
tree = DecisionTreeRegressor()
[Link](X_train, y_train)
y_pred_tree = [Link](X_test)
mse_dt = mean_squared_error(y_test, y_pred_tree)
mae_dt = mean_absolute_error(y_test, y_pred_tree)
print('Mean squared error on test data: ', mse_dt)
print('Mean absolute error on test data: ', mae_dt)
pip install shap
import shap
[Link]()
explainer = [Link](model, X_train[:100].values)
shap_values = explainer.shap_values(X_test[:100].values)
shap.summary_plot(shap_values, X_test, plot_type='bar')
Experiment 5:
Build a Convolution Neural Network for MNIST Hand written Digit Classification.
Program:
import tensorflow as tf
from [Link] import layers,models
from tensorflow import keras
import numpy as np
(X_train, y_train) , (X_test, y_test) = [Link].load_data()
X_train = X_train / 255
X_test = X_test / 255
X_train[0]
X_train[0]
X_train = X_train.reshape(-1,28,28,1)
X_train.shape
X_test = X_test.reshape(-1,28,28,1)
X_test.shape
convolutional_neural_network = [Link]([
layers.Conv2D(filters=25, kernel_size=(3, 3), activation='relu', input
_shape=(28,28,1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
[Link](),
[Link](64, activation='relu'),
[Link](10, activation='softmax')
])
convolutional_neural_network.compile(optimizer='adam', loss='sparse_catego
rical_crossentropy', metrics=['accuracy'])
convolutional_neural_network.fit(X_train, y_train, epochs=10)
convolutional_neural_network.evaluate(X_test, y_test)
y_predicted_by_model = convolutional_neural_network.predict(X_test)
y_predicted_by_model[0] #getting probability score for each class digits
[Link](y_predicted_by_model[0])
y_predicted_labels = [[Link](i) for i in y_predicted_by_model]
y_predicted_labels[:5]
Experiment 6:
Build a Convolution Neural Network for simple image (dogs and Cats) Classification.
Program:
import os, shutil
from [Link] import drive
[Link]("/content/gdrive")
# The path to the directory where the original
# dataset was uncompressed
original_dataset_dir = '/content/gdrive/My Drive/dogs_cats_small'
# The directory where we will
# store our smaller dataset
[Link](original_dataset_dir,exist_ok=True)
# Directories for our training,
# validation and test splits
train_dir = [Link](original_dataset_dir, 'train')
[Link](train_dir,exist_ok=True)
validation_dir = [Link](original_dataset_dir, 'validation')
[Link](validation_dir,exist_ok=True)
test_dir = [Link](original_dataset_dir, 'test')
[Link](test_dir,exist_ok=True)
# Directory with our training cat pictures
train_cats_dir = [Link](train_dir, 'cats')
[Link](train_cats_dir,exist_ok=True)
# Directory with our training dog pictures
train_dogs_dir = [Link](train_dir, 'dogs')
[Link](train_dogs_dir,exist_ok=True)
# Directory with our validation cat pictures
validation_cats_dir = [Link](validation_dir, 'cats')
[Link](validation_cats_dir,exist_ok=True)
# Directory with our validation dog pictures
validation_dogs_dir = [Link](validation_dir, 'dogs')
[Link](validation_dogs_dir,exist_ok=True)
# Directory with our validation cat pictures
test_cats_dir = [Link](test_dir, 'cats')
[Link](test_cats_dir,exist_ok=True)
# Directory with our validation dog pictures
test_dogs_dir = [Link](test_dir, 'dogs')
[Link](test_dogs_dir,exist_ok=True)
# Copy first 1000 cat images to train_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:
src = [Link](original_dataset_dir, fname)
dst = [Link](train_cats_dir, fname)
[Link](src, dst)
# Copy next 500 cat images to validation_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1000, 1500)]
for fname in fnames:
src = [Link](original_dataset_dir, fname)
dst = [Link](validation_cats_dir, fname)
[Link](src, dst)
# Copy next 500 cat images to test_cats_dir
fnames = ['cat.{}.jpg'.format(i) for i in range(1500, 2000)]
for fname in fnames:
src = [Link](original_dataset_dir, fname)
dst = [Link](test_cats_dir, fname)
[Link](src, dst)
# Copy first 1000 dog images to train_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1000)]
for fname in fnames:
src = [Link](original_dataset_dir, fname)
dst = [Link](train_dogs_dir, fname)
[Link](src, dst)
# Copy next 500 dog images to validation_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1000, 1500)]
for fname in fnames:
src = [Link](original_dataset_dir, fname)
dst = [Link](validation_dogs_dir, fname)
[Link](src, dst)
# Copy next 500 dog images to test_dogs_dir
fnames = ['dog.{}.jpg'.format(i) for i in range(1500, 2000)]
for fname in fnames:
src = [Link](original_dataset_dir, fname)
dst = [Link](test_dogs_dir, fname)
[Link](src, dst)
print('total training cat images:', len([Link](train_cats_dir)))
print('total training dog images:', len([Link](train_dogs_dir)))
print('total validation cat images:', len([Link](validation_cats_dir))
)
print('total validation dog images:', len([Link](validation_dogs_dir))
)
print('total test cat images:', len([Link](test_cats_dir)))
print('total test dog images:', len([Link](test_dogs_dir)))
from keras import layers
from keras import models
model = [Link]()
[Link](layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(150, 150, 3)))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(64, (3, 3), activation='relu'))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(128, (3, 3), activation='relu'))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(128, (3, 3), activation='relu'))
[Link](layers.MaxPooling2D((2, 2)))
[Link]([Link]())
[Link]([Link](512, activation='relu'))
[Link]([Link](1, activation='sigmoid'))
[Link]()
from keras import optimizers
[Link](loss='binary_crossentropy',
optimizer=[Link](lr=1e-4),
metrics=['acc'])
from [Link] import ImageDataGenerator
# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
# This is the target directory
train_dir,
# All images will be resized to 150x150
target_size=(150, 150),
batch_size=20,
# Since we use binary_crossentropy loss, we need binary labels
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(150, 150),
batch_size=20,
class_mode='binary')
for data_batch, labels_batch in train_generator:
print('data batch shape:', data_batch.shape)
print('labels batch shape:', labels_batch.shape)
break
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=30,
validation_data=validation_generator,
validation_steps=50)
import [Link] as plt
acc = [Link]['acc']
val_acc = [Link]['val_acc']
loss = [Link]['loss']
val_loss = [Link]['val_loss']
epochs = range(len(acc))
[Link](epochs, acc, 'bo', label='Training acc')
[Link](epochs, val_acc, 'b', label='Validation acc')
[Link]('Training and validation accuracy')
[Link]()
[Link]()
[Link](epochs, loss, 'bo', label='Training loss')
[Link](epochs, val_loss, 'b', label='Validation loss')
[Link]('Training and validation loss')
[Link]()
[Link]()
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
from [Link] import load_img, img_to_array,array_to_img
fnames = [[Link](train_cats_dir, fname) for fname in [Link](trai
n_cats_dir)]
# We pick one image to "augment"
img_path = fnames[3]
# Read the image and resize it
img = load_img(img_path, target_size=(150, 150))
# Convert it to a Numpy array with shape (150, 150, 3)
x = img_to_array(img)
# Reshape it to (1, 150, 150, 3)
x = [Link]((1,) + [Link])
# The .flow() command below generates batches of randomly transformed imag
es.
# It will loop indefinitely, so we need to `break` the loop at some point!
i = 0
for batch in [Link](x, batch_size=1):
[Link](i)
imgplot = [Link](array_to_img(batch[0]))
i += 1
if i % 4 == 0:
break
[Link]()
model = [Link]()
[Link](layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(150, 150, 3)))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(64, (3, 3), activation='relu'))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(128, (3, 3), activation='relu'))
[Link](layers.MaxPooling2D((2, 2)))
[Link](layers.Conv2D(128, (3, 3), activation='relu'))
[Link](layers.MaxPooling2D((2, 2)))
[Link]([Link]())
[Link]([Link](0.5))
[Link]([Link](512, activation='relu'))
[Link]([Link](1, activation='sigmoid'))
[Link](loss='binary_crossentropy',
optimizer=[Link](lr=1e-4),
metrics=['acc'])
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,)
# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
# This is the target directory
train_dir,
# All images will be resized to 150x150
target_size=(150, 150),
batch_size=3,
# Since we use binary_crossentropy loss, we need binary labels
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(150, 150),
batch_size=3,
class_mode='binary')
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=100,
validation_data=validation_generator,
validation_steps=50)
acc = [Link]['acc']
val_acc = [Link]['val_acc']
loss = [Link]['loss']
val_loss = [Link]['val_loss']
epochs = range(len(acc))
[Link](epochs, acc, 'bo', label='Training acc')
[Link](epochs, val_acc, 'b', label='Validation acc')
[Link]('Training and validation accuracy')
[Link]()
[Link]()
[Link](epochs, loss, 'bo', label='Training loss')
[Link](epochs, val_loss, 'b', label='Validation loss')
[Link]('Training and validation loss')
[Link]()
[Link]()
Experiment 7:
Use a pre-trained convolution neural network (VGG16) for image classification.
Program:
from [Link].vgg16 import VGG16
model = VGG16(weights='imagenet')
print([Link]())
import os, shutil
from [Link] import drive
[Link]("/content/gdrive")
from [Link] import image
from [Link].vgg16 import preprocess_input,decode_pr
edictions
import numpy as np
img_path = '/content/gdrive/MyDrive/[Link]'
#There is an interpolation method to match the source size with the target
size
#image loaded in PIL (Python Imaging Library)
img = image.load_img(img_path,color_mode='rgb', target_size=(224, 224))
display(img)
# Converts a PIL Image to 3D Numy Array
x = image.img_to_array(img)
[Link]
# Adding the fouth dimension, for number of images
x = np.expand_dims(x, axis=0)
#mean centering with respect to Image
x = preprocess_input(x)
features = [Link](x)
p = decode_predictions(features)
Experiment 8:
Implement one hot encoding of words or characters.
Program:
import keras
keras.__version__
import numpy as np
# This is our initial data; one entry per "sample"
# (in this toy example, a "sample" is just a sentence, but
# it could be an entire document).
samples = ['The cat sat on the mat.', 'The dog ate my homework.']
# First, build an index of all tokens in the data.
token_index = {}
for sample in samples:
# We simply tokenize the samples via the `split` method.
# in real life, we would also strip punctuation and special characters
# from the samples.
for word in [Link]():
if word not in token_index:
# Assign a unique index to each unique word
token_index[word] = len(token_index) + 1
# Note that we don't attribute index 0 to anything.
# Next, we vectorize our samples.
# We will only consider the first `max_length` words in each sample.
max_length = 10
# This is where we store our results:
results = [Link]((len(samples), max_length, max(token_index.values()) +
1))
for i, sample in enumerate(samples):
for j, word in list(enumerate([Link]()))[:max_length]:
index = token_index.get(word)
results[i, j, index] = 1.
import string
samples = ['The cat sat on the mat.', 'The dog ate my homework.']
characters = [Link] # All printable ASCII characters.
token_index = dict(zip(characters, range(1, len(characters) + 1)))
max_length = 50
results = [Link]((len(samples), max_length, max(token_index.values()) +
1))
for i, sample in enumerate(samples):
for j, character in enumerate(sample[:max_length]):
index = token_index.get(character)
results[i, j, index] = 1.
from [Link] import Tokenizer
samples = ['The cat sat on the mat.', 'The dog ate my homework.']
# We create a tokenizer, configured to only take
# into account the top-1000 most common words
tokenizer = Tokenizer(num_words=1000)
# This builds the word index
tokenizer.fit_on_texts(samples)
# This turns strings into lists of integer indices.
sequences = tokenizer.texts_to_sequences(samples)
# You could also directly get the one-hot binary representations.
# Note that other vectorization modes than one-hot encoding are supported!
one_hot_results = tokenizer.texts_to_matrix(samples, mode='binary')
# This is how you can recover the word index that was computed
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
samples = ['The cat sat on the mat.', 'The dog ate my homework.']
# We will store our words as vectors of size 1000.
# Note that if you have close to 1000 words (or more)
# you will start seeing many hash collisions, which
# will decrease the accuracy of this encoding method.
dimensionality = 1000
max_length = 10
results = [Link]((len(samples), max_length, dimensionality))
for i, sample in enumerate(samples):
for j, word in list(enumerate([Link]()))[:max_length]:
# Hash the word into a "random" integer index
# that is between 0 and 1000
index = abs(hash(word)) % dimensionality
results[i, j, index] = 1.
Experiment 9:
Implement word embeddings for IMDB dataset.
Program:
# get reproducible results
from [Link] import seed
seed(0xdeadbeef)
import tensorflow as tf
[Link].set_seed(0xdeadbeef)
from tensorflow import keras
imdb = [Link]
num_words = 20000
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(seed
=1, num_words=num_words)
print(train_data[0])
print('label:', train_labels[0])
# A dictionary mapping words to an integer index
vocabulary = imdb.get_word_index()
# The first indices are reserved
vocabulary = {k:(v+3) for k,v in [Link]()}
vocabulary["<PAD>"] = 0
# See how integer 1 appears first in the review above.
vocabulary["<START>"] = 1
vocabulary["<UNK>"] = 2 # unknown
vocabulary["<UNUSED>"] = 3
# reversing the vocabulary.
# in the index, the key is an integer,
# and the value is the corresponding word.
index = dict([(value, key) for (key, value) in [Link]()])
def decode_review(text):
'''converts encoded text to human readable form.
each integer in the text is looked up in the index, and
replaced by the corresponding word.
'''
return ' '.join([[Link](i, '?') for i in text])
decode_review(train_data[0])
train_data = [Link].pad_sequences(train_data,
value=vocabulary["
<PAD>"],
padding='post',
maxlen=256)
test_data = [Link].pad_sequences(test_data,
value=vocabulary["<
PAD>"],
padding='post',
maxlen=256)
train_data[1]
model = [Link]()
# the first layer is the embedding layer.
# we indicate the number of possible words,
# the dimension of the embedding space,
# and the maximum size of the text.
[Link]([Link](len(vocabulary), 2, input_length=256))
# the output of the embedding is multidimensional,
# with shape (256, 2)
# for each word, we obtain two values,
# the x and y coordinates
# we flatten this output to be able to
# use it in a dense layer
[Link]([Link]())
# dropout regularization
[Link]([Link](rate=0.5))
# small dense layer. It's role is to analyze
# the distribution of points from embedding
[Link]([Link](5))
# final neuron, with sigmoid activation
# for binary classification
[Link]([Link](1, activation='sigmoid'))
[Link]()
[Link](optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
history = [Link](train_data,
train_labels,
epochs=5,
batch_size=100,
validation_data=(test_data, test_labels),
verbose=1)
import [Link] as plt
def plot_accuracy(history, miny=None):
acc = [Link]['accuracy']
test_acc = [Link]['val_accuracy']
epochs = range(len(acc))
[Link](epochs, acc)
[Link](epochs, test_acc)
if miny:
[Link](miny, 1.0)
[Link]('accuracy')
[Link]('epoch')
[Link]()
plot_accuracy(history)
# with a Sequential model
get_embed_out = [Link](
[[Link][0].input],
[[Link][1].output])
layer_output = get_embed_out([test_data[0]])
print(type(layer_output), len(layer_output), layer_output[0].shape)
words = layer_output[0]
[Link](words[:,0], words[:,1])
review = ['great', 'brilliant','crap','bad',
'fantastic', 'movie', 'seagal']
enc_review = [Link]([vocabulary[word] for word in review])
enc_review
words = get_embed_out([enc_review])[0]
[Link](words[:,0], words[:,1])
for i, txt in enumerate(review):
[Link](txt, (words[i,0], words[i,1]))
import math
def plot_review(i):
# plot the distribution of points
enc_words = test_data[i]
emb_words = get_embed_out([enc_words])[0]
[Link](figsize=(8,8))
[Link](emb_words[:,0], emb_words[:,1])
# use the label as title: 1 is positive,
# 0 is negative
[Link](test_labels[i])
# for words that are far enough from (0,0),
# print the word
for i, (enc_word, emb_word) in enumerate(zip(enc_words, emb_words)):
word = index[enc_word]
x, y = emb_word
if [Link](x**2 + y**2)>0.2:
[Link](word, (x, y))
# fix the range in x and y to be able to compare
# the distributions of different reviews
axes = [Link]()
axes.set_xlim([-0.5,0.5])
axes.set_ylim([-0.5, 0.5])
axes.set_aspect('equal', adjustable='box')
plot_review(15)
plot_review(17)
Experiment 10:
Implement a Recurrent Neural Network for IMDB movie review classification problem.
Program:
import keras
keras.__version__
from [Link] import SimpleRNN
from [Link] import Sequential
from [Link] import Embedding, SimpleRNN
model = Sequential()
[Link](Embedding(10000, 32))
[Link](SimpleRNN(32))
[Link]()
model = Sequential()
[Link](Embedding(10000, 32))
[Link](SimpleRNN(32, return_sequences=True))
[Link]()
model = Sequential()
[Link](Embedding(10000, 32))
[Link](SimpleRNN(32, return_sequences=True))
[Link](SimpleRNN(32, return_sequences=True))
[Link](SimpleRNN(32, return_sequences=True))
[Link](SimpleRNN(32)) # This last layer only returns the last outputs.
[Link]()
from [Link] import imdb
from [Link] import sequence
from [Link] import pad_sequences
max_features = 10000 # number of words to consider as features
maxlen = 500 # cut texts after this number of words (among top max_featur
es most common words)
batch_size = 32
print('Loading data...')
(input_train, y_train), (input_test, y_test) = imdb.load_data(num_words=ma
x_features)
print(len(input_train), 'train sequences')
print(len(input_test), 'test sequences')
print('Pad sequences (samples x time)')
input_train = [Link].data_utils.pad_sequences(input_train, maxlen=max
len)
input_test = [Link].data_utils.pad_sequences(input_test, maxlen=maxle
n)
print('input_train shape:', input_train.shape)
print('input_test shape:', input_test.shape)
from [Link] import Dense
model = Sequential()
[Link](Embedding(max_features, 32))
[Link](SimpleRNN(32))
[Link](Dense(1, activation='sigmoid'))
[Link](optimizer='rmsprop', loss='binary_crossentropy', metrics=['a
cc'])
history = [Link](input_train, y_train,
epochs=10,
batch_size=128,
validation_split=0.2)
import [Link] as plt
acc = [Link]['acc']
val_acc = [Link]['val_acc']
loss = [Link]['loss']
val_loss = [Link]['val_loss']
epochs = range(len(acc))
[Link](epochs, acc, 'bo', label='Training acc')
[Link](epochs, val_acc, 'b', label='Validation acc')
[Link]('Training and validation accuracy')
[Link]()
[Link]()
[Link](epochs, loss, 'bo', label='Training loss')
[Link](epochs, val_loss, 'b', label='Validation loss')
[Link]('Training and validation loss')
[Link]()
[Link]()
from [Link] import LSTM
model = Sequential()
[Link](Embedding(max_features, 32))
[Link](LSTM(32))
[Link](Dense(1, activation='sigmoid'))
[Link](optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['acc'])
history = [Link](input_train, y_train,
epochs=10,
batch_size=128,
validation_split=0.2)
acc = [Link]['acc']
val_acc = [Link]['val_acc']
loss = [Link]['loss']
val_loss = [Link]['val_loss']
epochs = range(len(acc))
[Link](epochs, acc, 'bo', label='Training acc')
[Link](epochs, val_acc, 'b', label='Validation acc')
[Link]('Training and validation accuracy')
[Link]()
[Link]()
[Link](epochs, loss, 'bo', label='Training loss')
[Link](epochs, val_loss, 'b', label='Validation loss')
[Link]('Training and validation loss')
[Link]()
[Link]()