1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

kelpnetの作法

Last updated at Posted at 2019-06-29

概要

kelpnetの作法を調べてみた。

部品

ロス - SoftmaxCrossEntropy MeanSquaredError

オプチマイザー - MomentumSGD SGD Adam AdaGrad

アクチベーション - Sigmoid ReLU TanhActivation

サンプルコード

xor

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            const int learningCount = 10000;
            Real[][] trainData = {
                new Real[] { 0, 0 },
                new Real[] { 1, 0 },
                new Real[] { 0, 1 },
                new Real[] { 1, 1 }
            };
            Real[][] trainLabel = {
                new Real[] { 0 },
                new Real[] { 1 },
                new Real[] { 1 },
                new Real[] { 0 }
            };
            FunctionStack nn = new FunctionStack(new Linear(2, 2, name: "l1"), new TanhActivation(name: "act"), new Linear(2, 2, name: "l2"));
            nn.SetOptimizer(new SGD());
            Console.WriteLine("1 Training...");

            for (int i = 0; i < learningCount; i++)
            {
                Real loss = 0;
                for (int j = 0; j < trainData.Length; j++)
                {
                    loss += Trainer.Train(nn, trainData[j], trainLabel[j], new SoftmaxCrossEntropy());
                }
                if (i % 1000 == 0)
                {
                    Console.WriteLine("loss: " + loss / 4);
                }
            }
            Console.WriteLine("Test Start...");
            foreach (Real[] input in trainData)
            {
                NdArray result = nn.Predict(input)[0];
                int resultIndex = Array.IndexOf(result.Data, result.Data.Max());
                Console.WriteLine(input[0] + " xor " + input[1] + " = " + resultIndex + " " + result);
            }
            ModelIO.Save(nn, "test.nn");
            Function testnn = ModelIO.Load("test.nn");
            Console.WriteLine("Test Start...");
            foreach (Real[] input in trainData)
            {
                NdArray result = testnn.Predict(input)[0];
                int resultIndex = Array.IndexOf(result.Data, result.Data.Max());
                Console.WriteLine(input[0] + " xor " + input[1] + " = " + resultIndex + " " + result);
            }
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

結果

image.png

以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?