DEEP LEARNING






BEIT 2019 Pattern 2023

https://www.transfernow.net/dl/DLL 

 # Assignment 1


# Implementing Feedforward neural networks with Keras and TensorFlow

# a. Import the necessary packages

# b. Load the training and testing data (MNIST)

# c. Define the network architecture using Keras

# d. Train the model using SGD

# e. Evaluate the network

# f. Plot the training loss and accuracy



# Step 1: Import necessary packages

import numpy as np

import tensorflow as tf

from keras.datasets import mnist

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Flatten

import matplotlib.pyplot as plt


# Step 2: Load the CIFAR-10 dataset

(x_train, y_train), (x_test, y_test) = mnist.load_data()


# Step 3 : Normalize the pixel values to the range [0, 1]

x_train = x_train.astype('float32') / 255

x_test = x_test.astype('float32') / 255


# One-hot encode the labels

y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)

y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)


# Step 3: Define the network architecture

model = Sequential([

                                                                     # Flatten(input_shape=(32, 32, 3)),  # Adjust input shape for CIFAR-10

    Flatten(input_shape=(28,28)),  # Adjust input shape for mnist

    Dense(64, activation='relu'),

    Dense(64, activation='relu'),

    Dense(10, activation='softmax')

])


# Step 4: Compile the model

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


# Step 5: Train the model

history = model.fit(x_train, y_train, epochs=10, batch_size=32, validation_data=(x_test, y_test))


# Step 6: Evaluate the network

test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)

print(f'Test accuracy: {test_accuracy*100:.2f}%')


# Step 7: Plot the training loss and accuracy

plt.figure(figsize=(10, 4))


# Plot training & validation accuracy values

plt.subplot(1, 2, 1)

plt.plot(history.history['accuracy'])

plt.plot(history.history['val_accuracy'])

plt.title('Model accuracy')

plt.ylabel('Accuracy')

plt.xlabel('Epoch')

plt.legend(['Train', 'Test'], loc='upper left')


# Plot training & validation loss values

plt.subplot(1, 2, 2)

plt.plot(history.history['loss'])

plt.plot(history.history['val_loss'])

plt.title('Model loss')

plt.ylabel('Loss')

plt.xlabel('Epoch')

plt.legend(['Train', 'Test'], loc='upper left')


plt.tight_layout()

plt.show()


#--Assignment 2--


# Build the Image classification model by dividing the model into following 4 stages:

# a. Loading and preprocessing the image data

# b. Defining the model’s architecture

# c. Training the model

# d. Estimating the model’s performance

# Dataset: CIFAR10 




#importing required packages

import tensorflow as tf

from keras.models import Sequential

from keras.layers import Dense,Conv2D,Dropout,Flatten,MaxPooling2D

import matplotlib.pyplot as plt

import numpy as np


# a. laoding and preprocessing image data

cifar10=tf.keras.datasets.cifar10

(x_train,y_train),(x_test,y_test)=cifar10.load_data()

input_shape=(32,32,3)


#making sure that values are float so that we can get decimal points after division

x_train=x_train.reshape(x_train.shape[0], 32,32,3)

x_test=x_test.reshape(x_test.shape[0], 32,32,3)


print("Data type of x_train : " , x_train.dtype)

x_train=x_train.astype('float32')

x_test=x_test.astype('float32')

print("Datatype after converting : ", x_train.dtype)


#Normalizing the rgb codes by dividing it to the max rgb values

x_train=x_train / 255

x_test=x_test / 255


print("shape of training : ",x_train.shape)

print("shape of testing : " , x_test.shape)


# b. Defining the model architecture 

model = Sequential()

model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))

model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())

model.add(Dense(200,activation="relu"))

model.add(Dropout(0,3))

model.add(Dense(10,activation="softmax"))


model.summary()


# c.training the model

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

model.fit(x_train,y_train,epochs=2)



# d.estimating the models performance

test_loss,test_acc = model.evaluate(x_test,y_test)

print("Loss=%.3f" %test_loss)

print("Accuracy=%.3f" %test_acc)


#showing image at position[] from dataset: 

image=x_train[0]

plt.imshow(np.squeeze(image), cmap='gray')

plt.show()


#predicting the class of image

image=[image.reshape(1, image.shape[0], image.shape[1], image.shape[2])]

predict_model = model.predict([image])

print("Predicted Class: {}".format(np.argmax(predict_model)))


# Assignment 3

# Use Autoencoder to implement anomaly detection. Build the musing:

odel by # a. Import required libraries

# b. Upload/access the dataset

# c. An encoder converts it into latent representation

# d. Decoder networks convert it back to the original input

# e. Compile the models with Optimizer, Loss, and Evaluation Metrics

# Dataset: ECG 



import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

import tensorflow as tf

from tensorflow.keras.models import Model

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import MinMaxScaler, StandardScaler

mpl.rcParams['figure.figsize'] = (10, 5)

mpl.rcParams['axes.grid'] = False


!cat "/content/ECG5000_TRAIN.txt" "/content/ECG5000_TEST.txt" > ecg_final.txt

df = pd.read_csv("/content/ecg_final.txt", sep='  ', header=None, engine='python')

df.shape


df = df.add_prefix('c')

df['c0'].value_counts()


x_train, x_test, y_train, y_test = train_test_split(df.values, df.values[:,0:1], test_size=0.2, random_state=111)


scaler = MinMaxScaler()

data_scaled = scaler.fit(x_train)

train_data_scaled = data_scaled.transform(x_train)

test_data_scaled = data_scaled.transform(x_test)


normal_train_data = pd.DataFrame(train_data_scaled).add_prefix('c').query('c0 == 0').values[:,1:]

anomaly_train_data = pd.DataFrame(train_data_scaled).add_prefix('c').query('c0 > 0').values[:, 1:]

normal_test_data = pd.DataFrame(test_data_scaled).add_prefix('c').query('c0 == 0').values[:,1:]

anomaly_test_data = pd.DataFrame(test_data_scaled).add_prefix('c').query('c0 > 0').values[:, 1:]


plt.plot(normal_train_data[0])

plt.plot(normal_train_data[1])

plt.plot(normal_train_data[2])

plt.title("Normal Data")

plt.show()


#Assignment 4


# Implement the Continuous Bag of Words (CBOW) Model. Stages can be:  

# a. Data preparation  

# b. Generate training data  

# c. Train model  

# d. Output  

# Dataset : Corona


import numpy as np

import keras.backend as K #imp

from keras.models import Sequential

from keras.layers import Dense, Embedding, Lambda

import tensorflow as tf

from keras.utils import pad_sequences

from keras.preprocessing.text import Tokenizer, text_to_word_sequence

import gensim


data = open(r"/content/corona.txt")

data


corona_data = [text for text in data if text.count(' ') >= 2]

corona_data


vectorize = Tokenizer()

vectorize.fit_on_texts(corona_data)

corona_data = vectorize.texts_to_sequences(corona_data)


total_vocab = sum(len(s) for s in corona_data)

word_count = len(vectorize.index_word)+1

corona_data


print(total_vocab)

print(word_count)


window_size = 2


# b. Generate training data

# Defining utility to generate context word pairs

def cbow_model(data, window_size, total_vocab):

    total_length = window_size*2

    for text in data:

        text_len = len(text)

#         print("zero",text)

        for idx, word in enumerate(text):

#             print("first",idx,word)

            context_word = []

            target   = []

            begin = idx - window_size

            end = idx + window_size + 1

            context_word.append([

                text[i]

                for i in range(begin, end)

                if 0 <= i < text_len

                and i != idx

            ])

            target.append(word)

#             print("second",context_word,target)

            contextual = pad_sequences(

                context_word,

                maxlen=total_length

            )

            final_target = tf.keras.utils.to_categorical(

                target,

                total_vocab

            )

#             print("third",contextual,final_target)

            yield(contextual, final_target)


# c. train model

# Defining the model architecture

model = Sequential()

model.add(

    Embedding(

        input_dim=total_vocab,

        output_dim=100,

        input_length=window_size*2

    )

)

model.add(

    Lambda(

        lambda x: K.mean(x, axis=1),

        output_shape=(100,)

    )

)

model.add(

    Dense(

        total_vocab,

        activation='softmax'

    )

)



model.summary()

model.compile(

    loss='categorical_crossentropy',

    optimizer='adam'

)


for i in range(10):

    cost = 0

    for x, y in cbow_model(corona_data, window_size, total_vocab):

        cost += model.train_on_batch(x, y)

    print("Epoch ", i,"\t: ", cost)



dimensions = 100

vect_file = open('./vectors.txt','w')

vect_file.write('{} {}\n'.format(102, dimensions))


weights = model.get_weights()[0]

for text, i in vectorize.word_index.items():

    final_vec = ' '.join(map(str, list(weights[i, :])))

    vect_file.write('{} {}\n'.format(text, final_vec))

vect_file.close()


# d. Output


cbow_output = gensim.models.KeyedVectors.load_word2vec_format(

    'vectors.txt',

    binary=False

)

cbow_output.most_similar(positive=['speed'])


#--Assignment 5--


# Build the Image classification model by dividing the model into following 4 stages:

# a. Loading and preprocessing the image data

# b. Defining the model’s architecture

# c. Training the model

# d. Estimating the model’s performance

# Dataset: MNIST Handwritten  





#importing required packages

import tensorflow as tf

from keras.models import Sequential

from keras.layers import Dense,Conv2D,Dropout,Flatten,MaxPooling2D

import matplotlib.pyplot as plt

import numpy as np


# a. laoding and preprocessing image data

mnist=tf.keras.datasets.mnist

(x_train,y_train),(x_test,y_test)=mnist.load_data()

input_shape=(28,28,1)


#making sure that values are float so that we can get decimal points after division

x_train=x_train.reshape(x_train.shape[0], 28,28,1)

x_test=x_test.reshape(x_test.shape[0], 28,28,1)


print("Data type of x_train : " , x_train.dtype)

x_train=x_train.astype('float32')

x_test=x_test.astype('float32')

print("Datatype after converting : ", x_train.dtype)


#Normalizing the rgb codes by dividing it to the max rgb values

x_train=x_train / 255

x_test=x_test / 255


print("shape of training : ",x_train.shape)

print("shape of testing : " , x_test.shape)


# b. Defining the model architecture 

model = Sequential()

model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))

model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())

model.add(Dense(200,activation="relu"))

model.add(Dropout(0,3))

model.add(Dense(10,activation="softmax"))


model.summary()


# c.training the model

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

model.fit(x_train,y_train,epochs=2)



# d.estimating the models performance

test_loss,test_acc = model.evaluate(x_test,y_test)

print("Loss=%.3f" %test_loss)

print("Accuracy=%.3f" %test_acc)


#showing image at position[] from dataset: 

image=x_train[0]

plt.imshow(np.squeeze(image), cmap='gray')

plt.show()


#predicting the class of image

image=[image.reshape(1, image.shape[0], image.shape[1], image.shape[2])]

predict_model = model.predict([image])

print("Predicted Class: {}".format(np.argmax(predict_model)))


# Assignment 7


# Use Autoencoder to implement anomaly detection. Build the model by using:  

# a. Import required libraries  

# b. Upload/access the dataset  

# c. An encoder converts it into latent representation  

# d. Decoder networks convert it back to the original input  

# e. Compile the models with Optimizer, Loss, and Evaluation Metrics  

# Dataset: Credit Card --------------------------------


from sklearn.model_selection import train_test_split

import pandas as pd;

from sklearn.preprocessing import MinMaxScaler

from keras.models import Model, Sequential

from keras.layers import Dense, Dropout


raw_data = pd.read_csv('/content/creditcard.csv.xls')

raw_data


raw_data = raw_data.drop("Time",axis=1)


raw_data.Class.unique()

features = raw_data.drop("Class",axis=1)


x_train, x_test, y_train, y_test = train_test_split(features,raw_data['Class'],random_state=4,test_size=0.3)


train_data = x_train.loc[y_train[y_train==1].index]


minmax = MinMaxScaler(feature_range=(0, 1))

x_train_scaled = minmax.fit_transform(train_data)

x_test_scaled = minmax.transform(x_test)


class AutoEncoder(Model):

    def __init__(self,output_unit,ldim=8):

        super().__init__()

        self.encoder = Sequential([

#             Dense(64,activation='relu'),

#             Dropout(0.1),

#             Dense(32,activation='relu'),

#             Dropout(0.1),

            Dense(16,activation='relu'),

            Dropout(0.1),

            Dense(ldim,activation='relu')

        ])

        self.decoder = Sequential([

            Dense(16,activation='relu'),

#             Dropout(0.1),

#             Dense(32,activation='relu'),

#             Dropout(0.1),

#             Dense(64,activation='relu'),

            Dropout(0.1),

            Dense(output_unit,activation='sigmoid')

        ])

    def call(self,inputs):

        encoded = self.encoder(inputs)

        decoded = self.decoder(encoded)

        return decoded


model = AutoEncoder(output_unit =  x_train_scaled.shape[1])

model.compile(optimizer='adam',loss='msle',metrics=['mse'])

h = model.fit(

    x_train_scaled,

    x_train_scaled,

    validation_data=(x_test_scaled,x_test_scaled),epochs=20,batch_size=512

)

print(x_train_scaled.shape[1])



import matplotlib.pyplot as plt


plt.plot(h.history['loss'])

plt.plot(h.history['mse'])

plt.plot(h.history['val_loss'])

plt.plot(h.history['val_mse'])

plt.legend(['loss','val_loss'])

plt.xlabel('Epochs')

plt.ylabel('MSLE Loss')

plt.show()



def find_threshold(model,x_train_scaled):

    recons = model.predict(x_train_scaled)

    recons_error = tf.keras.metrics.msle(recons,x_train_scaled)

    threshold = np.mean(recons_error.numpy()) + np.std(recons_error.numpy())

    return threshold


def get_pred(model,x_test_scaled,threshold):

    pred = model.predict(x_test_scaled)

    error = tf.keras.metrics.msle(pred,x_test_scaled)

    AnomalyMask = pd.Series(error)>threshold

    return AnomalyMask.map(lambda x:0.0 if x==True else 1.0)



threshold = find_threshold(model,x_train_scaled)

print(threshold)


from sklearn.metrics import accuracy_score


pred = get_pred(model,x_test_scaled,threshold)


accuracy_score(pred,y_test)












Post a Comment

0 Comments