First deep learning in C #-Imitating implementation in Python-

Deep learning in C # (matrix calculation)

This time, I created only a program that calculates the matrix required for deep learning. The book I referred to was "Make from scratch Deep Learning (O'Reilly)". I recommend this book because it is very easy to understand and I think it is a good book. It's not a manga, but it doesn't stop ... I was so interested. I've only read about half of it yet, so there are many things I don't understand, but this time I made a matrix calculation program in C #.

C #, not Python?

This book also explains the theory, so I wanted to make it ** from scratch ** in C # without relying on the Python library. I'll try to implement it in F # and Haskell later.

Why did you start deep learning?

I recently started studying functional languages such as F # and Haskell, and I still can't understand the meaning of using functional languages. "By the way, functional languages have come closer to mathematical ideas." Then, deep learning! (The motive was that I didn't know how to use a functional language.) For the time being, I did it in C #.

C # source code

Matrix.cs


namespace Matrix
{
    class Mat
    {
        private int r = 0;
        public int R
        {
            get { return r; }
        }
        private int c = 0;
        public int C
        {
            get { return c; }
        }
        private bool err = false;
        public bool Err
        {
            get { return err; }
        }
        private double[][] matrix_data;
        public double[][] Matrix_data
        {
            get {
                double[][] a = new double[2][];
                a[0] = new double[] { 0, 0 };
                a[1] = new double[] { 0, 0 };
                if (err) return a;
                else return matrix_data;
            }
            set
            {
                matrix_data = value;
            }
        }
        public Mat(params double[][] vs)
        {
            int len = vs[0].Length;

            for (int i = 0; i < vs.Length; i++)
            {
                if (i != 0 && len != vs[i].Length)
                {
                    err = true;
                }
            }
            if (!err)
            {
                r = vs.Length;
                c = vs[0].Length;
                matrix_data = vs;
            }
        }
        public static double[][] operator +(Mat p1, Mat p2)
        {
            double[][] d = new double[p1.R][];

            if (p1.C == p2.C && p1.R == p2.R)
            {
                for (int i = 0; i < p1.R; i++)
                {
                    d[i] = new double[p1.C];
                    for (int j = 0; j < p1.C; j++)
                    {
                        d[i][j] = p1.Matrix_data[i][j] + p2.Matrix_data[i][j];
                    }
                }
            }
            else
            {
                for (int k = 0; k < p1.R; k++)
                {
                    d[k] = new double[2] { 0, 0 };
                }
            }

            return d;
        }
        public static double[][] operator +(double p1, Mat p2)
        {
            double[][] d = new double[p2.R][];
            for (int i = 0; i < p2.R; i++)
            {
                d[i] = new double[p2.C];
                for (int j = 0; j < p2.C; j++)
                {
                    d[i][j] = p2.Matrix_data[i][j] + p1;
                }
            }

            return d;
        }
        public static double[][] operator +(Mat p1, double p2)
        {
            double[][] d = new double[p1.R][];
            for (int i = 0; i < p1.R; i++)
            {
                d[i] = new double[p1.C];
                for (int j = 0; j < p1.C; j++)
                {
                    d[i][j] = p1.Matrix_data[i][j] + p2;
                }
            }

            return d;
        }
        public static double[][] operator -(Mat p1, Mat p2)
        {
            double[][] d = new double[p1.R][];

            if (p1.C == p2.C && p1.R == p2.R)
            {
                for (int i = 0; i < p1.R; i++)
                {
                    d[i] = new double[p1.C];
                    for (int j = 0; j < p1.C; j++)
                    {
                        d[i][j] = p1.Matrix_data[i][j] - p2.Matrix_data[i][j];
                    }
                }
            }
            else
            {
                for (int k = 0; k < p1.R; k++)
                {
                    d[k] = new double[2] { 0, 0 };
                }
            }

            return d;
        }
        public static double[][] operator -(double p1, Mat p2)
        {
            double[][] d = new double[p2.R][];
            for (int i = 0; i < p2.R; i++)
            {
                d[i] = new double[p2.C];
                for (int j = 0; j < p2.C; j++)
                {
                    d[i][j] = p1 - p2.Matrix_data[i][j];
                }
            }

            return d;
        }
        public static double[][] operator -(Mat p1, double p2)
        {
            double[][] d = new double[p1.R][];
            for (int i = 0; i < p1.R; i++)
            {
                d[i] = new double[p1.C];
                for (int j = 0; j < p1.C; j++)
                {
                    d[i][j] = p1.Matrix_data[i][j] - p2;
                }
            }

            return d;
        }
        public static double[][] dot(Mat p1, Mat p2)
        {
            double[][] d = new double[p1.R][];
            double temp = 0;

            if (p1.C == p2.R)
            {
                for (int i = 0; i < p1.R; i++)
                {
                    d[i] = new double[p2.C];
                    for (int j = 0; j < p2.C; j++)
                    {
                        for(int a = 0; a < p1.C; a++)
                        {
                            temp = temp + p1.Matrix_data[i][a] * p2.Matrix_data[a][j];
                        }
                        d[i][j] = temp;
                        temp = 0.0;
                    }
                }
            }
            else
            {
                for (int k = 0; k < p1.R; k++)
                {
                    d[k] = new double[2] { 0, 0 };
                }
            }

            return d;
        }
    }
}

Class Mat is a class that calculates a matrix. The usage example is as follows.

Program.cs


namespace Matrix
{
    class Program
    {
        static void Main(string[] args)
        {
            Mat A = new Mat(
                new double[] { 1, 2, 3 },
                new double[] { 2, 3, 4 }) ,
                B = new Mat(
                new double[] { 1, 2 },
                new double[] { 2, 3 },
                new double[] { 3, 4 });

            double[][] ans = Mat.dot(A, B);

            _ = 1;    //Tammy program to set breakpoints

        }
    }
}

It was a hassle to output to this sole, so I put a breakpoint on the line _ = 1; and checked the contents of the array ʻans`.

無題.png

It is calculated properly. This time, the dot product is calculated with Mat, but you can also add and subtract.

Recommended Posts

First deep learning in C #-Imitating implementation in Python-
Python Deep Learning
Deep learning × Python
First Deep Learning ~ Struggle ~
RNN implementation in python
ValueObject implementation in Python
First Deep Learning ~ Preparation ~
Next Python in C
First Deep Learning ~ Solution ~
Python: Deep Learning in Natural Language Processing: Basics
Python: Deep Learning Tuning
[Implementation for learning] Implement Stratified Sampling in Python (1)
C API in Python 3
Deep Learning from scratch-Chapter 4 tips on deep learning theory and implementation learned in Python
SVM implementation in python
Python: Deep learning in natural language processing: Implementation of answer sentence selection system
Deep Learning Experienced in Python Chapter 2 (Materials for Journals)
Extend python in C ++ (Boost.NumPy)
Binary search in Python / C ++
Neural network implementation in python
Implementation of quicksort in Python
Deep reinforcement learning 2 Implementation of reinforcement learning
First simple regression analysis in Python
Sorting algorithm and implementation in Python
HMM parameter estimation implementation in python
Mixed normal distribution implementation in python
Implementation of life game in Python
Widrow-Hoff learning rules implemented in Python
Solve ABC036 A ~ C in Python
How to wrap C in Python
Deep learning learned by implementation 1 (regression)
Python: Preprocessing in Machine Learning: Overview
C / C ++ programmer challenges Python (first step)
Deep learning image recognition 2 model implementation
Implementation of original sorting in Python
Solve ABC037 A ~ C in Python
Write C unit tests in Python
The first step in Python Matplotlib
(python) Deep Learning Library Chainer Basics Basics
Differences C # engineers felt when learning python for the first time
Generate a first class collection in Python
Deep learning learned by implementation 2 (image classification)
Othello-From the tic-tac-toe of "Implementation Deep Learning" (3)
Algorithm in Python (ABC 146 C Binary Search
Implementation module "deque" in queue and Python
Implement FIR filters in Python and C
[python] Frequently used techniques in machine learning
Write O_SYNC file in C and Python
Python: Preprocessing in machine learning: Data acquisition
[Python] First data analysis / machine learning (Kaggle)
python learning
MongoDB for the first time in Python
Python: Gender Identification (Deep Learning Development) Part 1
Python: Gender Identification (Deep Learning Development) Part 2
"Deep Learning from scratch" in Haskell (unfinished)
[Python] Saving learning results (models) in machine learning
Python: Preprocessing in machine learning: Data conversion
Deep Learning
Python vs Ruby "Deep Learning from scratch" Chapter 4 Implementation of loss function
Run Python in C ++ on Visual Studio 2017
Othello-From the tic-tac-toe of "Implementation Deep Learning" (2)