perceptron implementation changed + perceptronLearningAlgorithm and tests
This commit is contained in:
@@ -25,6 +25,12 @@ target_link_libraries(feedforward_perf NeuralNetwork)
|
||||
add_executable(optical_backpropagation optical_backpropagation.cpp)
|
||||
target_link_libraries(optical_backpropagation NeuralNetwork)
|
||||
|
||||
add_executable(perceptron perceptron.cpp)
|
||||
target_link_libraries(perceptron NeuralNetwork)
|
||||
|
||||
add_executable(perceptron_learning perceptron_learning.cpp)
|
||||
target_link_libraries(perceptron_learning NeuralNetwork)
|
||||
|
||||
add_executable(recurrent recurrent.cpp)
|
||||
target_link_libraries(recurrent NeuralNetwork)
|
||||
|
||||
|
||||
16
tests/perceptron.cpp
Normal file
16
tests/perceptron.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <NeuralNetwork/FeedForward/Perceptron.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
NeuralNetwork::FeedForward::Perceptron p(2,1);
|
||||
|
||||
p[1].weight(0)=-1.0;
|
||||
p[1].weight(1)=1.001;
|
||||
|
||||
assert(p.computeOutput({1,1})[0] == 1.0);
|
||||
p[1].weight(1)=0.999;
|
||||
|
||||
assert(p.computeOutput({1,1})[0] == 0.0);
|
||||
}
|
||||
41
tests/perceptron_learning.cpp
Normal file
41
tests/perceptron_learning.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <NeuralNetwork/Learning/PerceptronLearning.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
{ // XOR problem
|
||||
NeuralNetwork::FeedForward::Perceptron n(2,1);
|
||||
|
||||
n.randomizeWeights();
|
||||
|
||||
NeuralNetwork::Learning::PerceptronLearning learn(n);
|
||||
|
||||
for(int i=0;i<10;i++) {
|
||||
learn.teach({1,0},{1});
|
||||
learn.teach({1,1},{1});
|
||||
learn.teach({0,0},{0});
|
||||
learn.teach({0,1},{1});
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =n.computeOutput({1,1});
|
||||
assert(ret[0] > 0.9);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =n.computeOutput({0,1});
|
||||
assert(ret[0] > 0.9);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =n.computeOutput({1,0});
|
||||
assert(ret[0] > 0.9);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<float> ret =n.computeOutput({0,0});
|
||||
assert(ret[0] < 0.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user