Warning: session_start(): open(/var/lib/php/sessions/sess_o591lfqpoindinq87nssot3kf6, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
sclass Perceptron {
srecord Example(double[] inputs, bool answer) {}
new L examples;
double c = 1;
bool randomizeC = true, startWithRandomWeights = false;
long trainingRound;
double error = -1; // -1 = not trained yet
double[] weights;
*() {}
*(L examples) {}
Iterator trainingIterator() {
if (empty(examples)) fail("No examples");
int n = l(first(examples).inputs);
if (weights == null) {
weights = new double[n];
if (startWithRandomWeights)
for i over weights: weights[i] = random(-1.0, 1.0);
}
ret iff(() -> {
if (error == 0) ret endMarker();
++trainingRound;
double lastError = error;
error = trainARound();
ret error != lastError ? error : null;
});
}
double trainARound() {
double error = 0;
for (Example e : examples) error += abs(train(e));
ret error/l(examples);
}
bool feedForward(double[] inputs) {
double sum = last(weights);
for i over inputs: sum += inputs[i] * weights[i];
ret activate(sum);
}
bool activate(double s) {
return s > 0;
}
double train(Example e) {
int guess = boolToInt(feedForward(e.inputs));
double error = boolToInt(e.answer) - guess;
double cc = c/l(examples);
for i over weights:
weights[i] += (randomizeC ? rand(cc) : cc) * error * e.inputs[i];
ret error;
}
void printWithWeights {
print("Error: " + error + ", round " + trainingRound + ", weights: " + sfu(weights));
}
}