dl

/////////////// 2222222222222222222222

import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import random
import numpy as np
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train / 255
x_test = x_test / 255
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=’relu’),
    keras.layers.Dense(10, activation=’softmax’)
])
model.compile(optimizer=’sgd’, loss=’sparse_categorical_crossentropy’, metrics=[‘accuracy’])
history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10)
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(“loss is -> “, test_loss)
print(“accuracy is -> “, test_accuracy)
n = random.randint(0, 9999)
plt.imshow(x_test[n])
plt.show()
predicted_value = model.predict(x_test)
print(np.argmax(predicted_value[n]))
plt.imshow(x_test[n])
plt.show()
print(“Predicted value is: “, predicted_value[n])
plt.plot(history.history[‘accuracy’])
plt.plot(history.history[‘val_accuracy’])
plt.xlabel(‘epochs’)
plt.ylabel(‘accuracy’)
plt.title(‘epoch vs accuracy’)
plt.show()
plt.plot(history.history[‘loss’])
plt.plot(history.history[‘val_loss’])
plt.xlabel(‘epochs’)
plt.ylabel(‘loss’)
plt.title(‘epoch vs loss’)
plt.show()
“`

////////////////////////3333333333333333333333

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from keras.datasets import mnist
from keras.layers import Flatten, Dense, Conv2D, MaxPooling2D, Dropout
from keras.models import Sequential
(x_train, y_train), (x_test, y_test) = mnist.load_data()
input_shape = (28, 28, 1)
x_train = x_train.reshape(60000, 28, 28, 1)
x_test = x_test.reshape(10000, 28, 28, 1)
x_train = x_train / 255
x_test = x_test / 255
model = Sequential()
model.add(Conv2D(28, (3, 3), activation=’relu’, input_shape=input_shape))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(28, activation=’relu’))
model.add(Dropout(0.3))
model.add(Dense(10, activation=’softmax’))
model.compile(optimizer=’adam’, loss=’sparse_categorical_crossentropy’, metrics=[‘accuracy’])
history = model.fit(x_train, y_train, epochs=2)
test_loss, test_accuracy = model.evaluate(x_test, y_test)
for i in range(9):
    plt.subplot(330 + i + 1)
    plt.imshow(x_test[i])
n = np.random.randint(0, 9999)
plt.imshow(np.squeeze(x_test[n]))
plt.show()
pred = model.predict(x_test)
print(“above number is -> “, np.argmax(pred[n]))
plt.plot(history.history[‘loss’])
plt.plot(history.history[‘accuracy’])
plt.show()

@@@@@ NO 6666666666666666  

# Import necessary libraries
import tensorflow_datasets as tfds
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.applications import VGG16
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
import numpy as np
# Load the tf_flowers dataset, split into train and test sets
(train_images, train_labels), (test_images, test_labels) = tfds.load(
    “tf_flowers”, split=[“train[:70%]”, “train[:30%]”], batch_size=-1, as_supervised=True
)
# Resize images to 150×150 and preprocess labels
train_images = tf.image.resize(train_images, (150, 150))
test_images = tf.image.resize(test_images, (150, 150))
train_labels = to_categorical(train_labels, num_classes=5)
test_labels = to_categorical(test_labels, num_classes=5)
# Load VGG16 model without the top layer, freeze layers
base_model = VGG16(weights=”imagenet”, include_top=False, input_shape=(150, 150, 3))
base_model.trainable = False  # Freeze VGG16 layers
# Build and compile the model
model = models.Sequential([
    base_model,
    layers.Flatten(),
    layers.Dense(50, activation=’relu’),
    layers.Dense(20, activation=’relu’),
    layers.Dense(5, activation=’softmax’)
])
model.compile(optimizer=’adam’, loss=’categorical_crossentropy’, metrics=[‘accuracy’])
# Train the model with early stopping
early_stopping = tf.keras.callbacks.EarlyStopping(monitor=’val_accuracy’, patience=5, restore_best_weights=True)
history = model.fit(train_images, train_labels, epochs=10, validation_split=0.2, batch_size=32, callbacks=[early_stopping])
# Evaluate the model on test data
loss, accuracy = model.evaluate(test_images, test_labels)
print(“Loss:”, loss, “Accuracy:”, accuracy)
# Plot training accuracy
plt.plot(history.history[‘accuracy’])
plt.title(‘Training Accuracy’)
plt.xlabel(‘Epoch’)
plt.ylabel(‘Accuracy’)
plt.show()
# Predict and print first 10 results
predictions = model.predict(test_images)
predicted_classes = np.argmax(predictions, axis=1)
print(“Predicted classes:”, predicted_classes[:10])
print(“Actual classes:”, np.argmax(test_labels[:10], axis=1))

Scroll to Top