Skip to content

Commit

Permalink
第一次成功整合了卷积核的bias,达到89750新高:# train_num 3000, test_num 400, epoch 300,…
Browse files Browse the repository at this point in the history
… lr 0.03-100-0.003, filter_bias G(0, 0.2), minus_maxi True, filter_bias_update True
  • Loading branch information
ThomasAtlantis committed Dec 31, 2019
1 parent 5fe45ec commit 058c16b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
8 changes: 2 additions & 6 deletions include/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ _type convMatrix(CVLayer * input, CVLayer * conv, int k, int x, int y, int index
*/
void UpdateFilter(CVLayer * filter[], CVLayer * input, CVLayer * conv, double alpha) {
for (int index = 0; index < filter_num; ++ index) {
// filter[index]->bias -= alpha * sumMatrix(conv, index);
filter[index]->bias -= alpha * sumMatrix(conv, index);
for (int k = 0; k < filter[index]->H; ++ k)
for (int i = 0; i < filter[index]->L; ++ i)
for (int j = 0; j < filter[index]->W; ++ j)
Expand All @@ -232,11 +232,7 @@ void UpdateFilter(CVLayer * filter[], CVLayer * input, CVLayer * conv, double al
* @param alpha 学习率参数
* @param label Ground Truth标签
*/
void backPropagation(Network * CNN, int step, Alpha * alpha, int label) {
for (int i = 0; i < class_num; ++ i) {
CNN->fc_output->deltas[i] = CNN->fc_output->values[i];
if (i == label) CNN->fc_output->deltas[i] -= 1.0;
}
void backPropagation(Network * CNN, int step, Alpha * alpha) {
for (int i = 0; i < CNN->fc_input->L; ++ i) {
CNN->fc_input->deltas[i] = 0;
for (int j = 0; j < class_num; ++ j) {
Expand Down
2 changes: 1 addition & 1 deletion include/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ CVLayer * Convol2D_(int h, int l, int w) {

CVLayer * Filter2D_(int h, int l, int w) {
CVLayer * layer = (CVLayer *)malloc(sizeof(CVLayer));
layer->destroy = killCVLayer; layer->bias = GaussRand(2.75, 0.2);
layer->destroy = killCVLayer; layer->bias = GaussRand(0.1, 0.2);
layer->L = l; layer->W = w; layer->H = h; layer->deltas = NULL;
layer->values = (_type ***)malloc(h * sizeof(_type **));
for (int k = 0; k < h; ++ k) {
Expand Down
Binary file added model-20191231-8975.sav
Binary file not shown.
Binary file removed model.sav
Binary file not shown.
25 changes: 18 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
return 0; \
}while(0)

int train_num = 3000; // 训练样本数
int test_num = 400; // 测试样本数
int train_num = 6000; // 训练样本数
int test_num = 600; // 测试样本数
int epoch_num = 300; // 训练轮数
int batch_size = 1;

Vector1D labels_train;
Vector2D images_train;
Expand Down Expand Up @@ -45,10 +46,20 @@ void train(Network * CNN, Alpha * alpha, const char * fileName) {
printf("Begin Training ...\n");
for (int step = 0; step < epoch_num; ++ step) {
_type err = 0;
for (int i = 0; i < train_num; i ++) {
forePropagation(CNN, i, images_train);
err -= log(CNN->fc_output->values[(int)labels_train.data[i]]);
backPropagation(CNN, step, alpha, (int)labels_train.data[i]);
for (int i = 0; i < train_num / batch_size; i ++) {
for (int k = 0; k < class_num; ++ k)
CNN->fc_output->deltas[k] = 0;
for (int j = 0; j < batch_size; ++ j) {
forePropagation(CNN, i * batch_size + j, images_train);
int label = (int)labels_train.data[i * batch_size + j];
for (int k = 0; k < class_num; ++ k)
CNN->fc_output->deltas[k] += CNN->fc_output->values[k];
CNN->fc_output->deltas[label] -= 1.0;
err -= log(CNN->fc_output->values[label]);
}
for (int k = 0; k < class_num; ++ k)
CNN->fc_output->deltas[k] /= batch_size;
backPropagation(CNN, step, alpha);
}
new_score = test(CNN);
printf("step: %3d loss: %.5f prec: %.5f\n",
Expand Down Expand Up @@ -102,7 +113,7 @@ int main(int argc, char * argv[]) {
images_test.data[i][j] /= 255.0;

// 训练
train(CNN, alpha, "model.sav");
train(CNN, alpha, "../model.sav");

// 释放内存
delete_p(CNN);
Expand Down

0 comments on commit 058c16b

Please sign in to comment.