LoginSignup
0
1

More than 5 years have passed since last update.

概要

cで、ニューラルネットワークやってみた。
sin問題やってみた。

ライブラリー

写真

image.png

サンプルコード

#include "catseye.h"

#define M_PI            3.1415926535

int main()
{
    int sample = 360;
    int u[] = {
        CATS_LINEAR, CATS_ACT_IDENTITY, 1, 1, 0, 0, 0, 500,
        CATS_LINEAR, CATS_ACT_SIGMOID, 1, 100, 0, 0, 0, 0,
        CATS_LINEAR, CATS_ACT_IDENTITY, 1, 1, 0, 0, 0, CATS_LOSS_MSE,
    };
    int layers = sizeof(u) / sizeof(int) / LPLEN;
    CatsEye cat;
    CatsEye__construct(&cat, 0, 0, layers, u);
    double x[sample];
    for (int i = 0; i < sample; i++) x[i] = 2.0 * M_PI / sample * i;
    double t[sample];
    for (int i = 0; i < sample; i++) t[i] = sin(x[i]);
    printf ("Starting training using (stochastic) gradient descent\n");
    CatsEye_train(&cat, x, t, sample, 2000, 0.01);
    printf ("Training complete\n");
    CatsEye_save(&cat, "sin.weights");
    FILE * fp = fopen("sin.csv", "w");
    if (fp == NULL) return -1;
    for (int i = 0; i < sample; i++) 
    {
        CatsEye_forward(&cat, x + i);
        fprintf (fp, "%lf, %lf, %lf\n", x[i], sin(x[i]), cat.o[2][0]);
    }
    CatsEye__destruct(&cat);
    return 0;
}

以上。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1