optimizer.c 1.3 KB
Newer Older
1 2 3 4
#include <stdlib.h>

#include "optimizer.h"

5
typedef int (*update_func)(void*, void *, paddle_element_type, const void*, int);
H
Helin Wang 已提交
6
typedef void (*release_func)(void*);
7

8
typedef struct paddle_optimizer{
H
Helin Wang 已提交
9 10
  update_func update;
  release_func release;
11 12
  void* optimizer;
} paddle_optimizer;
13 14

void paddle_release_optimizer(paddle_optimizer* o) {
H
Helin Wang 已提交
15
  o->release(o->optimizer);
16 17 18
  free(o);
}

19
int paddle_update_parameter(paddle_optimizer* o, void *buffer, paddle_element_type element_type, const void* gradient, int num_bytes) {
H
Helin Wang 已提交
20
  return o->update(o->optimizer, buffer, element_type, gradient, num_bytes);
21 22 23 24 25 26
}

typedef struct {
  double learning_rate;
} SGD_optimizer;

H
Helin Wang 已提交
27 28
int update_SGD(void* optimizer, void *buffer, paddle_element_type element_type, const void* gradient, int num_bytes) {
  SGD_optimizer* o = (SGD_optimizer*)optimizer;
29 30 31
  // TODO
  return 0;
}
32

H
Helin Wang 已提交
33 34 35 36 37
void release_SGD(void *optimizer) {
    SGD_optimizer* o = (SGD_optimizer*)optimizer;
    // nothing allocated on heap
}

38 39 40 41
paddle_optimizer* paddle_create_SGD_optimizer(double learning_rate) {
  SGD_optimizer* o = (SGD_optimizer*)malloc(sizeof(SGD_optimizer));
  o->learning_rate = learning_rate;
  paddle_optimizer* container = (paddle_optimizer*)malloc(sizeof(paddle_optimizer));
H
Helin Wang 已提交
42 43
  container->update = update_SGD;
  container->release = release_SGD;
44 45 46
  container->optimizer = o;
  return container;
}