Posts

2

#1 from tensorflow.keras.datasets import fashion_mnist (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() x_train,x_test=x_train/255.0,x_test/255.0 #2 import tensorflow as tf model = tf.keras.Sequential([   tf.keras.layers.Flatten(input_shape=(28, 28)),   tf.keras.layers.Dense(128, activation='relu'),   tf.keras.layers.BatchNormalization(),   tf.keras.layers.Dropout(0.5),   tf.keras.layers.Dense(64, activation='relu'),   tf.keras.layers.Dense(10, activation='softmax') ]) model.compile(optimizer='adam',               loss='sparse_categorical_crossentropy',               metrics=['accuracy']) #3 lr_schedule = tf.keras.callbacks.LearningRateScheduler(     lambda epoch: 1e-3 * 10**(epoch / 20)) history = model.fit(x_train, y_train, epochs=10,                     validation_data=(x_test, y_test),       ...

3

#1.load amd preprocess the CIFAR-10 dataset import numpy as np import tensorflow as tf from tensorflow.keras.datasets import cifar10 from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense from sklearn.metrics import accuracy_score, precision_score, recall_score # Load the CIFAR-10 dataset (x_train, y_train), (x_test, y_test) = cifar10.load_data() # Normalize pixel values to be between 0 and 1 x_train = x_train.astype('float32') / 255.0 x_test = x_test.astype('float32') / 255.0 # convert class vectors to binary class matrices(one-hot encoding) y_train = tf.keras.utils.to_categorical(y_train,num_classes=10) y_test = tf.keras.utils.to_categorical(y_test,num_classes=10) #2.define a deeper cnn model #define the cnn model model = Sequential() model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(32, 32, 3))) model.add(Conv2D(32, (3, 3), activation='relu', padding=...

4

#1.load the IMDB dataset: import numpy as np from tensorflow.keras.datasets import imdb from tensorflow.keras.preprocessing.sequence import pad_sequences #load dataset (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000) #pad sequences to ensure equal length inputs x_train = pad_sequences(x_train, maxlen=200) x_test = pad_sequences(x_test, maxlen=200) #2.define the LSTM model with an embedding layer: model = tf.keras.Sequential([   tf.keras.layers.Embedding(input_dim=10000, output_dim=128, input_length=200),   tf.keras.layers.LSTM(128, return_sequences=False),   tf.keras.layers.Dense(1, activation='sigmoid') ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) #3.train the model on the IMDB dataset: history = model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test, y_test)) #4.visualize training progress: import matplotlib.pyplot as plt #plot accuracy plt.plot(history.history['ac...

5

!pip install tensorflow import tensorflow as tf #1. Load and preprocess the CIFAR-10 dataset: from tensorflow.keras.datasets import cifar10 # Load dataset (X_train, _), (X_test, _) = cifar10.load_data() # Normalize the data X_train = X_train.astype('float32') / 255.0 X_test = X_test.astype('float32') / 255.0 #2. Add noise to the CIFAR-10 images: import numpy as np noise_factor = 0.5 X_train_noisy = X_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_train.shape) X_test_noisy = X_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=X_test.shape) # Clip to keep pixel values between 0 and 1 X_train_noisy = np.clip(X_train_noisy, 0., 1.) X_test_noisy = np.clip(X_test_noisy, 0., 1.) #3. Define the convolutional autoencoder (CAE): model = tf.keras.Sequential([  # Encoder  tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(32, 32, 3)),  tf.keras.layers.MaxPooling2D((2, 2), padding='same'),  tf....

1

#Steps: #1. Set up the environment: !pip install tensorflow import numpy as np #2. Define the OR and XOR gate datasets: # Define OR and XOR datasets X_OR = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32) y_OR = np.array([[0], [1], [1], [1]], dtype=np.float32) X_XOR = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32) y_XOR = np.array([[0], [1], [1], [0]], dtype=np.float32) #3. Create a single-layer perceptron model: import tensorflow as tf # Function to create and train a single-layer perceptron with improved #accuracy def train_perceptron(X, y, epochs=100, learning_rate=0.1):  model = tf.keras.Sequential([  tf.keras.layers.Dense(1, activation='sigmoid', input_shape=(2,))  ])  model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=  learning_rate),loss='binary_crossentropy',  metrics=['accuracy'])  model.fit(X, y, epochs=epochs, verbose=0)  return model #4. Train the model on OR and XOR datasets: # Train on OR gate wi...