LoginSignup
0
0

More than 3 years have passed since last update.

kelpnetの作法 その7

Last updated at Posted at 2019-07-03

概要

kelpnetの作法を調べてみた。
ライントレースやってみる。
学習してみた。

結果

image.png

サンプルコード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KelpNet;
using System.IO;

namespace ConsoleApp4
{
    class Program
    {
        const int EPOCH = 3001;       
        static void Main(string[] args)
        {
            int N = 2000;
            Real[][] trainData = new Real[N][];
            Real[][] trainLabel = new Real[N][];
            int[] Label = new int[N];
            int i = 0;
            Console.WriteLine("Read Start...");
            using (StreamReader sr = new StreamReader(@"test.csv"))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] items = line.Split(',');
                    trainData[i] = new Real[] {
                        double.Parse(items[0]),
                        double.Parse(items[1]),
                        double.Parse(items[2]),
                        double.Parse(items[3]),
                        double.Parse(items[4])
                    };
                    trainLabel[i] = new Real[] {
                        int.Parse(items[5])
                    };
                    Label[i] = int.Parse(items[5]);
                    i++;
                }
            }
            N = i;
            FunctionStack nn = new FunctionStack(new Linear(5, 20, name: "in"), new Sigmoid(name: "act"), new Linear(20, 3, name: "out"));
            nn.SetOptimizer(new SGD());
            Console.WriteLine("Train Start...");
            for (i = 0; i < EPOCH; i++)
            {
                Real loss = 0;
                for (int j = 0; j < N; j++)
                {
                    loss += Trainer.Train(nn, trainData[j], trainLabel[j], new SoftmaxCrossEntropy());
                }
                if (i % 100 == 0)
                {
                    Console.WriteLine("loss:" + loss / N);
                }
            }
            Console.WriteLine("Save Start...");
            ModelIO.Save(nn, "test.nn");
            Console.WriteLine("Load Start...");
            Function testnn = ModelIO.Load("test.nn");
            Console.WriteLine("Test Start...");
            float ac = 0;
            for (int j = 0; j < N; j++)
            {
                NdArray result = testnn.Predict(trainData[j])[0];
                int resultIndex = Array.IndexOf(result.Data, result.Data.Max());
                if (resultIndex == Label[j]) ac++;
            }
            Console.Write("正解率 ");
            Console.WriteLine(ac / N * 100);
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

以上。

0
0
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
0