public class SimpleSoftmax {
/**
* This method calculates the softmax output of a
non-normalized array.
* Example: double[] nonNormArr = {-3.0, 0.2,
7.8}
* gives the result {0.00002, 0.00050, 0.99948}
* Details of how to compute the softmax output can be
found at zyBook instructions.
* Hint: use Math.exp(double a) to calculate the
exponential values.
* @param nonNormArr The non-normalized array to be
softmaxed.
* @return The softmax output of the input array; return null if
nonNormArr is null.
*/
public static double[] softmax(double[] nonNormArr)
{
}
/**
* This method gives the predicted label of a
normalized array.
* Algorithm:
* 1. Find the maximum value in the normalized
array.
* 2. Return the index of the maximum value. If there's
a tie, return the leftmost
* maximum one.
* Example: double[] normArr = {0.00002, 0.00050,
0.99948},
* The predicted label is 2 because the largest
probability is 0.99948.
* @param normArr The normalized input array.
* @return The classification label inferred from the
input array.
* Return -1 if normArr is null or empty.
*/
public static int predictLabel(double[] normArr) {
}
/**
* This method computes the prediction accuracy of a
sequence of non-normalized arrays.
* Algorithm:
* 1. For each non-normalized array in the input
sequence, call the softmax method
* to calculate the normalized array. Then call the
predictLabel
* method to find the predicted classification label.
All the
* prediction results in the sequence will form a
prediction array.
* 2. Perform an element-wise comparison between the
prediction array and
* the groundTruthArr. If an element in the prediction
array equals to
* the corresponding one in the groundTruthArr, it is a
correct prediction.
* 3. The prediction accuracy is calculated as:
* Number Of Correct Predictions / Number Of Total
Predictions.
* Example: double[][] nonNormArrSeq = {{-3.0, 0.2,
7.8},
* {1.0, 0.0, 0.0},
* {1.0, 2.0, 3.0}},
* and int[] groundTruthArr = {2, 0, 0}.
*
* First, the normalized array sequence should be:
* {{0.00002, 0.00050, 0.99948},
* {0.576117, 0.21194, 0.21194},
* {0.090031, 0.24473, 0.66524}}
* the prediction array should be {2, 0, 2}.
* The correctness of the 3 predictions are {correct, correct,
wrong}.
* Therefore, the prediction accuracy is 2 / 3 = 0.67.
* @param nonNormArrSeq The sequence of non-normalized
arrays.
* @param groundTruthArr The array of ground truth
labels.
* @return The accuracy of all the predictions in the
input sequence
* compared to the ground truth labels.
* Return -1 if either input is null, either input is
empty,
* or the length of the two input arrays doesn't
match.
*/
public static double calcPredictionAccuracy(double[][]
nonNormArrSeq, int[] groundTruthArr) {
int[] predictionArr = new
int[nonNormArrSeq.length];
// TODO: Iterate over
nonNormArrSeq, call softmax and predictLabel to find the
prediction,
// and populate
predictionArr.
// TODO: Do element-wise comparison
between predictionArr and groundTruthArr
}
}
I need code for this given by the instructor
Program:
Here is the completed code for this problem including updated
test program. Comments are included, go through it, learn how
things work and let me know if you have any doubts or if you need
anything to change. If you are satisfied with the solution, please
rate the answer. If not, PLEASE let me know before you rate, I’ll
help you fix whatever issues. Thanks
// SimpleSoftmax.java
public class SimpleSoftmax {
/**
* This method calculates the softmax output of a non-normalized
array.
* Example: double[] nonNormArr = {-3.0, 0.2, 7.8} gives the
result
* {0.00002, 0.00050, 0.99948} Details of how to compute the softmax
output
* can be found at zyBook instructions. Hint: use Math.exp(double a)
to
* calculate the exponential values.
*
* @param nonNormArr
* The non-normalized array to be softmaxed.
* @return The softmax output of the input array; return null if
nonNormArr
* is null.
*/
public static double[] softmax(double[] nonNormArr) {
if (nonNormArr == null) {
return null;
}
// creating a variable to store the sum of exponents
double sum_exps = 0;
// looping through each number in nonNormArr, finding exp value,
adding
// to sum
for (int i = 0; i < nonNormArr.length; i++) {
sum_exps += Math.exp(nonNormArr[i]);
}
// creating a new array of same length to store normalized
values
double normArray[] = newdouble[nonNormArr.length];
// looping through each element in nonNormArr once more
for (int i = 0; i < nonNormArr.length; i++) {
// finding exp of nonNormArr[i], dividing by sum to get
normalized
// value
normArray[i] = Math.exp(nonNormArr[i]) / sum_exps;
}
// returning normalized array
return normArray;
}
/**
* This method gives the predicted label of a normalized array.
Algorithm:
* 1. Find the maximum value in the normalized array. 2. Return the
index of
* the maximum value. If there's a tie, return the leftmost maximum
one.
* Example: double[] normArr = {0.00002, 0.00050, 0.99948}, The
predicted
* label is 2 because the largest probability is 0.99948.
*
* @param normArr
* The normalized input array.
* @return The classification label inferred from the input array.
Return -1
* if normArr is null or empty.
*/
public static int predictLabel(double[] normArr) {
if (normArr == null || normArr.length == 0) {
return -1;
}
// initializing index to -1
int index_max = -1;
// looping through the normArr
for (int i = 0; i < normArr.length; i++) {
// if index_max is still -1 or element at i is bigger than
element
// at index_max, updating index_max
if (index_max == -1 || normArr[i] > normArr[index_max]) {
index_max = i;
}
}
return index_max;
}
/**
* This method computes the prediction accuracy of a sequence
of
* non-normalized arrays. Algorithm: 1. For each non-normalized
array in the
* input sequence, call the softmax method to calculate the
normalized
* array. Then call the predictLabel method to find the
predicted
* classification label. All the prediction results in the sequence
will
* form a prediction array. 2. Perform an element-wise comparison
between
* the prediction array and the groundTruthArr. If an element in
the
* prediction array equals to the corresponding one in the
groundTruthArr,
* it is a correct prediction. 3. The prediction accuracy is
calculated as:
* Number Of Correct Predictions / Number Of Total Predictions.
Example:
* double[][] nonNormArrSeq = {{-3.0, 0.2, 7.8}, {1.0, 0.0, 0.0},
{1.0, 2.0,
* 3.0}}, and int[] groundTruthArr = {2, 0, 0}.
*
* First, the normalized array sequence should be: {{0.00002,
0.00050,
* 0.99948}, {0.576117, 0.21194, 0.21194}, {0.090031, 0.24473,
0.66524}} the
* prediction array should be {2, 0, 2}. The correctness of the
3
* predictions are {correct, correct, wrong}. Therefore, the
prediction
* accuracy is 2 / 3 = 0.67.
*
* @param nonNormArrSeq
* The sequence of non-normalized arrays.
* @param groundTruthArr
* The array of ground truth labels.
* @return The accuracy of all the predictions in the input
sequence
* compared to the ground truth labels. Return -1 if either input
is
* null, either input is empty, or the length of the two input
* arrays doesn't match.
*/
public static doublecalcPredictionAccuracy(double[][]
nonNormArrSeq,
int[] groundTruthArr) {
//if input is invalid, returning -1
if (nonNormArrSeq == null || groundTruthArr == null
|| nonNormArrSeq.length != groundTruthArr.length
|| nonNormArrSeq.length == 0) {
return -1;
}
int[] predictionArr = newint[nonNormArrSeq.length];
// Iterating over nonNormArrSeq
for (int i = 0; i < nonNormArrSeq.length; i++) {
// fetching non normalized array at index i
double[] nonNormArr = nonNormArrSeq[i];
// finding normalized array using softmax method
double[] normArr =softmax(nonNormArr);
// predicting label, storing result in predictionArr
predictionArr[i] = predictLabel(normArr);
}
int right = 0; // number of right predictions
// performing an element-wise comparison between predictionArr
and
// groundTruthArr
for (int i = 0; i < nonNormArrSeq.length; i++) {
if (predictionArr[i] == groundTruthArr[i]) {
// incrementing right predictions
right++;
}
}
// dividing right predictions by total number of predictions to
get
// accuracy
double accuracy = (double) right / nonNormArrSeq.length;
return accuracy;
}
}
// TestSimpleSoftmax.java
import java.lang.Math;
public class TestSimpleSoftmax {
// Tests for the softmax method.
// Returns true if tests pass, false otherwise.
public static boolean testSoftmax() {
System.out.println("Starting testSoftmax...");
double[] input1 = { -3.0, 0.2, 7.8 };
double[] output1 = { 2.0388883841921518E-5,
5.001909085372945E-4,
0.9994794202076208 };
if(!equalDoubleArr(SimpleSoftmax.softmax(input1), output1)) {
System.out.println("\tTest softmax 1 failed!");
return false;
}
double[] input2 = { 0, 0, 0, 1 };
double[] output2 = { 0.17487770452710946,
0.17487770452710946,
0.17487770452710946, 0.4753668864186717 };
if(!equalDoubleArr(SimpleSoftmax.softmax(input2), output2)) {
System.out.println("\tTest softmax 2 failed!");
return false;
}
System.out.println("...Done testPredictLabel");
return true;
}
// Tests for the calcPredictionAccuracy method.
// Returns true if tests pass, false otherwise.
public static booleantestCalcPredictionAccuracy() {
double EPSILON = 0.000001;
System.out.println("Starting testCalcPredictionAccuracy...");
{
double[][] input = { { -3.0, 0.2, 7.8 }, { 1, 0, 0 },
{ 10, 30, -3 } };
int[] gtLabel = { 2, 0, 0 };
double correctResult = (double) 2 / 3;
if (!(Math.abs(SimpleSoftmax.calcPredictionAccuracy(input,
gtLabel)
- correctResult) <= EPSILON)) {
System.out.println("\tTest calcPredictionAccuracy 1
failed!");
return false;
}
}
{
double[][] input = { { -3.0, 0.2, 7.8, 2.2 }, { 0, 0, 0, 1 }
};
int[] gtLabel = { 2, 3 };
double correctResult = 1.0;
if (!(Math.abs(SimpleSoftmax.calcPredictionAccuracy(input,
gtLabel)
- correctResult) <= EPSILON)) {
System.out.println("\tTest calcPredictionAccuracy 2
failed!");
return false;
}
}
{
double[][] input = { { -3.0, 0.2, 7.8, 2.2, 1.3 },
{ 0, 0, 0, 1, 0 }, { -3.2, 2.1, 2.1, 0.0005, 1.32 },
{ 5.74, -1.81, 10.94, 0.84, 13.70 },
{ 7.71, 11.34, 0.27, 13.08, -0.95 },
{ 0.97, 2.22, 11.80, 8.76, 2.12 },
{ -3.15, 6.76, 7.64, 5.64, 6.24 },
{ 13.26, -0.35, 10.26, 6.13, 11.25 },
{ 2.91, -0.92, 3.51, 11.00, 13.57 },
{ 8.60, 13.21, 12.88, 10.89, 12.27 } };
int[] gtLabel = { 2, 3, 2, 4, 3, 2, 2, 0, 4, 2 };
double correctResult = 0.8;
if (!(Math.abs(SimpleSoftmax.calcPredictionAccuracy(input,
gtLabel)
- correctResult) <= EPSILON)) {
System.out.println("\tTest calcPredictionAccuracy 3
failed!");
return false;
}
}
System.out.println("...Done testCalcPredictionAccuracy");
return true;
}
public static void main(String[] args) {
testSoftmax();
testPredictLabel();
testCalcPredictionAccuracy();
}
}
OUTPUT:
Starting testSoftmax...
...Done testSoftmax
Starting testPredictLabel...
...Done testPredictLabel
Starting testCalcPredictionAccuracy...
...Done testCalcPredictionAccuracy
#Could you please leave a THUMBS UP for my work..
public class SimpleSoftmax { /** * This method calculates the softmax output of a...
Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) { if(arr == null || arr.length == 0) { return -1; } for (int i = 0; i < arr.length; i++) { if(arr[i] == ch) { return i; } } return -1; ...
/** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; } /** * Create a String that...
Java
Here is the template
public class ShiftNumbers {
public static void main(String[] args) {
// TODO: Declare matrix shell size
// TODO: Create first row
// TODO: Generate remaining rows
// TODO: Display matrix
}
/**
* firstRow
*
* This will generate the first row of the matrix, given the size n. The
* elements of this row will be the values from 1 to n
*
* @param size int Desired size of the array
* @return...
Can someone help me with the writing of this code please? public static int promptInt(Scanner input, String prompt, int min, int max) { return 0; //TODO replace } /** * Returns the index within arr of the first occurrence of the specified character. * If arr is null or 0 length then return -1. For all arrays, don't assume a length * but use the array .length attribute. * @param arr The array to look...
* The Map class is used to create and manipulate voting maps. The value of a * cell on the map denotes the party for which the majority of the population * of that cell votes for. For instance, in the following map, PARTY_X is the * choice of voters in three cells, while PARTY_O is preferred in the rest of * the map: * O X O * X O X * O O O * A map is...
The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...
in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); Scanner input2=new Scanner(System.in); UNOCard c=new UNOCard (); UNOCard D=new UNOCard (); Queue Q=new Queue(); listplayer ll=new listplayer(); System.out.println("Enter Players Name :\n Click STOP To Start Game.."); String Name = input.nextLine();...
import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of this class in the * MasterMind program. * * @author TODO add your name here */ public class MasterMind { /** * Prompts the user for a value by displaying prompt. * Note: This method should not add a new line to the output of prompt. * * After prompting the user, the method will consume an entire * line of input while reading...
I currently have this but it doesn't work for the three item
arrays
public class Quicksort extends Partitioner {
public static void quicksort(int[] values) {
if (values == null || values.length < 2) {
return;
}
quicksort(values, 0, values.length - 1);
}
private static void quicksort(int[] values, int start, int end)
{
System.out.println(values);
System.out.println(start);
System.out.println(end);
if (values == null || Math.abs(start - end) == 1) {
return;
}
if (end > start) {
int pivotValueIndex = partition(values, start, end);
quicksort(values,...