Skip to content

Commit 6a14c4c

Browse files
committed
totally working training loop with nothing wrong
1 parent b19774e commit 6a14c4c

2 files changed

Lines changed: 39 additions & 63 deletions

File tree

src/layers/DenseLayer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace TensorSANN{
77
DenseLayer::DenseLayer(size_t input_size, size_t output_size){
88
weights_ = Tensor({input_size, output_size});
9-
biases_ = Tensor({output_size});
9+
biases_ = Tensor({1, output_size}); // YEAH THIS MIGHT BE A PROBLEM LATER
1010

1111
std::random_device rd;
1212
std::mt19937 gen(rd());
@@ -55,10 +55,10 @@ namespace TensorSANN{
5555
}
5656

5757
void DenseLayer::update_weights_biases(float learning_rate){
58-
// std::cout << weights_.to_string() << std:: endl;
59-
// std::cout << (*(weights_.grad())).to_string() << std:: endl;
60-
// std::cout << biases_.to_string() << std:: endl;
61-
// std::cout << (*(biases_.grad())).to_string() << std:: endl;
58+
std::cout << weights_.to_string() << std:: endl;
59+
std::cout << (*(weights_.grad())).to_string() << std:: endl;
60+
std::cout << biases_.to_string() << std:: endl;
61+
std::cout << (*(biases_.grad())).to_string() << std:: endl;
6262
weights_ = weights_ - ((*(weights_.grad())) * learning_rate);
6363
biases_ = biases_ - ((*(biases_.grad())) * learning_rate);
6464

src/model_main.cpp

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,75 +36,51 @@ int main() {
3636
3737
*/
3838

39-
TensorSANN::DenseLayer dense1(16, 2);
40-
TensorSANN::ReLU relu;
41-
TensorSANN::DenseLayer dense2(2, 1);
42-
TensorSANN::Softmax smax;
43-
4439
std::vector<std::shared_ptr<TensorSANN::Layer>> layers;
4540
layers.push_back(std::make_shared<TensorSANN::DenseLayer>(16, 4));
4641
layers.push_back(std::make_shared<TensorSANN::ReLU>());
4742
layers.push_back(std::make_shared<TensorSANN::DenseLayer>(4, 2));
4843
layers.push_back(std::make_shared<TensorSANN::Softmax>());
4944

50-
TensorSANN::Tensor fwd_op = input_tensor.transpose();
51-
52-
// fwd
53-
for (auto &layer : layers){
54-
fwd_op = layer->forward(fwd_op);
55-
std::cout << "LOOP FWD==" << fwd_op.to_string() << std::endl;
56-
}
5745

58-
TensorSANN::SGD optimizer = TensorSANN::SGD(0.01f);
46+
int epoch = 2000;
47+
for (int i = 1; i <= epoch; ++i){
48+
TensorSANN::Tensor fwd_op = input_tensor.transpose();
49+
// fwd
50+
for (auto &layer : layers){
51+
fwd_op = layer->forward(fwd_op);
52+
std::cout << "\nLOOP FWD==" << fwd_op.to_string() << std::endl;
53+
}
5954

60-
// calculate loss
61-
TensorSANN::Tensor loss_grad = fwd_op - 1;
62-
std::cout << "LOSS GRAD==" << loss_grad.to_string() << std::endl;
63-
// bkwd
64-
for (int i = layers.size()-2; i >= 0; --i){
65-
loss_grad = layers[i]->backward(loss_grad);
66-
std::cout << "LOOP BKWD==" << loss_grad.shape()[0] << loss_grad.shape()[1] << std::endl;
67-
if ((*layers[i]).isTrainable()){
68-
auto yup = std::dynamic_pointer_cast<TensorSANN::DenseLayer>(layers[i]);
69-
std::string weight_str = yup->weights().to_string();
70-
std::cout << "L---> weight_g" << weight_str.substr(0, 50) << "..." << std::endl;
71-
std::string bias_str = yup->biases().to_string();
72-
std::cout << "L---> bias_g" << bias_str.substr(0, 50) << "..." << std::endl;
73-
// std::cout << "L---> bias-g" << yup.biases().to_string() << std::endl;
55+
TensorSANN::SGD optimizer = TensorSANN::SGD(0.01f);
56+
57+
// calculate loss
58+
TensorSANN::Tensor loss_grad = fwd_op - 1;
59+
std::cout << "\nLOSS GRAD==" << loss_grad.to_string() << std::endl;
60+
// bkwd
61+
for (int i = layers.size()-2; i >= 0; --i){
62+
loss_grad = layers[i]->backward(loss_grad);
63+
std::cout << "\nLOOP BKWD==" << loss_grad.shape()[0] << loss_grad.shape()[1] << std::endl;
64+
if ((*layers[i]).isTrainable()){
65+
auto yup = std::dynamic_pointer_cast<TensorSANN::DenseLayer>(layers[i]);
66+
std::string weight_str = yup->weights().to_string();
67+
std::cout << "L---> weight_g" << weight_str.substr(0, 50) << "..." << std::endl;
68+
std::string bias_str = yup->biases().to_string();
69+
std::cout << "L---> bias_g" << bias_str.substr(0, 50) << "..." << std::endl;
70+
// std::cout << "L---> bias-g" << yup.biases().to_string() << std::endl;
71+
}
72+
7473
}
7574

76-
}
77-
78-
// update weights
79-
// int i =0;
80-
// for (auto &layer : layers){
81-
// if (i == 0) continue;
82-
// // std::cout << ++i << std::endl;
83-
// optimizer.update(*layer);
84-
// }
85-
86-
87-
const TensorSANN::Tensor d1w = dense1.weights();
88-
const TensorSANN::Tensor d1b = dense1.biases();
89-
90-
std::cout << input_tensor.to_string() << std::endl;
91-
std::cout << d1w.to_string() << std::endl;
92-
std::cout << d1b.to_string() << std::endl;
93-
94-
TensorSANN::Tensor f1 = dense1.forward(input_tensor.transpose());
95-
std::cout << f1.to_string() << std::endl;
96-
97-
98-
TensorSANN::Tensor r1 = relu.forward(f1);
99-
std::cout << r1.to_string() << std::endl;
100-
101-
75+
// update weights
76+
// int i =0;
77+
for (auto &layer : layers){
78+
// if (i == 0) continue;
79+
// std::cout << ++i << std::endl;
80+
optimizer.update(*layer);
81+
}
10282

103-
TensorSANN::Tensor f2 = dense2.forward(r1);
104-
std::cout << f2.to_string() << std::endl;
105-
106-
TensorSANN::Tensor s1 = smax.forward(f2);
107-
std::cout << (s1 - 1).to_string() << std::endl;
83+
}
10884

10985
return 0;
11086

0 commit comments

Comments
 (0)