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 fscore of a classifier based on an artificial neural network. Also report the trained model. 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 lossfunction 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 fscore based on the testing set.
Note: You can increase the maxepoch, 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.modelselection import traintestsplit
from sklearn import metrics
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
# Problem
## when you include batch size in the training process,
# you need to use dataloader
class MyDatasetDataset:
def initself x y:
self.Xtorch.tensorXvalues, dtypetorch.float
self.ytorch.tensoryvalues, dtypetorch.float
def lenself:
return lenselfy
def getitemself idx:
return self.xidx self.yidx
def problemdf Xlabel, ylabel, hidden testsize
batchsize learningrate maxepochs:
# write your logic here, model is the trained ANN model
model
precision
recall
fscore
randomstate # default is
module
# YOUR CODE HERE: create the list of modules
# the output layer is fixed to neural and sigmoid
module.appendnnLinearhidden
module.appendnnSigmoid
# create the nn model based on the list
model nnSequentialmodule
# split the dataset
Xtrain, Xtest, ytrain, ytest traintestsplitdfxlabel
dfylabel testsizetestsize, randomstaterandomstate
# create the DataLoader for the training set
mytrain MyDatasetxtrain, ytrain
trainloader DataLoadermytrain batchsizebatchsize,
shuffleFalse
# use MSE loss function, and SGD optimizer
lossfunction nnMSELoss
optimizer torch.optim.SGDmodelparameters lrlearningrate
# training loop
for epoch in rangemaxepochs:
# you can uncomment the line below,
# to visualize the slow training process
# printDebug: at epoch: epoch
for data, labels in trainloader:
# 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, fscore
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
