Skip to content

Commit 221a228

Browse files
author
fm94
committed
have a totally functionning forward pass
1 parent be266d4 commit 221a228

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tipousi - a tiny and fast deep learning framework from scratch in C++
22

3-
[![Tipous Build Status](https://github.com/fm94/Tipousi/actions/workflows/cmake-multi-platform.yml/badge.svg?branch=master)](https://github.com/fm94/Tipousi/actions/workflows/cmake-multi-platform.yml)
3+
[![Tipousi Build Status](https://github.com/fm94/Tipousi/actions/workflows/cmake-multi-platform.yml/badge.svg?branch=master)](https://github.com/fm94/Tipousi/actions/workflows/cmake-multi-platform.yml)
44

55
This is a WIP project that implements an entire deep learning framework from scratch in C++.</br>
66
The goal is to implement as many layers and modules as possible and have a python interface at the end.</br>

Diff for: include/graph/sequential.hpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ namespace Tipousi
1818
// travers all nodes in backward pass
1919
// and delete all of them sequentially
2020
Node *current_node = m_output_node;
21-
bool all_cleaned = false;
22-
while (!all_cleaned)
21+
while (true)
2322
{
24-
// TODO : hacky approch deleting only first node -> should
25-
// be recursive
26-
Node *next_cleaned = current_node->get_inputs()[0];
23+
// TODO : hacky approch deleting only first node
24+
// -> should be recursive
25+
auto input_nodes = current_node->get_inputs();
26+
if (input_nodes.size() == 0 || !input_nodes[0])
27+
{
28+
break;
29+
}
2730
delete current_node;
28-
current_node = next_cleaned;
31+
current_node = input_nodes[0];
2932
}
3033
}
3134

Diff for: src/activation/softmax.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ namespace Tipousi
1010
{
1111
Eigen::MatrixXf expX = in.array().exp();
1212
Eigen::VectorXf sumExpX = expX.rowwise().sum();
13-
out =
14-
(expX.array().rowwise() / sumExpX.transpose().array()).matrix();
13+
out = (expX.array().colwise() / sumExpX.array()).matrix();
1514
}
1615

1716
void Softmax::backward(const Eigen::MatrixXf &dout,

Diff for: src/graph/sequential.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ namespace Tipousi
1616
// while keeping the original data intact
1717
Eigen::MatrixXf data_copy = in;
1818
Node *current_node = m_input_node;
19-
bool is_finished = false;
20-
while (!is_finished)
19+
// continue until no more nodes
20+
while (true)
2121
{
2222
if (current_node)
2323
{
2424
// TODO hacky approachs: always take number 0
2525
auto output_nodes = current_node->get_outputs();
2626
if (output_nodes.size() == 0 || !output_nodes[0])
2727
{
28-
is_finished = true;
28+
break;
2929
}
3030
current_node->forward(data_copy);
3131
current_node = output_nodes[0];
3232
}
3333
}
34+
out = data_copy;
3435
}
3536

3637
void Sequential::backward()

Diff for: tests/main.cpp

+18-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "graph/sequential.hpp"
55
#include "layer/dense.hpp"
66
#include "loss/mse.hpp"
7+
#include <chrono>
8+
#include <iostream>
79
#include <memory>
810

911
using namespace Tipousi;
@@ -14,12 +16,15 @@ using namespace Loss;
1416

1517
void test_create_net()
1618
{
19+
int n_features{2};
20+
int n_labels{2};
21+
1722
// create layer nodes
1823
// these are raw ptrs and ownership will go to the graph,
1924
// it is responsable for cleaning them!
20-
Node *node1 = Node::create<Dense>(5, 32);
25+
Node *node1 = Node::create<Dense>(n_features, 32);
2126
Node *node2 = Node::create<ReLU>();
22-
Node *node3 = Node::create<Dense>(32, 1);
27+
Node *node3 = Node::create<Dense>(32, n_labels);
2328
Node *node4 = Node::create<Softmax>();
2429

2530
// build the dependencies
@@ -32,13 +37,21 @@ void test_create_net()
3237

3338
// test inference
3439
int n_samples{32};
35-
int n_features{2};
36-
int n_labels{1};
3740
auto features = Eigen::MatrixXf::Random(n_samples, n_features);
3841
auto labels = Eigen::MatrixXf(n_samples, n_labels);
39-
// forward pass
42+
43+
// forward pass with time measurement
4044
Eigen::MatrixXf preds;
45+
auto start = std::chrono::high_resolution_clock::now();
4146
net.forward(features, preds);
47+
auto end = std::chrono::high_resolution_clock::now();
48+
auto duration =
49+
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
50+
.count();
51+
52+
std::cout << "Forward pass execution time: " << duration << " microseconds"
53+
<< std::endl;
54+
4255
// backward pass
4356
net.backward();
4457
}

0 commit comments

Comments
 (0)