BIA9
BIA9
import numpy as np
print(output)
#Problema 2
import tensorflow as tf
from tensorflow.keras.datasets import svhn
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
#We load the dataset using svhn.load_data() function from the TensorFlow Keras
library. The input images are normalized by dividing their pixel values by 255.0,
and the labels #are one-hot encoded using the tf.keras.utils.to_categorical()
function.
#The training dataset is then split into training and validation sets, with the
first 30,000 images used for validation and the remaining images used for training.
#The CNN model architecture is defined using the Sequential API of TensorFlow
Keras. It consists of two convolutional layers with max pooling, followed by two
fully connected #layers and an output softmax layer.
#The model is compiled using stochastic gradient descent (SGD) optimizer with a
learning rate of 0.5, and the categorical cross-entropy loss function. The model is
then trained #for 10 epochs on the training data with a batch size of 32, and the
validation data is used to monitor the training progress.
#Finally, the trained model is evaluated on the test dataset using the
model.evaluate() function, which returns the test loss and accuracy. The test
accuracy is printed to the console.
#Note that the LeNet model architecture consists of two convolutional layers
followed by max pooling, and three fully connected layers. The learning rate is set
to 0.5, and the #model is trained for 10 epochs with a batch size of 32. Finally,
the model is evaluated on the test dataset to obtain the accuracy.
#Problema 3
#Classify the SVHN dataset using the following convolutional neural network
architecture.Divide the training dataset as follows:30000 validation images and
43257 training #images.Train your model for 5 epochs using a learning rate of 0.05
and a batch size of 256.Define a class for implementing the Convolutional
Block.Conv2D,32,3x3 means 32 #output channels and 3x3 kernel size.Use padding,if
necessary.
#The convolutional block is described like that:
#-input->Conv2d,64,3x3 ReLU
->
#
Add->Flatten->Linear,10
# ->Conv2d,32,3x3->Conv2d,64,3x3,BatchNorm2d ReLU ->
#Generate a Python code for this request.
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision.datasets import SVHN
from torchvision.transforms import ToTensor
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print('Finished Training')
#We train the convolutional neural network on the SVHN dataset for 5 epochs with a
learning rate of 0.05 and a batch size of 256. The training dataset is divided into
training and #validation datasets with 43,257 and 30,000 images, respectively. The
ConvBlock class implements a convolutional block with skip connections, and the CNN
class implements #the full convolutional neural network with the specified
architecture. The train_loader and val_loader data loaders are used to load the
training and validation datasets, #respectively. The criterion and optimizer are
used to define the loss function and optimizer, respectively. The for loop trains
the model for 5 epochs, and the with torch.no_grad() #block evaluates the model on
the validation dataset. The final line of the code indicates that the training has
finished.