Question: Anaswer question P 1 . 3 , P 2 . 3 , P 3 . 1 and P 3 . 2 . ( Dont write

Anaswer question P1.3, P2.3, P3.1 and P3.2.(Dont write description, just code).Also review these codes and find the isuse. Check the following code lines (DONT USE AI), against these questions and modify if any issue is there.
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
(x_train, y_train),(x_test, y_test)= mnist.load_data()
x_train = x_train.astype("float32")/255.0
x_test = x_test.astype("float32")/255.0
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
input_shape = x_train.shape[1:]
num_classes =10
epochs =1000
def build_mlp_model():
model = Sequential([
layers.Flatten(input_shape=input_shape),
layers.Dense(4, activation="relu"),
layers.Dense(4, activation="relu"),
layers.Dense(4, activation="relu"),
layers.Dense(4, activation="relu"),
layers.Dense(4, activation="relu"),
layers.Dense(4, activation="relu"),
layers.Dense(np.prod(input_shape), activation="sigmoid"),
layers.Reshape(input_shape)
])
return model
mlp_model = build_mlp_model()
mlp_model.compile(optimizer="sgd", loss="mse")
mlp_model.summary()
mlp_history = mlp_model.fit(x_train[:1], x_train[:1], epochs=epochs, batch_size=1, verbose=1)
plt.plot(mlp_history.history['loss'])
plt.title('MLP Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()
def build_cnn_model():
model = Sequential([
layers.Conv2D(10, kernel_size=(5,5), activation='relu', padding='same', input_shape=input_shape),
layers.Conv2D(input_shape[-1], kernel_size=(1,1), activation='sigmoid', padding='same')
])
return model
cnn_model = build_cnn_model()
cnn_model.compile(optimizer="sgd", loss="mse")
cnn_model.summary()
cnn_history = cnn_model.fit(x_train[:1], x_train[:1], epochs=epochs, batch_size=1, verbose=1)
plt.plot(cnn_history.history['loss'])
plt.title('CNN Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()
W1= np.eye(input_shape[0])
W2= np.eye(4)
W3= np.eye(4)
b1= np.zeros((4,))
b2= np.zeros((4,))
b3= np.zeros(input_shape)
b3= np.ones(input_shape)* x_train[0][0][0][0]
def build_identity_cnn_model():
model = keras.Sequential([
layers.Conv2D(input_shape[-1], kernel_size=(1,1), activation='linear', padding='same', input_shape=input_shape)
])
return model
def build_identity_relu_cnn_model():
model = keras.Sequential([
layers.Conv2D(input_shape[-1], kernel_size=(1,1), activation='relu', padding='same', input_shape=input_shape)
])
return model
Answer these questions:
P1.1- Implement a fully connected neural network h:[0,1]^28x28(up to)[0,1]^28x28 model that regresses an image into itself. The architecture should have 7 trainable dense layers: the first 6 layers with 4 neurons and ReLU activation, and an output layer with the necessary number of units and activation.
P1.2- Train the model using SGD on the appropriate loss function for 10^3 epochs on the training data. Plot the training loss over epochs.
P1.3- Plot the prediction over the training set and test set (you should spot a pattern in the predictions, but since there is some randomness associated with using the GPU we recommend repeating the training 3-5 times to be sure you pick up the right pattern). Which function do you conjecture h(x) has learnt (write it in formula)?
P2.1- Implement a CNN g:[0,1]^28x28(up to)[0,1]^28x28
model that regresses an image into itself. The architecture should have 2 convolutional layers: the first with 10 filters, kernel size 5x5
and the same output size as input, and the second a convolutional output layer with the necessary number of filters, kernel and activation.
P2.2- Train the model using SGD on the appropriate loss function for 10^3 epochs on the training data. Plot the training loss over ephocs.
P2.3-[exaclty the same as P1.3 but for g(x)]
P3.1- Consider a multilayer ReLU network h: R^n (up to) R^n such that h(x)= W3ReLU(W2ReLU(W1x+b1)+b2)+b3 with W1(as an element of)R^a x n, W2(as an element of) R^ n x a, W3(as an element of) R^n x n, b1(as element of )R^a; b2, b3(as element of ) R^n. Find a possible solution for W1, W2, W3, b1, b2, b3 such that h represents the identity funct
What if you want h to represent a constant function that always outputs x0?
P3.2- Consider a CNN g: R^n x n (up to) R^n x n model composed by a first hidden convolutional layer with c filters, d x d (d>1 odd) kernel, identity activation and a suitable convolutional output layer. Find a possible architecture for g (i.e. specify the complete architecture, c, the values in the filters, padding and stride) such that g represents the identity function.
If instead of the identity activation, we use a ReLU activation, how should the architecture change?
(Note: R for set of real

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!