-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynap.c
99 lines (84 loc) · 2.73 KB
/
synap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <Python.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include "header_files/dense_layer.h"
#include "header_files/activation.h"
#include "header_files/ndarray.h"
#include "header_files/num.h"
#include "header_files/metric.h"
#include "header_files/loss.h"
#include "header_files/nn.h"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
static NdArray build_ndArray(PyArrayObject *array){
double* data = (double*)PyArray_DATA(array);
int nd = PyArray_NDIM(array);
npy_intp* dims = PyArray_DIMS(array);
int *shape=(int*)calloc(INT_SIZE,nd);
for(int i=0;i<nd;i++) shape[i]=dims[i];
NdArray arr= NdArray__init__(data,shape,nd);
return arr;
}
static PyObject* network_init(PyObject* self, PyObject* args) {
int num_layers;
if (!PyArg_ParseTuple(args, "i", &num_layers)) {
return NULL;
}
Network *nn = Network__init__(num_layers);
return PyCapsule_New(nn, NULL, NULL);
}
static PyObject* compile_network(PyObject* self, PyObject* args) {
PyObject* network_capsule;
PyArrayObject* X_train_obj;
if (!PyArg_ParseTuple(args, "OO", &network_capsule, &X_train_obj)) {
return NULL;
}
Network *nn = PyCapsule_GetPointer(network_capsule, NULL);
NdArray X_train=build_ndArray(X_train_obj);
_compile(nn, X_train);
Py_RETURN_NONE;
}
static PyObject* add(PyObject* self, PyObject* args) {
PyObject* network_capsule;
int neurons;
if (!PyArg_ParseTuple(args, "Oi", &network_capsule, &neurons)) {
return NULL;
}
Network *nn = PyCapsule_GetPointer(network_capsule, NULL);
nn_add(nn,neurons);
Py_RETURN_NONE;
}
static PyObject* train_network(PyObject* self, PyObject* args) {
PyObject* network_capsule;
PyArrayObject* X_train_obj;
PyArrayObject* y_train_obj;
int epochs;
if (!PyArg_ParseTuple(args, "OOOi", &network_capsule, &X_train_obj, &y_train_obj, &epochs)) {
return NULL;
}
Network *nn = PyCapsule_GetPointer(network_capsule, NULL);
NdArray X_train=build_ndArray(X_train_obj);
NdArray y_train=build_ndArray(y_train_obj);
train(nn, X_train, y_train, epochs);
Py_RETURN_NONE;
}
static PyMethodDef NetworkMethods[] = {
{"init", network_init, METH_VARARGS, "Initialize network"},
{"compile", compile_network, METH_VARARGS, "Compile network"},
{"train", train_network, METH_VARARGS, "Train network"},
{"add", add, METH_VARARGS, "add layer"},
{NULL, NULL, 0, NULL} // Sentinel
};
static struct PyModuleDef synapc = {
PyModuleDef_HEAD_INIT,
"synapc",
"Module for network operations",
-1,
NetworkMethods
};
PyMODINIT_FUNC PyInit_synapc(void) {
return PyModule_Create(&synapc);
}