Advertisement
bunny007

Untitled

Nov 19th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.80 KB | None | 0 0
  1. ---------------------------------------------------------------------------------------------------------------
  2. ------------------------------------------------ PRACTICAL 2 ------------------------------------------
  3.  
  4. !pip install keras
  5.  
  6.  
  7. import numpy as np
  8. from keras.datasets import mnist
  9. from keras.models import Sequential
  10.  
  11.  
  12. from keras.layers import Dense
  13.  
  14.  
  15. #load the trining and training data
  16. (x_train, y_train), (x_test, y_test)=mnist.load_data()
  17.  
  18.  
  19. x_train.shape
  20.  
  21.  
  22. x_test.shape
  23.  
  24.  
  25. import matplotlib.pyplot as plt
  26.  
  27.  
  28. x= np.zeros(100).reshape(10,10)
  29.  
  30.  
  31. x
  32.  
  33.  
  34. plt.imshow(x,cmap ='gray')
  35.  
  36.  
  37. plt.imshow(x_train[200], cmap = 'gray')
  38.  
  39.  
  40. y_train[200]
  41.  
  42.  
  43. plt.imshow(x_train[220], cmap ='gray')
  44. y_train[220]
  45.  
  46.  
  47. x = np.array([[2,3,5] , [8,9,0]])
  48.  
  49. x
  50.  
  51. x.shape
  52.  
  53.  
  54. x= x.flatten()
  55.  
  56. x
  57.  
  58. img = x_train[5]
  59. img.shape
  60.  
  61. img = img.flatten()
  62. img.shape
  63.  
  64. x_train = x_train.reshape(60000,784)
  65. x_test = x_test.reshape (10000,784)
  66.  
  67. x = np.array([6,4,2,0,4,5])
  68.  
  69. x/6
  70.  
  71. x_train = x_train/255
  72. x_test = x_test/255
  73.  
  74. set(y_test)
  75.  
  76. import seaborn as sns
  77.  
  78. sns.countplot(x=y_train)
  79.  
  80. from collections import Counter
  81. Counter(y_train)
  82.  
  83. from keras.utils import to_categorical
  84.  
  85. x= [0,2,2,1,0,2]
  86.  
  87. to_categorical(x)
  88.  
  89. y_train = to_categorical (y_train)
  90. y_test = to_categorical(y_test)
  91.  
  92. y_train.shape
  93.  
  94. y_test.shape
  95.  
  96. **Define the Network Architecture**
  97.  
  98. #object of neural network
  99. model=Sequential()
  100. #imput layer
  101. model.add(Dense(784,input_shape=(784,), activation='relu'))
  102. #hidden layer
  103. model.add(Dense(256,activation='relu'))
  104. #output layer
  105. model.add(Dense(10,activation='softmax'))
  106.  
  107. model.summary()
  108.  
  109. **Compile the model**
  110.  
  111. model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  112.  
  113. **Train the model**
  114.  
  115. history=model.fit(x_train,y_train,epochs=10,batch_size=10)
  116.  
  117. history.history
  118.  
  119. plt.subplot(1,2,1)
  120. plt.plot(history.history['loss'],label='loss')
  121. plt.legend()
  122. plt.subplot(1,2,2)
  123. plt.plot(history.history['accuracy'],label='accuracy')
  124. plt.legend()
  125.  
  126. model.evaluate(x_test, y_test, batch_size=1)
  127.  
  128. -------------------------------------------------------------------------------------------------------------------------------------
  129. ------------------------------------------------------ PRACTICAL 3 ----------------------------------------------------------------
  130.  
  131. import numpy as np
  132. import matplotlib.pyplot as plt
  133. from tensorflow import keras
  134. from keras.datasets import cifar10
  135. from keras.models import Sequential
  136. from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
  137.  
  138.  
  139. (x_train, y_train),(x_test, y_test) = cifar10.load_data()
  140.  
  141.  
  142. x_train.shape
  143.  
  144.  
  145. **Explore the image data**
  146.  
  147. label= ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
  148.  
  149.  
  150. label
  151.  
  152.  
  153. # Display a 5x5 grid of random images from the training set with their
  154. # labels
  155. plt.figure(figsize=(12,12))
  156. for i in range(25):
  157. plt.subplot(5,5,i+1)
  158. plt.xticks([])
  159. plt.title(label[y_train[i][0]])
  160. plt.imshow(x_train[i])
  161.  
  162.  
  163. **pixel values of the images are scaled between 0 to 1 by dividing it by 255**
  164.  
  165. x_train=x_train/255
  166. x_test=x_test/255
  167.  
  168.  
  169. **One hot encoding**
  170.  
  171. from keras.utils import to_categorical
  172.  
  173.  
  174. y_train_new = to_categorical(y_train)
  175. y_test_new = to_categorical(y_test)
  176.  
  177.  
  178. y_train_new.shape
  179.  
  180.  
  181. **Build the Model**
  182.  
  183. # con2D -> 2D convolution layer
  184. # building block of CNN
  185. model= Sequential()
  186. model.add (Conv2D(filters=32, input_shape=(32,32,3), kernel_size=(3,3), activation='relu'))
  187.  
  188. # downsize it by maxpool->takes max value in a local region
  189. model.add(MaxPool2D(pool_size=(2,2)))
  190.  
  191. # regularization
  192. # randomly sets a fraction of our input units to 0 during training,
  193. # which helps in preventing overfitting
  194. model.add(Dropout(0.2))
  195.  
  196. # Add more conv layers
  197. model.add(Conv2D(filters=64,kernel_size=(3,3), activation='relu' ))
  198. model.add(MaxPool2D(pool_size=(2,2)))
  199.  
  200. # prepare the data for the fully connected layers
  201. model.add(Flatten())
  202.  
  203. # Add Fully connected layers
  204. # final dense layer has 10 units with softmax
  205. model.add(Dense(512, activation='relu'))
  206. model.add(Dense(10,activation='softmax'))
  207.  
  208.  
  209. model.summary()
  210.  
  211.  
  212. **Complie the model**
  213.  
  214. model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])
  215.  
  216.  
  217. **Train the model**
  218.  
  219. model.fit(x_train, y_train_new, epochs=10,batch_size=20)
  220.  
  221.  
  222. model.fit(x_train,y_train_new, epochs=15, batch_size=20)
  223.  
  224. ---------------------------------------------------------------------------------------------------------------------------------
  225. ---------------------------------------- PRACTICAL 4 ------------------------------------------------------------------------
  226.  
  227. import numpy as np
  228. import pandas as pd
  229. import tensorflow as tf
  230. import matplotlib.pyplot as plt
  231. from sklearn.metrics import accuracy_score
  232. from tensorflow.keras.optimizers import Adam
  233. from sklearn.preprocessing import MinMaxScaler
  234. from tensorflow.keras import Model, Sequential
  235. from tensorflow.keras.layers import Dense, Dropout
  236. from sklearn.model_selection import train_test_split
  237. from tensorflow.keras.losses import MeanSquaredLogarithmicError
  238.  
  239.  
  240. ## Load the data
  241. # Download the dataset
  242. path = '''http://storage.googleapis.com/
  243. download.tensorflow.org/data/ecg.csv'''
  244. data = pd.read_csv('http://storage.googleapis.com/download.tensorflow.org/data/ecg.csv', header=None)
  245. print(data.shape)
  246. data.head()
  247.  
  248.  
  249. ## Split the data for training and testing
  250.  
  251. # last column is the target
  252. # 0 = anomaly, 1 = normal
  253. TARGET = 140
  254.  
  255. features = data.drop(TARGET, axis=1)
  256. target = data[TARGET]
  257.  
  258. x_train, x_test, y_train, y_test = train_test_split(
  259. features, target, test_size=0.2,
  260. random_state = 0, stratify=target
  261. )
  262.  
  263.  
  264. x_test.shape
  265.  
  266.  
  267. x_train.shape
  268.  
  269.  
  270. target.value_counts()
  271.  
  272.  
  273. # use case is novelty detection so use only the normal data
  274. # for training
  275. train_index = y_train[y_train == 1].index
  276. train_data = x_train.loc[train_index]
  277.  
  278.  
  279. ## Scale the data using MinMaxScaler
  280.  
  281. # transform the data to a range between 0 and 1
  282.  
  283. min_max_scaler = MinMaxScaler()
  284. x_train_scaled = min_max_scaler.fit_transform(
  285. train_data.copy())
  286. x_test_scaled = min_max_scaler.transform(x_test.copy())
  287.  
  288.  
  289. x_train.describe()
  290.  
  291.  
  292. pd.DataFrame(x_train_scaled).describe()
  293.  
  294.  
  295. ## Build an AutoEncoder model
  296.  
  297. # create a model by subclassing Model class in tensorflow
  298.  
  299. '''define an AutoEncoder class that inherits from TensorFlow's
  300. Model class. It has an encoder and a decoder. The encoder part takes
  301. the input data and compresses it into a lower-dimensional representation
  302. (code). The decoder then tries to reconstruct the original data
  303. from this code.
  304. '''
  305. class AutoEncoder(Model):
  306. """
  307. Parameters
  308. ----------
  309. output_units: int
  310. Number of output units
  311.  
  312. code_size: int
  313. Number of units in bottle neck
  314. """
  315.  
  316. def __init__(self, output_units, code_size=8):
  317. super().__init__()
  318. self.encoder = Sequential([
  319. Dense(64, activation='relu'),
  320. Dropout(0.1),
  321. Dense(32, activation='relu'),
  322. Dropout(0.1),
  323. Dense(16, activation='relu'),
  324. Dropout(0.1),
  325. Dense(code_size, activation='relu')
  326. ])
  327.  
  328. '''The encoder is responsible for compressing the input data
  329. into a lower-dimensional representation (code).
  330. '''
  331. # Dropout layers are added after each Dense layer
  332.  
  333. self.decoder = Sequential([
  334. Dense(16, activation='relu'),
  335. Dropout(0.1),
  336. Dense(32, activation='relu'),
  337. Dropout(0.1),
  338. Dense(64, activation='relu'),
  339. Dropout(0.1),
  340. Dense(output_units, activation='sigmoid')
  341. ])
  342.  
  343. '''The decoder is responsible for reconstructing the original
  344. data from the compressed representation (code).
  345. '''
  346.  
  347. def call(self, inputs):
  348. encoded = self.encoder(inputs)
  349. decoded = self.decoder(encoded)
  350. return decoded
  351.  
  352.  
  353.  
  354.  
  355. model = AutoEncoder(output_units=x_train_scaled.shape[1])
  356. # configurations of model
  357. model.compile(loss='msle', metrics=['mse'], optimizer='adam')
  358.  
  359. history = model.fit(
  360. x_train_scaled,
  361. x_train_scaled,
  362. epochs=20,
  363. batch_size=512,
  364. validation_data=(x_test_scaled, x_test_scaled)
  365. )
  366.  
  367.  
  368.  
  369. ## Plot history
  370.  
  371. plt.plot(history.history['loss'])
  372. plt.plot(history.history['val_loss'])
  373. plt.xlabel('Epochs')
  374. plt.ylabel('MSLE Loss')
  375. plt.legend(['loss', 'val_loss'])
  376. plt.show()
  377.  
  378. --------------------------------------------------------------------------------------------------------------------------------
  379. ---------------------------------------------------- PRACTICAL 5 --------------------------------------------------------------
  380.  
  381. from google.colab import drive
  382. drive.mount('/content/drive')
  383. # if your file is in drive
  384.  
  385.  
  386. import string
  387. import re
  388. from os import listdir
  389. from nltk.corpus import stopwords
  390. from keras.preprocessing.text import Tokenizer
  391. from keras.utils import plot_model
  392. from keras.models import Sequential
  393. from keras.layers import Dense, Flatten, Embedding
  394. import numpy as np
  395. import nltk
  396. from keras.layers import Conv1D, MaxPooling1D
  397. nltk.download('stopwords')
  398.  
  399.  
  400. # Data download link
  401. # https://www.cs.cornell.edu/people/pabo/movie-review-data/review_polarity.tar.gz
  402.  
  403. # load doc into memory
  404. def load_doc(filename):
  405. # open the file as read only
  406. file = open(filename, 'r' )
  407. # read all text
  408. text = file.read()
  409. # close the file
  410. file.close()
  411. return text
  412.  
  413. # loading a single review
  414. text = load_doc('drive/MyDrive/review_polarity
  415. /txt_sentoken/pos/cv026_29325.txt')
  416.  
  417. text
  418.  
  419. # turn a doc into clean tokens
  420. def clean_doc(doc):
  421. # split into tokens by white space
  422. tokens = doc.split()
  423. # prepare regex for char filtering
  424. re_punc = re.compile( '[%s]' % re.escape(string.punctuation))
  425. # remove punctuation from each word
  426. tokens = [re_punc.sub( '' , w) for w in tokens]
  427. # remove remaining tokens that are not alphabetic
  428. tokens = [word for word in tokens if word.isalpha()]
  429. # filter out stop words
  430. stop_words = set(stopwords.words( 'english' ))
  431. tokens = [w for w in tokens if not w in stop_words]
  432. # filter out short tokens
  433. tokens = [word for word in tokens if len(word) > 1]
  434. return tokens
  435.  
  436. clean_doc(text)
  437.  
  438. # load doc, clean and return line of tokens
  439. def doc_to_line(filename, vocab):
  440. # load the doc
  441. doc = load_doc(filename)
  442. # clean doc
  443. tokens = clean_doc(doc)
  444. # filter by vocab
  445. tokens = [w for w in tokens if w in vocab]
  446. return ' ' .join(tokens)
  447.  
  448. vocab = open('vocab.txt')
  449. vocab = vocab.read().split()
  450.  
  451. text = doc_to_line('drive/MyDrive/review_polarity/txt_sentoken/pos/cv026_29325.txt',
  452. vocab)
  453.  
  454. text
  455.  
  456. # load all docs in a directory
  457. def process_train(directory, vocab):
  458. documents = list()
  459. for filename in listdir(directory):
  460. if not filename.startswith( 'cv9' ):
  461. path = directory + '/' + filename
  462. doc = load_doc(path)
  463. tokens = clean_doc(doc, vocab)
  464. documents.append(tokens)
  465. return documents
  466.  
  467. def process_test(directory, vocab):
  468. documents = list()
  469. for filename in listdir(directory):
  470. if filename.startswith( 'cv9' ):
  471. path = directory + '/' + filename
  472. doc = load_doc(path)
  473. tokens = clean_doc(doc, vocab)
  474. documents.append(tokens)
  475. return documents
  476.  
  477. # load all docs in a directory
  478. def process_docs(directory, vocab, is_train):
  479. documents = list()
  480. # walk through all files in the folder
  481. for filename in listdir(directory):
  482. # skip any reviews in the test set
  483. if is_train and filename.startswith( 'cv9' ):
  484. continue
  485. if not is_train and not filename.startswith( 'cv9' ):
  486. continue
  487. # create the full path of the file to open
  488. path = directory + '/' + filename
  489. # load the doc
  490. doc = load_doc(path)
  491. # clean doc
  492. tokens = clean_doc(doc)
  493. # add to list
  494. documents.append(tokens)
  495. return documents
  496.  
  497. lines = process_docs('drive/MyDrive/review_polarity/txt_sentoken/pos',
  498. vocab, False)
  499.  
  500. len(lines)
  501.  
  502. # load and clean a dataset
  503. def load_clean_dataset(vocab, is_train):
  504. # load documents
  505. neg = process_docs('drive/MyDrive/review_polarity/txt_sentoken/neg', vocab, is_train)
  506. pos = process_docs('drive/MyDrive/review_polarity/txt_sentoken/pos', vocab, is_train)
  507. docs = neg + pos
  508. # prepare labels
  509. labels = [0 for _ in range(len(neg))] + [1 for _ in range(len(pos))]
  510. return docs, labels
  511.  
  512. train, train_labels = load_clean_dataset(vocab, True)
  513. test, test_labels = load_clean_dataset(vocab, False)
  514.  
  515. len(train), len(test)
  516.  
  517. from collections import Counter
  518. Counter(l)
  519.  
  520. Build the DNN model
  521.  
  522. # define the model
  523. def define_model(n_words):
  524. # define network
  525. model = Sequential()
  526. model.add(Dense(50, input_shape=(n_words,), activation= 'relu' ))
  527. model.add(Dense(1, activation= 'sigmoid' ))
  528. # compile network
  529. model.compile(loss= 'binary_crossentropy' , optimizer= 'adam' ,
  530. metrics=[ 'accuracy' ])
  531. # summarize defined model
  532. model.summary()
  533. plot_model(model, to_file= 'model.png' , show_shapes=True)
  534. return model
  535.  
  536. # fit a tokenizer
  537. def create_tokenizer(lines):
  538. tokenizer = Tokenizer()
  539. tokenizer.fit_on_texts(lines)
  540. return tokenizer
  541.  
  542. # create the tokenizer
  543. tokenizer = create_tokenizer(d)
  544.  
  545. # encode data
  546. x_train = tokenizer.texts_to_matrix(d, mode= 'binary' )
  547.  
  548. x_train
  549.  
  550. x_train.shape
  551.  
  552. tokenizer.word_docs
  553.  
  554. # define network
  555. n_words = x_train.shape[1]
  556. model = define_model(n_words)
  557.  
  558. Train the model
  559.  
  560. # fit network
  561. model.fit(x_train, np.array(l), epochs=10, batch_size=10)
  562.  
  563. plot_model(model, show_dtype=True, show_layer_activations=True,
  564. show_shapes=True, show_layer_names=True )
  565.  
  566. # classify a review as negative or positive
  567. def predict_sentiment(review):
  568. # clean
  569. tokens = clean_doc(review)
  570. # filter by vocab
  571. tokens = [w for w in tokens if w in vocab]
  572. # convert to line
  573. line = ' ' .join(tokens)
  574. # encode
  575. encoded = tokenizer.texts_to_matrix([line], mode= 'binary' )
  576. # predict sentiment
  577. yhat = model.predict(encoded, verbose=0)
  578. # retrieve predicted percentage and label
  579. percent_pos = yhat[0,0]
  580. if round(percent_pos) == 0:
  581. return (1-percent_pos), 'NEGATIVE'
  582. return percent_pos, 'POSITIVE'
  583.  
  584. # test positive text
  585. text = ' Best movie ever! It was great, I will definitely recommend it. '
  586. percent, sentiment = predict_sentiment(text)
  587. print( ' Review: [%s]\nSentiment: %s (%.3f%%) ' % (text, sentiment, percent*100))
  588.  
  589. # test negative text
  590. text = ' This is a bad movie. '
  591. percent, sentiment = predict_sentiment(text)
  592. print( ' Review: [%s]\nSentiment: %s (%.3f%%) ' % (text, sentiment, percent*100))
  593.  
  594. ---------------------------------------------------------------------------------------------------------------------------------
  595. -------------------------------------------- PRACTICAL 6 -----------------------------------------------------------------------
  596.  
  597. '''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'''
  598.  
  599. import tensorflow as tf
  600. from tensorflow.keras.applications import ResNet50
  601. from tensorflow.keras.models import Sequential, Model
  602. from tensorflow.keras.layers import Dense, Flatten
  603.  
  604.  
  605. model = ResNet50()
  606. # pretrainned for image classification
  607.  
  608.  
  609. # freeze the weights
  610.  
  611. model.trainable= False
  612. # sets all the layers in the resnet50 model to
  613. # non_trainable.
  614. # only layers you add on top will learn
  615.  
  616.  
  617. model.summary()
  618.  
  619.  
  620. # dont want to include the final fully connected layers
  621.  
  622. resnet50= ResNet50(include_top=False,weights='imagenet', input_shape=(150,150,3))
  623.  
  624. # flatten
  625. flattened = Flatten()(resnet50.output)
  626.  
  627. # fully connected layer 1
  628. fc1 = Dense(128,activation='relu', name='AddedDense1')(flattened)
  629.  
  630. # fully connected layer 2
  631. fc2 = Dense(10,activation='softmax', name='AddedDense2')(fc1)
  632.  
  633. model=Model(inputs=resnet50.inputs, outputs=fc2)
  634.  
  635.  
  636.  
  637. resnet50.summary()
  638.  
  639.  
  640. model.summary()
  641.  
  642. -------------------------------------------------------------------------------------------------------------------------------------
  643.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement