Skip to content

Commit a56e41e

Browse files
feat: add a 3rd approach with cpp
1 parent 4118e90 commit a56e41e

5 files changed

Lines changed: 93 additions & 0 deletions

File tree

try3 (Vector Approach 2)/classes.cpp

Whitespace-only changes.

try3 (Vector Approach 2)/classes.h

Whitespace-only changes.

try3 (Vector Approach 2)/main.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <cstdlib>
2+
#include <iostream>
3+
#include <vector>
4+
5+
struct Connection {
6+
double weight;
7+
double deltaWeight;
8+
};
9+
class Neuron;
10+
11+
typedef std::vector<Neuron> Layer;
12+
13+
// ********************** Class Neuron **************************
14+
15+
class Neuron {
16+
public:
17+
Neuron(unsigned numOutputs);
18+
19+
private:
20+
static double randomWeight() { return rand() / double(RAND_MAX); }
21+
double m_outputVal;
22+
std::vector<Connection> m_outputWeights;
23+
};
24+
Neuron::Neuron(unsigned numOutputs) {
25+
for (unsigned c = 0; c < numOutputs; c++) {
26+
m_outputWeights.push_back(Connection());
27+
m_outputWeights.back().weight = randomWeight();
28+
}
29+
};
30+
31+
// ********************** Class Net **************************
32+
class Net {
33+
public:
34+
Net(const std::vector<unsigned> &topology);
35+
void feedforward(std::vector<double> &inputVals) {};
36+
void backProp(const std::vector<double> &targetVals) {};
37+
void getResults(const std::vector<double> resultVals) const {};
38+
39+
private:
40+
std::vector<Layer> m_layers;
41+
};
42+
43+
Net::Net(const std::vector<unsigned> &topology) {
44+
unsigned numLayers = topology.size();
45+
for (unsigned layerNum = 0; layerNum < numLayers; layerNum++) {
46+
m_layers.push_back(Layer());
47+
unsigned numOutputs =
48+
layerNum == numLayers - 1 ? 0 : topology[layerNum + 1];
49+
50+
for (unsigned neuronNumber = 0; neuronNumber <= topology[layerNum];
51+
neuronNumber++) {
52+
m_layers.back().push_back(Neuron(numOutputs));
53+
std::cout << "Made a new Neuron!" << std::endl;
54+
}
55+
}
56+
}
57+
int main(int argc, char *argv[]) {
58+
std::vector<unsigned> topology;
59+
topology.push_back(3);
60+
topology.push_back(2);
61+
topology.push_back(1);
62+
Net myNet(topology);
63+
64+
std::vector<double> inputVals;
65+
myNet.feedforward(inputVals);
66+
67+
std::vector<double> targetVals;
68+
myNet.backProp(targetVals);
69+
70+
std::vector<double> resultVals;
71+
myNet.getResults(resultVals);
72+
73+
return 0;
74+
}

try3 (Vector Approach 2)/makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CXX = g++
2+
CXXFLAGS =
3+
TARGET = my_program
4+
SRCS = main.cpp classes.cpp
5+
OBJDIR = build
6+
OBJS = $(addprefix $(OBJDIR)/, $(SRCS:.cpp=.o))
7+
all: $(TARGET)
8+
9+
$(TARGET): $(OBJS)
10+
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS)
11+
./$(TARGET)
12+
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
13+
$(CXX) $(CXXFLAGS) -c $< -o $@
14+
$(OBJDIR):
15+
mkdir -p $(OBJDIR)
16+
run: $(TARGET)
17+
./$(TARGET)
18+
clean:
19+
rm -rf $(OBJDIR) $(TARGET)
66.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)