Question: / * Do not edit any given code in this file. * / import java.io . File; import java.io . FileWriter; import java.io . IOException;

/*
Do not edit any given code in this file.
*/
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
public class Model {
/**
* The weight matrix between layer 3 and the output layer
*/
private double[][] weights;
/**
* The bias vector of the output layer
*/
private double[] bias;
/**
* ToDo: Write a no-arg constructor with empty body
*/
/**
* ToDo: Write a constructor that initialize the model to be the DEEP COPY of the given arguments
* @param 1: a double[][] that keeps the data of weights
* @param 2: a double[][] that keeps the data of bias
*/
/**
* ToDo: Write a constructor that initialize the model from a given saved model file
* @param 1: A String that indicates the path of the saved model file
*/
/**
* The getter of the weights field.
*/
public double[][] getWeights(){
return weights;
}
/**
* The getter of the bias field.
*/
public double[] getBias(){
return bias;
}
/**
* ToDo: Write a method named "save" that saves the current model into a file
* @param 1: A String that indicates the path to the destination location for saving the file
*/
/**
* ToDo: A method named "load" that loads the model from a saved model file.
* @param 1: A String that indicates the path to the destination location for model file to be loaded.
*/
/**
* A method that trains the model
* @param 1: An int that indicates the number of possible output labels
* @param 2: An int that indicates the number of iteration for the training
* @param 3: A String the indicates the path of the directory that keeps the training data
* @param 4: A double that indicates the learning rate
*/
public void trainModel(int numLabel, int iter, String path, double learningRate) throws IOException {
Model m = Trainer.train(numLabel, iter, path, learningRate);
weights = m.getWeights();
bias = m.getBias();
}
/**
* A method that performs classification according to the current model
* @param 1: A String that indicates the location of the target data that to be classified
* @return the integer that indicates the classification result (the label)
*/
public int classification(String file) throws IOException {
return Trainer.predict(file, weights, bias);
}
/**
* Prints the weight matrix for debugging purpose if needed
*/
public void printWeights(){
if (weights != null)
ExtraMUtil.matPrint(weights);
}
/**
* Prints the bias vector for debugging purpose if needed
*/
public void printBias(){
if (bias != null)
ExtraMUtil.vecPrint(bias);
}
}

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!