-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_clf_template.py.txt
138 lines (100 loc) · 4 KB
/
mnist_clf_template.py.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from ulab import numpy as np
##### weights sections starts #####
##### weights sections ends #####
##### validation sections starts #####
##### validation sections ends #####
def relu(x):
return np.maximum(0, x)
def convolve_2d_layer(image, filter, bias):
output_height = image.shape[0] - filter.shape[0] + 1
output_width = image.shape[1] - filter.shape[1] + 1
output = np.zeros((output_height, output_width))
for i in range(output_height):
for j in range(output_width):
conv_sum = np.sum(image[i : i + 3, j : j + 3] * filter) + bias
output[i, j] = relu(conv_sum)
return output
def max_pool_2d(input_array, pool_size):
output_height = input_array.shape[0] // pool_size
output_width = input_array.shape[1] // pool_size
output = np.zeros((output_height, output_width))
for i in range(output_height):
for j in range(output_width):
pool_region = input_array[
i * pool_size : (i + 1) * pool_size, j * pool_size : (j + 1) * pool_size
]
output[i, j] = np.max(pool_region)
return output
def flatten_and_concatenate(array1, array2):
flattened_array1 = array1.flatten()
flattened_array2 = array2.flatten()
flattened_combined_array = np.concatenate((flattened_array1, flattened_array2))
return flattened_combined_array
def interleave_arrays(array1, array2):
flattened_array1 = array1.flatten()
flattened_array2 = array2.flatten()
interleaved_array = np.zeros(
len(flattened_array1) + len(flattened_array2), dtype=flattened_array1.dtype
)
interleaved_array[0::2] = flattened_array1
interleaved_array[1::2] = flattened_array2
return interleaved_array
def dense_layer(input_vector, weights, biases):
output = np.dot(input_vector, weights) + biases
return output
def softmax(x):
x -= np.max(x)
exp_x = np.exp(x)
return exp_x / np.sum(exp_x)
def predict(image):
layer_1_output = []
for i in range(0, len(weights_0)):
x = convolve_2d_layer(image, weights_0[i], biases_0[i])
layer_1_output.append(x)
layer_2_output = []
for i in range(0, len(layer_1_output)):
x = max_pool_2d(layer_1_output[i], 2)
layer_2_output.append(x)
layer_3_output = []
for filter_len in range(0, 2): # 2 times
output = np.zeros((12, 12))
for i in range(12):
for j in range(12):
op_channel = np.zeros((3, 3))
for channel in range(0, 8):
x = (
layer_2_output[channel][i : i + 3, j : j + 3]
* weights_1[filter_len][channel]
)
op_channel = op_channel + x
conv_sum = np.sum(
op_channel
)
output[i, j] = relu(conv_sum + biases_1[filter_len])
layer_3_output.append(output)
layer_4_input = interleave_arrays(
layer_3_output[0], layer_3_output[1]
)
layer_4_output = dense_layer(layer_4_input, weights_2, biases_2)
layer_5_output = softmax(layer_4_output)
arg_max = np.argmax(layer_5_output)
return arg_max, layer_5_output[arg_max],layer_5_output
def validate():
patterns = [
np.array([[1, 0] * 15] * 30),
np.ones((30, 30)),
np.array([[0, 1] * 15] * 30),
np.array([[1, 1, 0, 0] * 7 + [1, 1]] * 30),
]
results = [result_0, result_1, result_2, result_3]
for i in range(len(patterns)):
expected_result = results[i]
pattern = patterns[i]
output = predict(pattern)[2]
output_equal = True
for j in range(len(output)):
if abs(output[j] - expected_result[j]) > 0.00001:
output_equal = False
break
assert output_equal, f"Validation failed for result_{i}, {output},{expected_result}"
return True