, bevor ich in den Code eingreift und was mein neuronales Netzwerk ausgibt, eine kurze Zusammenfassung seiner Architektur: < /p>
1. Schicht, Eingabe: Dies ist nur eine einzige Zahl, die das neuronale Netzwerk < /p>
< /li> klassifizieren muss
2. Schicht, versteckt: Diese Schicht besteht aus 5 Neuronen. Jeder verarbeitet die Eingabe mit der Gleichung y = Gewicht * Eingabe + Bias . Seine AktivaCion -Funktion ist ein undichte Relu < /p>
< /li>
3. Schicht, Ausgabe: Diese Schicht nimmt jede Ausgabe der vorherigen Schicht und multipliziert sie mit dem zugehörigen Gewichtszustand Eine Summe daraus, wie hier zu sehen ist: < /p>
< /li>
< /ul>
Code: Select all
float sum_previous_outputs = output_bias;
for(int j = 0 ; j < hidden_layer_neurons ; j++) {
sum_previous_outputs += output_weights[j] * hidden_layer_outputs[j];
}
Code: Select all
Prediction with input = 1: prediction = 0.46512857
Prediction with input = 2: prediction = 0.48425278
Prediction with input = 3: prediction = 0.5034232
Prediction with input = 4: prediction = 0.52258354
Prediction with input = 5: prediction = 0.54167765
< /code>
Beachten Sie, wie langsam es 'lernt' auch mit 9000000 -Epochen: < /p>
Verlustwert bei der ersten Iteration: < /p>
0. loss = 8.499304
Verlustwert bei der letzten Iteration:
Code: Select all
8999999. loss = 6.7793283
Ich habe versucht, den Parameter Learning_Rate zu optimieren, aber es ist größer Vorhersage, es führt auch zu Nan . Für niedrigere Werte von Learning_Rate "lernt", aber sehr langsam, bis zu einem Punkt, an dem die Erhöhung der Epochen keinen Unterschied macht. Ich habe auch versucht, die Anzahl der Neuronen zu erhöhen, aber das Ergebnis ist das gleiche < /p>
Dies ist meine Hauptfunktion: < /p>
Code: Select all
public class Main {
public static void main(String[] args) {
float training_data[] = {
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f
};
float expected_data[] = {
0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f
};
int epochs = 9000000;
ClassificationAI ai = new ClassificationAI();
ai.train(training_data, expected_data, epochs);
for(int i = 1 ; i < 6 ; i++) {
System.out.println("Prediction with input = "+i+": prediction = "+ai.predict(i));
}
}
}
Code: Select all
import java.util.Random;
//ORIGINAL
public class ClassificationAI {
//hidden layer
private static int hidden_layer_neurons = 5;
private float[] hidden_layer_weights = new float[hidden_layer_neurons];
private float[] hidden_layer_biases = new float[hidden_layer_neurons];
//output layer
private float[] output_weights = new float[hidden_layer_neurons];
private float output_bias;
public ClassificationAI() {
Random random = new Random();
for(int i = 0 ; i < hidden_layer_neurons ; i++) {
this.hidden_layer_weights[i] = random.nextFloat(-1, 1);
this.hidden_layer_biases[i] = 0;
}
for(int i = 0 ; i < hidden_layer_neurons ; i++) {
this.output_weights[i] = random.nextFloat(-1, 1);
}
this.output_bias = 0;
}
private float leaky_relu(float x) {
return (x > 0) ? x : 0.01f * x;
}
private float forward_hidden_layer_neuron(float weight, float x, float bias) {
float input = weight * x + bias;
return leaky_relu(input);
}
private float sigmoid(float x) {
return (float)(1 / (1 + Math.pow(Math.E, -x)));
}
private float sigmoid_derivative(float x) {
float sig = sigmoid(x);
return sig * (1 - sig);
}
public void train(float[] training_data, float[] expected_data, int epochs) {
float learning_rate = 0.00001f;
for(int epoch = 0 ; epoch < epochs ; epoch++) {
float total_loss = 0;
for(int i = 0 ; i < training_data.length ; i++) {
float x = training_data[i];
float expected_x = expected_data[i];
//hidden layer
float[] hidden_layer_outputs = new float[hidden_layer_neurons];
for(int j = 0 ; j < hidden_layer_neurons ; j++) {
hidden_layer_outputs[j] = forward_hidden_layer_neuron(hidden_layer_weights[j], x, hidden_layer_biases[j]);
}
//output layer
float sum_previous_outputs = output_bias;
for(int j = 0 ; j < hidden_layer_neurons ; j++) {
sum_previous_outputs += output_weights[j] * hidden_layer_outputs[j];
}
float final_output = sigmoid(sum_previous_outputs);
//calculating loss
float epsilon = 1e-8f;
total_loss += (float) ((-1.0f)*(expected_x*Math.log(final_output + epsilon) + (1 - expected_x)*Math.log(1 - final_output + epsilon)));
//derivatives for output layer
float dc_ds = -((expected_x/final_output) - (1 - expected_x)/(1 - final_output));
float ds_dy = sigmoid_derivative(sum_previous_outputs);
//float dy_dow; this derivative is every single output of the previous layer
float dy_dob = 1;
float dc_dow = dc_ds * ds_dy/* every single dy_dow of previous layer */;
float dc_dob = dc_ds * ds_dy * dy_dob;
for(int j = 0 ; j < hidden_layer_neurons ; j++) {
output_weights[j] -= dc_dow * hidden_layer_outputs[j] * learning_rate;
}
output_bias -= dc_dob * learning_rate;
//derivatives for hidden layer
for(int j = 0 ; j < hidden_layer_neurons ; j++) {
float hidden_layer_processed_x = hidden_layer_weights[j] * x + hidden_layer_biases[j];
float dR_dy = (hidden_layer_processed_x