Skip to content

Instantly share code, notes, and snippets.

@praeclarum
Created October 28, 2021 19:19
Show Gist options
  • Save praeclarum/6f739893e160e8bbb93a9650de802af1 to your computer and use it in GitHub Desktop.
Save praeclarum/6f739893e160e8bbb93a9650de802af1 to your computer and use it in GitHub Desktop.
C code implementing the neural network that I trained to control my balancing robot
#include "NNControl.h"
#include <math.h>
extern const float nncontrol_dense_74_weights0[320]; // (5, 64)
extern const float nncontrol_dense_74_weights1[64]; // (64,)
extern const float nncontrol_dense_75_weights0[4096]; // (64, 64)
extern const float nncontrol_dense_75_weights1[64]; // (64,)
extern const float nncontrol_dense_76_weights0[64]; // (64, 1)
static const float tf_math_multiply_69_Mul_0_const0[1] = {0};
void nncontrol(const float *input_26, float *tf_math_multiply_69_Mul_0, nncontrol_hidden *hidden) {
for (int i = 0; i < 64; i++) {
hidden->dense_74_Relu_0[i] = nncontrol_dense_74_weights1[i];
for (int j = 0; j < 5; j++) {
hidden->dense_74_Relu_0[i] += input_26[j]*nncontrol_dense_74_weights0[j*64 + i];
}
}
for (int i = 0; i < 64; i++) {
if (hidden->dense_74_Relu_0[i] < 0.0f) hidden->dense_74_Relu_0[i] = 0.0f;
}
for (int i = 0; i < 64; i++) {
hidden->dense_75_Relu_0[i] = nncontrol_dense_75_weights1[i];
for (int j = 0; j < 64; j++) {
hidden->dense_75_Relu_0[i] += hidden->dense_74_Relu_0[j]*nncontrol_dense_75_weights0[j*64 + i];
}
}
for (int i = 0; i < 64; i++) {
if (hidden->dense_75_Relu_0[i] < 0.0f) hidden->dense_75_Relu_0[i] = 0.0f;
}
for (int i = 0; i < 1; i++) {
hidden->dense_76_Tanh_0[i] = 0.0f;
for (int j = 0; j < 64; j++) {
hidden->dense_76_Tanh_0[i] += hidden->dense_75_Relu_0[j]*nncontrol_dense_76_weights0[j*1 + i];
}
}
*hidden->dense_76_Tanh_0 = tanh(*hidden->dense_76_Tanh_0);
*tf_math_multiply_69_Mul_0 = *hidden->dense_76_Tanh_0 * 0.3f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment