#my first C++ project

21 messages · Page 1 of 1 (latest)

potent basin
#

#include <iostream>
#include <vector>
#include <cmath>

double sigmoid(double x) {
return (1.0 / (1.0 + std::exp(-x)));
}

struct Neuron{
std::vector<double> i;
std::vector<double> w;
double bias;
double out;
};

using N = Neuron;

/*
[n1]
[n3]-
[n2]/
*/

void generate(N &_N) {
_N.out = 0;
for(unsigned int i = 0;i<_N.w.size();i++) {
_N.out += _N.w[i] * _N.i[i];
}
_N.out += _N.bias;
_N.out = sigmoid(_N.out);
}

void lm_train(N &_N,double lr,double target,std::vector<double> data) {
_N.i = data;
generate(_N);
double error = target - _N.out;
for(unsigned int i = 0; i < _N.w.size();i++) {
_N.w[i] += _N.i[i] * error * lr;
}
_N.bias += error * lr;
}

int main() {
N n1;
n1.i.resize(2);
n1.w.resize(2);

N n2;
n2.i.resize(2);
n2.w.resize(2);

N n3;
n3.i.resize(2);
n3.w.resize(2);

for(int i = 0;i<800;i++) { // nand
    lm_train(n1,0.1,0,{1,1});
    lm_train(n1,0.1,1,{0,1});
    lm_train(n1,0.1,1,{1,0});
    lm_train(n1,0.1,1,{0,0});
}
for(int i = 0;i<800;i++) { // or
    lm_train(n2,0.1,1,{1,1});
    lm_train(n2,0.1,1,{0,1});
    lm_train(n2,0.1,1,{1,0});
    lm_train(n2,0.1,0,{0,0});
}
for(int i = 0;i<800;i++) { // and
    lm_train(n3,0.1,1,{1,1});
    lm_train(n3,0.1,0,{0,1});
    lm_train(n3,0.1,0,{1,0});
    lm_train(n3,0.1,0,{0,0});
}
std::cin >> n1.i[0] >> n1.i[1];
n2.i = n1.i;
generate(n1);
generate(n2);
n3.i = {n1.out,n2.out};
generate(n3);
std::cout << n3.out << std::endl;

}
will it compile?

#

my first C++ project

smoky turtle
#

try it out

potent basin
#

it works i tested it now

#

do u wanna test it?

bleak wyvernBOT
# potent basin #include <iostream> #include <vector> #include <cmath> double sigmoid(double x)...
#include <cmath>
#include <iostream>
#include <vector>

double sigmoid(double x) {
  return (1.0 / (1.0 + std::exp(-x)));
}

struct Neuron {
  std::vector<double> i;
  std::vector<double> w;
  double bias;
  double out;
};

using N = Neuron;

/*
     [n1]\
           [n3]-
     [n2]/
*/

void generate(N& _N) {
  _N.out = 0;
  for (unsigned int i = 0; i < _N.w.size(); i++) {
    _N.out += _N.w[i] * _N.i[i];
  }
  _N.out += _N.bias;
  _N.out = sigmoid(_N.out);
}

void lm_train(N& _N, double lr, double target, std::vector<double> data) {
  _N.i = data;
  generate(_N);
  double error = target - _N.out;
  for (unsigned int i = 0; i < _N.w.size(); i++) {
    _N.w[i] += _N.i[i] * error * lr;
  }
  _N.bias += error * lr;
}

int main() {
  N n1;
  n1.i.resize(2);
  n1.w.resize(2);

  N n2;
  n2.i.resize(2);
  n2.w.resize(2);

  N n3;
  n3.i.resize(2);
  n3.w.resize(2);

  for (int i = 0; i < 800; i++) {  // nand
    lm_train(n1, 0.1, 0, {1, 1});
    lm_train(n1, 0.1, 1, {0, 1});
    lm_train(n1, 0.1, 1, {1, 0});
    lm_train(n1, 0.1, 1, {0, 0});
  }
  for (int i = 0; i < 800; i++) {  // or
    lm_train(n2, 0.1, 1, {1, 1});
    lm_train(n2, 0.1, 1, {0, 1});
    lm_train(n2, 0.1, 1, {1, 0});
    lm_train(n2, 0.1, 0, {0, 0});
  }
  for (int i = 0; i < 800; i++) {  // and
    lm_train(n3, 0.1, 1, {1, 1});
    lm_train(n3, 0.1, 0, {0, 1});
    lm_train(n3, 0.1, 0, {1, 0});
    lm_train(n3, 0.1, 0, {0, 0});
  }
  std::cin >> n1.i[0] >> n1.i[1];
  n2.i = n1.i;
  generate(n1);
  generate(n2);
  n3.i = {n1.out, n2.out};
  generate(n3);
  std::cout << n3.out << std::endl;
}

will it compile?

Serial Designation N
smoky turtle
#

would be nice if you explained what its supposed to do

potent basin
#

this kdo is a mathematical simulation of AI, this code teaches AI to solve the problem for XOR frames, so if you compile it and add e.g. 0 1 it will give you about 1

#

and in summary this code can be easily converted into any other more complicated logic gate

smoky turtle
#

then you could build up on this one and make something more complicated

potent basin
#

ill try

smoky turtle
#

and document the journey

#

and its not really AI though, even tho everyone slaps that term on anything these days

#

perhaps an early and very crude version of some neural network

potent basin
#

Ok, I'll try to make some additional documentation, but I don't know if I'll have time, because I have to write the code for this network. It turned out to be something more difficult, but I never paste ready-made ones and I had to figure out how to write such code.

smoky turtle
#

code is not running away

#

you have all the time in the world

#

unless it was just a stupid school assignment and you will never pursue any other programming stuff, then this was just a waste of time

potent basin
#

no, I just have school in a few days and I also have to study for a test bro, I'm 14 years old and I've been coding in C++ for 3 years

inner ice
potent basin
#

i know