Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdlib>
- #include <cassert>
- #include <cmath>
- using namespace std;
- class Neuron;
- typedef vector<Neuron> Layer;
- struct Connection
- {
- double weight;
- };
- class Neuron
- {
- public:
- Neuron(unsigned numOutputs, unsigned myIndex);
- void setOutputVal(double val) { m_outputVal = val; }
- double getOutputVal(void) const { return m_outputVal; }
- void feedForward(const Layer &prevLayer);
- void loadInputWeights(Layer &prevLayer, vector<double> &weights);
- private:
- static double transferFunction(double x);
- double m_outputVal;
- vector<Connection> m_outputWeights;
- unsigned m_myIndex;
- };
- Neuron::Neuron(unsigned numOutputs, unsigned myIndex)
- {
- for (unsigned c = 0; c < numOutputs; c++)
- {
- m_outputWeights.push_back(Connection());
- }
- m_myIndex = myIndex;
- }
- void Neuron::feedForward(const Layer &prevLayer)
- {
- double sum = 0.0;
- for (unsigned n = 0; n < prevLayer.size(); n++)
- {
- sum += prevLayer[n].getOutputVal() * prevLayer[n].m_outputWeights[m_myIndex].weight;
- }
- m_outputVal = Neuron::transferFunction(sum);
- }
- double Neuron::transferFunction(double x)
- {
- return tanh(x);
- }
- void Neuron::loadInputWeights(Layer &prevLayer, vector<double> &weights)
- {
- for (unsigned n = 0; n < prevLayer.size(); n++)
- {
- Neuron &neuron = prevLayer[n];
- neuron.m_outputWeights[m_myIndex].weight = weights[n];
- }
- }
- class Net
- {
- public:
- Net(const vector<unsigned> &topology);
- void feedForward(const vector<double> &inputVals);
- void getResults(vector<double> &resultVals) const;
- void fillWeights(vector<vector<vector<double>>> &weights);
- private:
- vector<Layer> m_layers;
- };
- Net::Net(const vector<unsigned> &topology)
- {
- unsigned numLayers = topology.size();
- for (unsigned layerNum = 0; layerNum < numLayers; layerNum++)
- {
- m_layers.push_back(Layer());
- unsigned numOutputs = layerNum == topology.size() - 1 ? 0 : topology[layerNum + 1];
- for (unsigned neuronNum = 0; neuronNum <= topology[layerNum]; neuronNum++)
- {
- m_layers.back().push_back(Neuron(numOutputs, neuronNum));
- }
- m_layers.back().back().setOutputVal(1.0);
- }
- }
- void Net::feedForward(const vector<double> &inputVals)
- {
- assert(inputVals.size() == m_layers[0].size() - 1);
- for (unsigned i = 0; i < inputVals.size(); i++)
- {
- m_layers[0][i].setOutputVal(inputVals[i]);
- }
- for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++)
- {
- Layer &prevLayer = m_layers[layerNum - 1];
- for (unsigned n = 0; n < m_layers[layerNum].size() - 1; n++)
- {
- m_layers[layerNum][n].feedForward(prevLayer);
- }
- }
- }
- void Net::fillWeights(vector<vector<vector<double>>> &weights)
- {
- for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++)
- {
- Layer &layer = m_layers[layerNum];
- Layer &prevLayer = m_layers[layerNum - 1];
- for (unsigned n = 0; n < layer.size() - 1; n++)
- {
- layer[n].loadInputWeights(prevLayer, weights[layerNum - 1][n]);
- }
- }
- }
- void Net::getResults(vector<double> &resultVals) const
- {
- resultVals.clear();
- for (unsigned n = 0; n < m_layers.back().size() - 1; n++)
- {
- resultVals.push_back(m_layers.back()[n].getOutputVal());
- }
- }
- int main()
- {
- vector<unsigned> topology = {2, 2, 1};
- Net myNet(topology);
- vector<vector<vector<double>>> weights = {{{-0.688558, 0.756463, -0.000669394}, {2.35899, -2.38341, -0.692542}}, {{0.0261347, 1.43365, 1.28669}}};
- myNet.fillWeights(weights);
- while (true)
- {
- double a;
- double b;
- string nameA;
- string nameB;
- cin >> nameA;
- cin.ignore();
- cin >> a;
- cin.ignore();
- cin >> nameB;
- cin.ignore();
- cin >> b;
- cin.ignore();
- a /= 100.0;
- b /= 100.0;
- vector<double> inputVals;
- inputVals.push_back(a);
- inputVals.push_back(b);
- myNet.feedForward(inputVals);
- vector<double> resultVals;
- myNet.getResults(resultVals);
- int answerId = round(abs(resultVals.back()));
- if (answerId == 0)
- {
- cout << nameA << endl;
- }
- else
- {
- cout << nameB << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement