Question: Write a function that reports the testing precision, recall and f 1 - score of a classifier based on an artificial neural network. Also report

Write a function that reports the testing precision, recall and f1-score of a classifier based on an artificial neural network. Also report the trained model. (15%) a. Create a list of NN models, based on the list of numbers in hidden. b. Study the data loader, then, implement the training loop using the provided loss_function and optimizer. c. Study the data loader in the training loop, use the data loader for the testing set. d. Calculate the metrics (precision, recall and f1-score) based on the testing set.
Note: You can increase the max_epoch, and it will take a very long time to train. Note: The example model in this problem is too small and insufficient to solve this problem. If you really want to solve this problem using ANN, you need to use a much bigger one. This problem covers the basics of a neuron network. ```
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import metrics
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
# Problem 2
## when you include batch size in the training process,
# you need to use dataloader
class MyDataset(Dataset):
def __init__(self, x, y):
self.X=torch.tensor(X.values, dtype=torch.float32)
self.y=torch.tensor(y.values, dtype=torch.float32)
def __len__(self):
return len(self.y)
```
```
def __getitem__(self, idx):
return self.x[idx], self.y[idx]
def problem_2(df, Xlabel, ylabel, hidden=[3,3,3], test_size=0.3,
batch_size=100, learning_rate=0.01, max_epochs=50000):
# write your logic here, model is the trained ANN model
model =[]
precision =0
recall =0
f1score =0
random_state =4320 # default is 4320
module =[]
# YOUR CODE HERE: create the list of modules
# the output layer is fixed to 1 neural and sigmoid
module.append(nn.Linear(hidden [-1],1))
module.append(nn.Sigmoid())
# create the nn model based on the list
model = nn.Sequential(*module)
# split the dataset
X_train, X_test, y_train, y_test = train_test_split(df[xlabel],
df[ylabel], test_size=test_size, random_state=random_state)
# create the DataLoader for the training set
mytrain = MyDataset(x_train, y_train)
train_loader = DataLoader(mytrain, batch_size=batch_size,
shuffle=False)
# use MSE loss function, and SGD optimizer
loss_function = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# training loop
for epoch in range(max_epochs):
# you can uncomment the line below,
# to visualize the slow training process
# print("Debug: at epoch: ", epoch)
for data, labels in train_loader:
# YOUR CODE HERE: training loop
# YOUR CODE HERE: follow the training set
# to create dataloader for testing
# then, calculate the metrics
return model, precision, recall, f1score
```
Write a function that reports the testing

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 Programming Questions!