test.cpp是一份用于测试该库性能的代码,使用MINST对其进行测试。
<code class="language-c"> printf("===========\nMINST_READ\n===========\n"); printf("--\nReading training images.\n"); ImgArr train_img = read_Img("./MINST/train-images.idx3-ubyte"); printf("Read training images finished.\n"); printf("Training images data:\n"); printf("%d images.\n",train_img->ImgNum); printf("--\nReading testing images.\n"); printf("Size : %d*%d.\n",train_img->ImgPtr[0].c,train_img->ImgPtr[0].r); ImgArr test_img = read_Img("./MINST/t10k-images.idx3-ubyte"); printf("Read testing images finished.\n"); printf("Testing images data:\n"); printf("%d images.\n",test_img->ImgNum); printf("Size : %d*%d.\n",test_img->ImgPtr[0].c,test_img->ImgPtr[0].r); printf("--\nReading training lables.\n"); LabelArr train_label = read_Lable("./MINST/train-labels.idx1-ubyte"); printf("Read training lables finished.\n"); printf("Training lables data:\n"); printf("%d lables.\n",train_label->LabelNum); printf("--\nReading testing lables.\n"); LabelArr test_label = read_Lable("./MINST/t10k-labels.idx1-ubyte"); printf("Read testing lables finished.\n"); printf("Testing lables data:\n"); printf("%d lables.\n",test_label->LabelNum); </code>
这一段代码用于读取MINST数据集。
<code class="language-c">CNN.init(); </code>
该函数的作用是初始化卷积神经网络。
<code class="language-c"> for (int i=0;i<train_img->ImgNum;i++){ if ((i%25)==0){ printf("%d\n",i); } input_minst(train_img->ImgPtr[i]); do { CNN.train(train_label->LabelPtr[i].LabelData,0.0005,0.0005,0.0005); CNN.calculate(); }while (sort(train_label->LabelPtr[i].LabelData)!=sort(CNN.FC_9.y)); } </train_img-></code>
这是用于训练的代码,在该代码外加一层循环就可以改变训练次数。
<code class="language-c"> for (int i=0;i<test_img->ImgNum;i++){ input_minst(test_img->ImgPtr[i]); CNN.calculate(); if (sort(test_label->LabelPtr[i].LabelData)!=sort(CNN.FC_9.y)){ right+=1; } } </test_img-></code>
该段代码用于计算正确率。
void input_minst(MinstImg input);
该函数将MINST数据输入到神经网络中;
int sort(float *data);
该函数用于计算输出数组中最大值的序号。
200字以内,仅用于支线交流,主线讨论请采用回复功能。