0% found this document useful (0 votes)
21 views

How to Train a Neural Network with TensorFlow_Pytorch and evaluation of logistic regression using tensorflow

This document outlines the process of training a Neural Network using TensorFlow and evaluating a Logistic Regression model on the MNIST dataset. It includes step-by-step code examples for both models, demonstrating data preparation, model definition, compilation, training, and evaluation. The conclusion emphasizes the effectiveness of Neural Networks for complex datasets and the suitability of Logistic Regression for binary classification.

Uploaded by

sumitdorle91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

How to Train a Neural Network with TensorFlow_Pytorch and evaluation of logistic regression using tensorflow

This document outlines the process of training a Neural Network using TensorFlow and evaluating a Logistic Regression model on the MNIST dataset. It includes step-by-step code examples for both models, demonstrating data preparation, model definition, compilation, training, and evaluation. The conclusion emphasizes the effectiveness of Neural Networks for complex datasets and the suitability of Logistic Regression for binary classification.

Uploaded by

sumitdorle91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment No 11

How to Train a Neural Network with TensorFlow/Pytorch


and evaluation of logistic regression using tensorflow

1. Introduction
Machine Learning (ML) and Deep Learning (DL) have emerged as powerful tools for solving
real-world problems. TensorFlow and PyTorch are the two most popular frameworks used to
implement Neural Networks (NN). Logistic Regression is one of the fundamental supervised
learning algorithms used for classification problems.

This assignment explains:

●​ Stepwise procedure to train a Neural Network using TensorFlow​

●​ Evaluation of Logistic Regression model using TensorFlow on MNIST dataset.​

3. Dataset Used — MNIST


●​ Total 70,000 images of handwritten digits (0 to 9)​

●​ Image Size: 28x28 pixels​

●​ Grayscale images (Pixel values 0-255)​


●​ Dataset divided into:​

○​ Training Data: 60,000 samples​

○​ Testing Data: 10,000 samples

Code for Training a Neural Network using TensorFlow


# Step 1: Import Libraries

import tensorflow as tf

from tensorflow.keras import layers, models

# Step 2: Load Dataset (MNIST)

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

# Step 3: Normalize Data

x_train, x_test = x_train / 255.0, x_test / 255.0

# Step 4: Define Neural Network Model

model = models.Sequential([

layers.Flatten(input_shape=(28, 28)),

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

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

])

# Step 5: Compile the Model

model.compile(optimizer='adam',

loss='sparse_categorical_crossentropy',

metrics=['accuracy'])
# Step 6: Train the Model

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

# Step 7: Evaluate the Model

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

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

Code for Logistic Regression using TensorFlow (Simple Binary Classification)

import tensorflow as tf

import numpy as np

# Step 1: Prepare Dataset

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)

# Step 2: Define Logistic Regression Model

model = tf.keras.Sequential([

layers.Dense(1, activation='sigmoid', input_shape=(1,))

])
# Step 3: Compile the Model

model.compile(optimizer='sgd',

loss='binary_crossentropy',

metrics=['accuracy'])

# Step 4: Train the Model

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

# Step 5: Evaluate the Model

loss, accuracy = model.evaluate(x_test, y_test)

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

# Step 6: Prediction

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


Conclusion:
●​ Neural Networks can classify complex datasets like MNIST very accurately due to
multiple layers and non-linearity.​

●​ Logistic Regression is best suited for binary classification with a linear decision
boundary.​

●​ TensorFlow provides easy-to-use APIs for model building, training, and evaluation.​

●​ Proper evaluation metrics like accuracy, loss, and prediction analysis help in
performance measurement.
●​

You might also like