Skip to content

Commit 5b5365d

Browse files
committed
idk where the leak is
1 parent 3aebfaa commit 5b5365d

File tree

7 files changed

+44
-49
lines changed

7 files changed

+44
-49
lines changed

Network.mojo

+6-12
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct Network:
6060

6161
@staticmethod
6262
fn relu(A: Matrix) -> Matrix:
63-
var B: Matrix = Matrix(A.rows, A.cols, True)
63+
var B: Matrix = Matrix(A.rows, A.cols)
6464
for i in range(B.rows):
6565
for j in range(B.cols):
6666
if A[i, j] > 0.01:
@@ -71,7 +71,7 @@ struct Network:
7171

7272
@staticmethod
7373
fn drelu(A: Matrix) -> Matrix:
74-
var B: Matrix = Matrix(A.rows, A.cols, True)
74+
var B: Matrix = Matrix(A.rows, A.cols)
7575
for i in range(B.rows):
7676
for j in range(B.cols):
7777
if A[i, j] > 0.01:
@@ -83,7 +83,7 @@ struct Network:
8383
@staticmethod
8484
fn tanh(A: Matrix) -> Matrix:
8585
# could need optimization alot
86-
var B: Matrix = Matrix(A.rows, A.cols, True)
86+
var B: Matrix = Matrix(A.rows, A.cols)
8787

8888
for i in range(A.rows):
8989
for j in range(A.cols):
@@ -93,7 +93,7 @@ struct Network:
9393
@staticmethod
9494
fn dtanh(A: Matrix) -> Matrix:
9595
# could need optimization alot
96-
var B: Matrix = Matrix(A.rows, A.cols, True)
96+
var B: Matrix = Matrix(A.rows, A.cols)
9797

9898
for i in range(A.rows):
9999
for j in range(A.cols):
@@ -103,9 +103,9 @@ struct Network:
103103
@staticmethod
104104
fn softmax_1d(A: Matrix) -> Matrix:
105105
# could need optimization alot
106-
var B: Matrix = Matrix(A.rows, A.cols, True)
106+
var B: Matrix = Matrix(A.rows, A.cols)
107107

108-
var row_exp_sum_mat: Matrix = Matrix(A.rows, 1, True)
108+
var row_exp_sum_mat: Matrix = Matrix(A.rows, 1)
109109
for i in range(A.rows):
110110
for j in range(A.cols):
111111
B[i, j] += exp(A[i, j])
@@ -141,19 +141,16 @@ struct Network:
141141

142142
let time_now = now()
143143
# calc output hidden layer1
144-
inputs_h1.zero()
145144
nj.matmul_vectorized(inputs_h1, inputs, self._wih)
146145
inputs_h1 = inputs_h1 + self._bih_l1
147146
inputs_h1 = self.relu(inputs_h1)
148147

149148
# calc output hidden layer 2
150-
inputs_h2.zero()
151149
nj.matmul_vectorized(inputs_h2, inputs_h1, self._whh)
152150
inputs_h2 = inputs_h2 + self._bih_l2
153151
inputs_h2 = self.tanh(inputs_h2)
154152

155153
# calc output output layer
156-
outputs.zero()
157154
nj.matmul_vectorized(outputs, inputs_h2, self._who)
158155
outputs = outputs + self._bho
159156
outputs = self.softmax_1d(outputs)
@@ -194,11 +191,8 @@ struct Network:
194191
var ih1_ho2: Matrix = Matrix(inputs_h1.cols, ho2_drelu.cols)
195192
var i_ho1: Matrix = Matrix(inputs.cols, ho1_drelu.cols)
196193

197-
ih2_o.zero()
198194
nj.matmul_vectorized(ih2_o, inputs_h2.transpose(), output_error_gradient)
199-
ih1_ho2.zero()
200195
nj.matmul_vectorized(ih1_ho2, inputs_h1.transpose(), ho2_drelu)
201-
i_ho1.zero()
202196
nj.matmul_vectorized(i_ho1, inputs.transpose(), ho1_drelu)
203197

204198
# updating weights and biases

main.mojo

+17-12
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ from python import Python
44
from time import now
55
import numjo as nj
66
from random import randn, rand
7+
from matmul import matmul_benchmark
8+
79

810
fn main() raises:
911
let input_nodes = 784
1012
let hidden_nodes_1 = 150
1113
let hidden_nodes_2 = 80
1214
let output_nodes = 10
1315
let learning_rate = 1e-4
14-
var outputs = Matrix(1, output_nodes, True)
15-
16-
var peval_nn = Network(input_nodes=input_nodes, hidden_nodes_l1=hidden_nodes_1, hidden_nodes_l2=hidden_nodes_2, output_nodes=output_nodes, learning_rate=learning_rate)
17-
# var train_nn = Network(input_nodes=input_nodes, hidden_nodes_l1=hidden_nodes_1, hidden_nodes_l2=hidden_nodes_2, output_nodes=output_nodes, learning_rate=learning_rate)
16+
var outputs = Matrix(1, output_nodes)
1817

1918
# download dataset first - https://www.kaggle.com/datasets/oddrationale/mnist-in-csv
2019
Python.add_to_path("./")
2120
let DataLoader = Python.import_module("DataLoader")
21+
let py_matmul = Python.import_module("py_matmul")
2222
let np = Python.import_module("numpy")
2323

2424
print("\nStarting python mnist data loader")
@@ -34,11 +34,11 @@ fn main() raises:
3434
mnist_train_labels = DataLoader.mnist_labels("train", output_nodes)
3535
mnist_test_labels = DataLoader.mnist_labels("", output_nodes)
3636

37-
let test_inputs: Matrix = Matrix(10000, 784, True)
38-
let test_labels: Matrix = Matrix(10000, 10, True)
37+
let test_inputs: Matrix = Matrix(10000, 784)
38+
let test_labels: Matrix = Matrix(10000, 10)
3939

40-
let train_inputs: Matrix = Matrix(60000, 784, True)
41-
let train_labels: Matrix = Matrix(60000, 10, True)
40+
let train_inputs: Matrix = Matrix(60000, 784)
41+
let train_labels: Matrix = Matrix(60000, 10)
4242

4343
print("Starting train data converter")
4444
for i in range(mnist_test_inputs.shape[0]):
@@ -62,16 +62,21 @@ fn main() raises:
6262
for j in range(mnist_test_labels.shape[1]):
6363
test_labels[i, j] = mnist_test_labels[i][j].to_float64().cast[DType.float32]()
6464

65+
py_matmul.py_matmul_benchmark()
66+
matmul_benchmark()
6567

66-
var mse: Matrix = Matrix(1, 1, True)
68+
var peval_nn = Network(input_nodes=input_nodes, hidden_nodes_l1=hidden_nodes_1, hidden_nodes_l2=hidden_nodes_2, output_nodes=output_nodes, learning_rate=learning_rate)
69+
# var train_nn = Network(input_nodes=input_nodes, hidden_nodes_l1=hidden_nodes_1, hidden_nodes_l2=hidden_nodes_2, output_nodes=output_nodes, learning_rate=learning_rate)
70+
71+
var mse: Matrix = Matrix(1, 1)
6772

6873
print("Start evaluating performance")
69-
var iter_time: Matrix = Matrix(1, 1, True)
74+
var iter_time: Matrix = Matrix(1, 1)
7075
var time_sum: Float32 = 0.0
7176
var iter: Int = 0
7277

73-
var new_input: Matrix = Matrix(1, input_nodes, True)
74-
var new_label: Matrix = Matrix(1, output_nodes, True)
78+
var new_input: Matrix = Matrix(1, input_nodes)
79+
var new_label: Matrix = Matrix(1, output_nodes)
7580

7681
var time_now = now()
7782
for i in range(test_inputs.rows):

matmul.mojo

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from numjo import matmul_vectorized, matmul, Matrix
22
from random import rand
33
from time import now
44

5-
fn main() -> None:
5+
fn matmul_benchmark() -> None:
66
let M: Int = 512
77
let K: Int = 512
88

matmul.py

-11
This file was deleted.

numjo/matrix/matrix.mojo

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from memory import Pointer
1+
from memory.unsafe import Pointer
22
from memory import memset_zero
33
from random import randn, rand, seed
44
from algorithm import vectorize
@@ -19,10 +19,9 @@ struct Matrix:
1919
var cols: Int
2020

2121
# Initialize
22-
fn __init__(inout self, rows: Int, cols: Int, zero:Bool = False):
22+
fn __init__(inout self, rows: Int, cols: Int):
2323
self.data = DTypePointer[type].alloc(rows * cols)
24-
if zero:
25-
memset_zero(self.data, rows * cols)
24+
memset_zero(self.data, rows * cols)
2625
self.rows = rows
2726
self.cols = cols
2827

py_matmul.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
from time import time
3+
4+
def py_matmul_benchmark():
5+
M = 512
6+
K = 512
7+
A = np.random.randn(M , K)
8+
B = np.random.randn(K, M)
9+
start_time = time()
10+
C = np.dot(A, B)
11+
end_time = time()-start_time
12+
print(f"Matrix Multiplication in Python with 512x512 matrices took {end_time * 1e3} milliseconds.")

test_numjo.mojo

+5-9
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,20 @@ fn main() -> None:
6666
nj.matmul(mul_matrix_C, mul_matrix_A, mul_matrix_B)
6767
mul_matrix_C.print_all()
6868

69-
let A: Matrix = Matrix(1, 50)
70-
rand(A.data, A.rows * A.cols)
69+
let A: Matrix = Matrix(2, 50)
70+
randn(A.data, A.rows * A.cols)
7171
A.print_all()
7272

73-
let B: Matrix = Matrix(50, 1)
74-
rand(B.data, B.rows * B.cols)
73+
let B: Matrix = Matrix(50, 2)
74+
randn(B.data, B.rows * B.cols)
7575
A.print_all()
7676

7777
let C: Matrix = Matrix(A.rows, B.cols)
78-
nj.matmul(C, A, B)
78+
nj.matmul_vectorized(C, A, B)
7979
C.print_all()
8080

8181
A[0, 35] = 0.99
8282
print(nj.argmax(A))
8383

8484
print(nj.mean(A))
8585
print(nj.sum(A))
86-
87-
88-
89-

0 commit comments

Comments
 (0)