I'm trying to implement neural network and deep learning code in C#. Sample code in my text book is written in Python, so I'm trying to convert them to C#.
My question is that calculating dot product with numpy is extremely faster than my C# code written from scratch.
While my numpy code takes a few second to calculate dot product 1000 times, my C# code takes much longer than it.
Here is my question. How can I make my C# code faster?
Here is numpy code:
C:\temp>more dot.py
from datetime import datetime
import numpy as np
W = np.random.randn(784, 100)
x = np.random.randn(100, 784)
print(datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
for i in range(0,1000):
np.dot(x, W)
print(datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
C:\temp>\Python35\python.exe dot.py
2017/02/08 00:49:14
2017/02/08 00:49:16
C:\temp>
And this is C# code:
public static double[,] dot(double[,] a, double[,] b)
{
double[,] dot = new double[a0, b1];
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
// the next loop looks way slow according to the profiler
for (int k = 0; k < b.GetLength(0); k++)
dot[i, j] += a[i, k] * b[k, j];
}
}
return dot;
}
static void Main(string[] args)
{
// compatible function with np.random.randn()
double[,] W = random_randn(784, 100);
double[,] x = random_randn(100, 784);
Console.WriteLine(DateTime.Now.ToString("F"));
for (int i = 0; i < 1000; i++)
dot(W, x);
Console.WriteLine(DateTime.Now.ToString("F"));
}
Regards,