0% found this document useful (0 votes)
7 views4 pages

JS - Ann 11 12

The document contains practical exercises on training neural networks using TensorFlow, specifically focusing on logistic regression and convolutional neural networks (CNNs). It includes code examples for loading datasets, building models, training, and evaluating their performance. The exercises demonstrate the implementation of a simple neural network for MNIST digit classification and a CNN for CIFAR-10 image classification.

Uploaded by

Jotiram Shinde
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)
7 views4 pages

JS - Ann 11 12

The document contains practical exercises on training neural networks using TensorFlow, specifically focusing on logistic regression and convolutional neural networks (CNNs). It includes code examples for loading datasets, building models, training, and evaluating their performance. The exercises demonstrate the implementation of a simple neural network for MNIST digit classification and a CNN for CIFAR-10 image classification.

Uploaded by

Jotiram Shinde
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/ 4

Practical No : 11

Name : Shinde Jotiram Navanath


Class : T.E. AI&DS
Roll.NO : 25

Title : How to Train a Neural Network with TensorFlow/Pytorch and evaluation of logistic
regression using tensorflow

Program Code :

import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import models, layers

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

x_train = x_train / 255


x_test = x_test / 255

model = models.Sequential([
layers.Flatten(input_shape = (28,28)),
layers.Dense(128,activation = 'relu'),
layers.Dense(10,activation = 'softmax')
])

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

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

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


print("Test Accuracy:", test_acc)

Output:
Program Code:

import tensorflow as tf
import numpy as np

x_train = np.array([[0.], [1.], [2.], [3.], [4.]], dtype=float)


y_train = np.array([[0.], [0.], [0.], [1.], [1.]], dtype=float)
x_test = np.array([[1.5], [2.5], [3.5]], dtype=float)
y_test = np.array([[0.], [0.], [1.]], dtype=float)

model = tf.keras.Sequential([
layers.Dense(1, activation='sigmoid', input_shape=(1,))
])

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

model.fit(x_train, y_train, epochs=1000, verbose=0)


loss, accuracy = model.evaluate(x_test, y_test)
print(f"Test Accuracy: {accuracy}")

print("Prediction for x=2.0: ", model.predict([[2.0]]))

Output:
Practical No : 12

Name : Shinde Jotiram Navanath


Class : T.E. AI&DS
Roll.NO : 25

Title : TensorFlow/Pytorch implementation of CNN

Program Code :

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt

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

x_train, x_test = x_train / 255.0, x_test / 255.0

class_names = ['Airplane','Automobile','Bird','Cat','Deer',
'Dog','Frog','Horse','Ship','Truck']

plt.figure(figsize=(2,2))
plt.imshow(x_train[0])

model = models.Sequential([

layers.Conv2D(32, (3, 3), strides=1, padding='same', activation='relu',


input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2), strides=2),

layers.Conv2D(64, (3, 3), strides=1, padding='same', activation='relu'),


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

layers.Conv2D(128, (3, 3), strides=1, padding='same', activation='relu'),


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

layers.Flatten(),

layers.Dense(128, activation='relu'),

layers.Dense(10, activation='softmax')
])

model.summary()

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

history = model.fit(x_train, y_train, epochs=10,


validation_data=(x_test, y_test))

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


print(f"Test Accuracy: {test_acc}")

pred = model.predict(x_test[0].reshape(1,32,32,3))
print("Predicted Class:", class_names[tf.argmax(pred[0])])
print("Actual Class:", class_names[y_test[0][0]])

plt.imshow(x_test[0])
plt.axis('off')
plt.show()

Output:

You might also like