0% found this document useful (0 votes)
15 views5 pages

ML Lab Test 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

ML Lab Test 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program 1

# first neural network with keras tutorial

from numpy import loadtxt

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense

# load the dataset

dataset = loadtxt('diabetes.csv', skiprows=1, delimiter=',')

# split into input (X) and output (y) variables

X = dataset[:,0:8]

y = dataset[:,8]

# define the keras model

model = Sequential()

model.add(Dense(12, input_shape=(8,), activation='relu'))

model.add(Dense(8, activation='relu'))

model.add(Dense(1, activation='sigmoid'))

# compile the keras model

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

# fit the keras model on the dataset

model.fit(X, y, epochs=150, batch_size=10)

# evaluate the keras model

_, accuracy = model.evaluate(X, y)

print('Accuracy: %.2f' % (accuracy*100))

program 2

import numpy as np

import matplotlib.pyplot as plt

from keras.layers import Input, Dense

from keras.models import Model

from keras.datasets import mnist

# Load the MNIST dataset

(x_train, _), (x_test, _) = mnist.load_data()


# Preprocess the data

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

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

x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))

x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))

# Define the autoencoder model

input_img = Input(shape=(784,))

encoded = Dense(32, activation='relu')(input_img) # Encoding layer with 32 units

decoded = Dense(784, activation='sigmoid')(encoded) # Decoding layer

autoencoder = Model(input_img, decoded)

autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

# Train the autoencoder

autoencoder.fit(x_train, x_train, epochs=50, batch_size=256, shuffle=True, validation_data=(x_test,


x_test))

# Encode and decode the test images

encoded_imgs = autoencoder.predict(x_test)

decoded_imgs = encoded_imgs

# Visualize original and reconstructed images

n = 10 # Number of images to display

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

for i in range(n):

# Original images

ax = plt.subplot(2, n, i + 1)

plt.imshow(x_test[i].reshape(28, 28))

plt.gray()

ax.get_xaxis().set_visible(False)

ax.get_yaxis().set_visible(False)

# Reconstructed images

ax = plt.subplot(2, n, i + 1 + n)

plt.imshow(decoded_imgs[i].reshape(28, 28))

plt.gray()
ax.get_xaxis().set_visible(False)

ax.get_yaxis().set_visible(False)

plt.show()

program 3

#Simple MNIST convnet

import numpy as np

from tensorflow import keras

from tensorflow.keras import layers

# Model / data parameters

num_classes = 10

input_shape = (28, 28, 1)

# Load the data and split it between train and test sets

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

# Scale images to the [0, 1] range

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

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

# Make sure images have shape (28, 28, 1)

x_train = np.expand_dims(x_train, -1)

x_test = np.expand_dims(x_test, -1)

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

print(x_train.shape[0], "train samples")

print(x_test.shape[0], "test samples")

# convert class vectors to binary class matrices

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

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

model = keras.Sequential(

keras.Input(shape=input_shape),

layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),

layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),

layers.MaxPooling2D(pool_size=(2, 2)),

layers.Flatten(),

layers.Dropout(0.5),

layers.Dense(num_classes, activation="softmax"),

model.summary()

batch_size = 128

epochs = 15

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)

score = model.evaluate(x_test, y_test, verbose=0)

print("Test loss:", score[0])

print("Test accuracy:", score[1])

program 5

# Python program to generate word vectors using Word2Vec

# importing all necessary modules

from nltk.tokenize import sent_tokenize, word_tokenize

import warnings

warnings.filterwarnings(action = 'ignore')

import gensim

from gensim.models import Word2Vec

# Reads ‘alice.txt’ file

sample = open("C:\\Users\\Admin\\Desktop\\alice.txt", "utf8")

s = sample.read()

# Replaces escape character with space

f = s.replace("\n", " ")

data = []

# iterate through each sentence in the file


for i in sent_tokenize(f):

temp = []

# tokenize the sentence into words

for j in word_tokenize(i):

temp.append(j.lower())

data.append(temp)

# Create CBOW model

model1 = gensim.models.Word2Vec(data, min_count = 1,

vector_size = 100, window = 5)

# Print results

print("Cosine similarity between 'alice' " +

"and 'wonderland' - CBOW : ",

model1.wv.similarity('alice', 'wonderland'))

print("Cosine similarity between 'alice' " +

"and 'machines' - CBOW : ",

model1.wv.similarity('alice', 'machines'))

# Create Skip Gram model

model2 = gensim.models.Word2Vec(data, min_count = 1, vector_size = 100,

window = 5, sg = 1)

# Print results

print("Cosine similarity between 'alice' " +

"and 'wonderland' - Skip Gram : ",

model2.wv.similarity('alice', 'wonderland'))

print("Cosine similarity between 'alice' " +

"and 'machines' - Skip Gram : ",

model2.wv.similarity('alice', 'machines'))

You might also like