Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------------------------------------------------------
- ------------------------------------------------ PRACTICAL 2 ------------------------------------------
- !pip install keras
- import numpy as np
- from keras.datasets import mnist
- from keras.models import Sequential
- from keras.layers import Dense
- #load the trining and training data
- (x_train, y_train), (x_test, y_test)=mnist.load_data()
- x_train.shape
- x_test.shape
- import matplotlib.pyplot as plt
- x= np.zeros(100).reshape(10,10)
- x
- plt.imshow(x,cmap ='gray')
- plt.imshow(x_train[200], cmap = 'gray')
- y_train[200]
- plt.imshow(x_train[220], cmap ='gray')
- y_train[220]
- x = np.array([[2,3,5] , [8,9,0]])
- x
- x.shape
- x= x.flatten()
- x
- img = x_train[5]
- img.shape
- img = img.flatten()
- img.shape
- x_train = x_train.reshape(60000,784)
- x_test = x_test.reshape (10000,784)
- x = np.array([6,4,2,0,4,5])
- x/6
- x_train = x_train/255
- x_test = x_test/255
- set(y_test)
- import seaborn as sns
- sns.countplot(x=y_train)
- from collections import Counter
- Counter(y_train)
- from keras.utils import to_categorical
- x= [0,2,2,1,0,2]
- to_categorical(x)
- y_train = to_categorical (y_train)
- y_test = to_categorical(y_test)
- y_train.shape
- y_test.shape
- **Define the Network Architecture**
- #object of neural network
- model=Sequential()
- #imput layer
- model.add(Dense(784,input_shape=(784,), activation='relu'))
- #hidden layer
- model.add(Dense(256,activation='relu'))
- #output layer
- model.add(Dense(10,activation='softmax'))
- model.summary()
- **Compile the model**
- model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
- **Train the model**
- history=model.fit(x_train,y_train,epochs=10,batch_size=10)
- history.history
- plt.subplot(1,2,1)
- plt.plot(history.history['loss'],label='loss')
- plt.legend()
- plt.subplot(1,2,2)
- plt.plot(history.history['accuracy'],label='accuracy')
- plt.legend()
- model.evaluate(x_test, y_test, batch_size=1)
- -------------------------------------------------------------------------------------------------------------------------------------
- ------------------------------------------------------ PRACTICAL 3 ----------------------------------------------------------------
- import numpy as np
- import matplotlib.pyplot as plt
- from tensorflow import keras
- from keras.datasets import cifar10
- from keras.models import Sequential
- from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
- (x_train, y_train),(x_test, y_test) = cifar10.load_data()
- x_train.shape
- **Explore the image data**
- label= ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
- label
- # Display a 5x5 grid of random images from the training set with their
- # labels
- plt.figure(figsize=(12,12))
- for i in range(25):
- plt.subplot(5,5,i+1)
- plt.xticks([])
- plt.title(label[y_train[i][0]])
- plt.imshow(x_train[i])
- **pixel values of the images are scaled between 0 to 1 by dividing it by 255**
- x_train=x_train/255
- x_test=x_test/255
- **One hot encoding**
- from keras.utils import to_categorical
- y_train_new = to_categorical(y_train)
- y_test_new = to_categorical(y_test)
- y_train_new.shape
- **Build the Model**
- # con2D -> 2D convolution layer
- # building block of CNN
- model= Sequential()
- model.add (Conv2D(filters=32, input_shape=(32,32,3), kernel_size=(3,3), activation='relu'))
- # downsize it by maxpool->takes max value in a local region
- model.add(MaxPool2D(pool_size=(2,2)))
- # regularization
- # randomly sets a fraction of our input units to 0 during training,
- # which helps in preventing overfitting
- model.add(Dropout(0.2))
- # Add more conv layers
- model.add(Conv2D(filters=64,kernel_size=(3,3), activation='relu' ))
- model.add(MaxPool2D(pool_size=(2,2)))
- # prepare the data for the fully connected layers
- model.add(Flatten())
- # Add Fully connected layers
- # final dense layer has 10 units with softmax
- model.add(Dense(512, activation='relu'))
- model.add(Dense(10,activation='softmax'))
- model.summary()
- **Complie the model**
- model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])
- **Train the model**
- model.fit(x_train, y_train_new, epochs=10,batch_size=20)
- model.fit(x_train,y_train_new, epochs=15, batch_size=20)
- ---------------------------------------------------------------------------------------------------------------------------------
- ---------------------------------------- PRACTICAL 4 ------------------------------------------------------------------------
- import numpy as np
- import pandas as pd
- import tensorflow as tf
- import matplotlib.pyplot as plt
- from sklearn.metrics import accuracy_score
- from tensorflow.keras.optimizers import Adam
- from sklearn.preprocessing import MinMaxScaler
- from tensorflow.keras import Model, Sequential
- from tensorflow.keras.layers import Dense, Dropout
- from sklearn.model_selection import train_test_split
- from tensorflow.keras.losses import MeanSquaredLogarithmicError
- ## Load the data
- # Download the dataset
- path = '''http://storage.googleapis.com/
- download.tensorflow.org/data/ecg.csv'''
- data = pd.read_csv('http://storage.googleapis.com/download.tensorflow.org/data/ecg.csv', header=None)
- print(data.shape)
- data.head()
- ## Split the data for training and testing
- # last column is the target
- # 0 = anomaly, 1 = normal
- TARGET = 140
- features = data.drop(TARGET, axis=1)
- target = data[TARGET]
- x_train, x_test, y_train, y_test = train_test_split(
- features, target, test_size=0.2,
- random_state = 0, stratify=target
- )
- x_test.shape
- x_train.shape
- target.value_counts()
- # use case is novelty detection so use only the normal data
- # for training
- train_index = y_train[y_train == 1].index
- train_data = x_train.loc[train_index]
- ## Scale the data using MinMaxScaler
- # transform the data to a range between 0 and 1
- min_max_scaler = MinMaxScaler()
- x_train_scaled = min_max_scaler.fit_transform(
- train_data.copy())
- x_test_scaled = min_max_scaler.transform(x_test.copy())
- x_train.describe()
- pd.DataFrame(x_train_scaled).describe()
- ## Build an AutoEncoder model
- # create a model by subclassing Model class in tensorflow
- '''define an AutoEncoder class that inherits from TensorFlow's
- Model class. It has an encoder and a decoder. The encoder part takes
- the input data and compresses it into a lower-dimensional representation
- (code). The decoder then tries to reconstruct the original data
- from this code.
- '''
- class AutoEncoder(Model):
- """
- Parameters
- ----------
- output_units: int
- Number of output units
- code_size: int
- Number of units in bottle neck
- """
- def __init__(self, output_units, code_size=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(code_size, activation='relu')
- ])
- '''The encoder is responsible for compressing the input data
- into a lower-dimensional representation (code).
- '''
- # Dropout layers are added after each Dense layer
- 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_units, activation='sigmoid')
- ])
- '''The decoder is responsible for reconstructing the original
- data from the compressed representation (code).
- '''
- def call(self, inputs):
- encoded = self.encoder(inputs)
- decoded = self.decoder(encoded)
- return decoded
- model = AutoEncoder(output_units=x_train_scaled.shape[1])
- # configurations of model
- model.compile(loss='msle', metrics=['mse'], optimizer='adam')
- history = model.fit(
- x_train_scaled,
- x_train_scaled,
- epochs=20,
- batch_size=512,
- validation_data=(x_test_scaled, x_test_scaled)
- )
- ## Plot history
- plt.plot(history.history['loss'])
- plt.plot(history.history['val_loss'])
- plt.xlabel('Epochs')
- plt.ylabel('MSLE Loss')
- plt.legend(['loss', 'val_loss'])
- plt.show()
- --------------------------------------------------------------------------------------------------------------------------------
- ---------------------------------------------------- PRACTICAL 5 --------------------------------------------------------------
- from google.colab import drive
- drive.mount('/content/drive')
- # if your file is in drive
- import string
- import re
- from os import listdir
- from nltk.corpus import stopwords
- from keras.preprocessing.text import Tokenizer
- from keras.utils import plot_model
- from keras.models import Sequential
- from keras.layers import Dense, Flatten, Embedding
- import numpy as np
- import nltk
- from keras.layers import Conv1D, MaxPooling1D
- nltk.download('stopwords')
- # Data download link
- # https://www.cs.cornell.edu/people/pabo/movie-review-data/review_polarity.tar.gz
- # load doc into memory
- def load_doc(filename):
- # open the file as read only
- file = open(filename, 'r' )
- # read all text
- text = file.read()
- # close the file
- file.close()
- return text
- # loading a single review
- text = load_doc('drive/MyDrive/review_polarity
- /txt_sentoken/pos/cv026_29325.txt')
- text
- # turn a doc into clean tokens
- def clean_doc(doc):
- # split into tokens by white space
- tokens = doc.split()
- # prepare regex for char filtering
- re_punc = re.compile( '[%s]' % re.escape(string.punctuation))
- # remove punctuation from each word
- tokens = [re_punc.sub( '' , w) for w in tokens]
- # remove remaining tokens that are not alphabetic
- tokens = [word for word in tokens if word.isalpha()]
- # filter out stop words
- stop_words = set(stopwords.words( 'english' ))
- tokens = [w for w in tokens if not w in stop_words]
- # filter out short tokens
- tokens = [word for word in tokens if len(word) > 1]
- return tokens
- clean_doc(text)
- # load doc, clean and return line of tokens
- def doc_to_line(filename, vocab):
- # load the doc
- doc = load_doc(filename)
- # clean doc
- tokens = clean_doc(doc)
- # filter by vocab
- tokens = [w for w in tokens if w in vocab]
- return ' ' .join(tokens)
- vocab = open('vocab.txt')
- vocab = vocab.read().split()
- text = doc_to_line('drive/MyDrive/review_polarity/txt_sentoken/pos/cv026_29325.txt',
- vocab)
- text
- # load all docs in a directory
- def process_train(directory, vocab):
- documents = list()
- for filename in listdir(directory):
- if not filename.startswith( 'cv9' ):
- path = directory + '/' + filename
- doc = load_doc(path)
- tokens = clean_doc(doc, vocab)
- documents.append(tokens)
- return documents
- def process_test(directory, vocab):
- documents = list()
- for filename in listdir(directory):
- if filename.startswith( 'cv9' ):
- path = directory + '/' + filename
- doc = load_doc(path)
- tokens = clean_doc(doc, vocab)
- documents.append(tokens)
- return documents
- # load all docs in a directory
- def process_docs(directory, vocab, is_train):
- documents = list()
- # walk through all files in the folder
- for filename in listdir(directory):
- # skip any reviews in the test set
- if is_train and filename.startswith( 'cv9' ):
- continue
- if not is_train and not filename.startswith( 'cv9' ):
- continue
- # create the full path of the file to open
- path = directory + '/' + filename
- # load the doc
- doc = load_doc(path)
- # clean doc
- tokens = clean_doc(doc)
- # add to list
- documents.append(tokens)
- return documents
- lines = process_docs('drive/MyDrive/review_polarity/txt_sentoken/pos',
- vocab, False)
- len(lines)
- # load and clean a dataset
- def load_clean_dataset(vocab, is_train):
- # load documents
- neg = process_docs('drive/MyDrive/review_polarity/txt_sentoken/neg', vocab, is_train)
- pos = process_docs('drive/MyDrive/review_polarity/txt_sentoken/pos', vocab, is_train)
- docs = neg + pos
- # prepare labels
- labels = [0 for _ in range(len(neg))] + [1 for _ in range(len(pos))]
- return docs, labels
- train, train_labels = load_clean_dataset(vocab, True)
- test, test_labels = load_clean_dataset(vocab, False)
- len(train), len(test)
- from collections import Counter
- Counter(l)
- Build the DNN model
- # define the model
- def define_model(n_words):
- # define network
- model = Sequential()
- model.add(Dense(50, input_shape=(n_words,), activation= 'relu' ))
- model.add(Dense(1, activation= 'sigmoid' ))
- # compile network
- model.compile(loss= 'binary_crossentropy' , optimizer= 'adam' ,
- metrics=[ 'accuracy' ])
- # summarize defined model
- model.summary()
- plot_model(model, to_file= 'model.png' , show_shapes=True)
- return model
- # fit a tokenizer
- def create_tokenizer(lines):
- tokenizer = Tokenizer()
- tokenizer.fit_on_texts(lines)
- return tokenizer
- # create the tokenizer
- tokenizer = create_tokenizer(d)
- # encode data
- x_train = tokenizer.texts_to_matrix(d, mode= 'binary' )
- x_train
- x_train.shape
- tokenizer.word_docs
- # define network
- n_words = x_train.shape[1]
- model = define_model(n_words)
- Train the model
- # fit network
- model.fit(x_train, np.array(l), epochs=10, batch_size=10)
- plot_model(model, show_dtype=True, show_layer_activations=True,
- show_shapes=True, show_layer_names=True )
- # classify a review as negative or positive
- def predict_sentiment(review):
- # clean
- tokens = clean_doc(review)
- # filter by vocab
- tokens = [w for w in tokens if w in vocab]
- # convert to line
- line = ' ' .join(tokens)
- # encode
- encoded = tokenizer.texts_to_matrix([line], mode= 'binary' )
- # predict sentiment
- yhat = model.predict(encoded, verbose=0)
- # retrieve predicted percentage and label
- percent_pos = yhat[0,0]
- if round(percent_pos) == 0:
- return (1-percent_pos), 'NEGATIVE'
- return percent_pos, 'POSITIVE'
- # test positive text
- text = ' Best movie ever! It was great, I will definitely recommend it. '
- percent, sentiment = predict_sentiment(text)
- print( ' Review: [%s]\nSentiment: %s (%.3f%%) ' % (text, sentiment, percent*100))
- # test negative text
- text = ' This is a bad movie. '
- percent, sentiment = predict_sentiment(text)
- print( ' Review: [%s]\nSentiment: %s (%.3f%%) ' % (text, sentiment, percent*100))
- ---------------------------------------------------------------------------------------------------------------------------------
- -------------------------------------------- PRACTICAL 6 -----------------------------------------------------------------------
- '''Transfer learning model that has been trained on one task is fine-tuned or used as a starting point for a different but related task'''
- import tensorflow as tf
- from tensorflow.keras.applications import ResNet50
- from tensorflow.keras.models import Sequential, Model
- from tensorflow.keras.layers import Dense, Flatten
- model = ResNet50()
- # pretrainned for image classification
- # freeze the weights
- model.trainable= False
- # sets all the layers in the resnet50 model to
- # non_trainable.
- # only layers you add on top will learn
- model.summary()
- # dont want to include the final fully connected layers
- resnet50= ResNet50(include_top=False,weights='imagenet', input_shape=(150,150,3))
- # flatten
- flattened = Flatten()(resnet50.output)
- # fully connected layer 1
- fc1 = Dense(128,activation='relu', name='AddedDense1')(flattened)
- # fully connected layer 2
- fc2 = Dense(10,activation='softmax', name='AddedDense2')(fc1)
- model=Model(inputs=resnet50.inputs, outputs=fc2)
- resnet50.summary()
- model.summary()
- -------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement