JS - Ann 11 12
JS - Ann 11 12
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()
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'])
Output:
Program Code:
import tensorflow as tf
import numpy as np
model = tf.keras.Sequential([
layers.Dense(1, activation='sigmoid', input_shape=(1,))
])
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=['accuracy'])
Output:
Practical No : 12
Program Code :
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
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.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
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: