-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
246 lines (213 loc) · 7.42 KB
/
main.py
File metadata and controls
246 lines (213 loc) · 7.42 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""
============================================================
Tensor Operations in Python Examples
============================================================
The following demonstrates naive tensor operations in Python
"""
import numpy as np
class TensorOpsInPython:
def __init__(self):
print(__doc__)
self.array_1d_y1 = [0, 1, 2, 3, 4, 5]
self.array_1d_x1 = [9, 7, 5, 3, 1, 0]
self.array_2d_x = [[0, 1, 2], [3, 4, 5]]
self.array_2d_y = [[2, 4, 6], [8, 10, 12]]
self.array_2d_x1 = [[0, 1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6],
[2, 3, 4, 5, 6, 7]]
self.array_2d_y1 = [[3, 2, 1],
[4, 2, 0],
[0, 3, 1],
[0, 1, 0],
[2, 4, 1],
[4, 4, 1]]
self.tensor_1d_x1 = np.array(self.array_1d_x1)
self.tensor_1d_y1 = np.array(self.array_1d_y1)
self.tensor_2d_x = np.array(self.array_2d_x)
self.tensor_2d_y = np.array(self.array_2d_y)
self.tensor_2d_x1 = np.array(self.array_2d_x1)
self.tensor_2d_y1 = np.array(self.array_2d_y1)
# Naive Relu
print('\n--- Naive Relu')
print('Input')
print(self.tensor_2d_x)
x = self.naive_relu(self.tensor_2d_x)
print('\nOutput')
print(x)
print('\nNumpy')
x = np.maximum(self.tensor_2d_x,0)
print(x)
# Naive Add
print('\n--- Naive Add')
print('Input')
print(self.tensor_2d_x)
print(self.tensor_2d_y)
x = self.naive_add(self.tensor_2d_x, self.tensor_2d_y)
print('\nOutput')
print(x)
print('\nNumpy')
x = self.tensor_2d_x + self.tensor_2d_y
print(x)
# Naive Subtract
print('\n--- Naive Subtract')
print('Input')
print(self.tensor_2d_x)
print(self.tensor_2d_y)
x = self.naive_subtract(self.tensor_2d_x, self.tensor_2d_y)
print('\nOutput')
print(x)
print('\nNumpy')
x = self.tensor_2d_x - self.tensor_2d_y
print(x)
# Naive Multiply
print('\n--- Naive Multiply')
print('Input')
print(self.tensor_2d_x)
print(self.tensor_2d_y)
x = self.naive_multiply(self.tensor_2d_x, self.tensor_2d_y)
print('\nOutput')
print(x)
print('\nNumpy')
x = self.tensor_2d_x * self.tensor_2d_y
print(x)
# Naive Add Matrix and Vector
print('\n--- Naive Add Matrix and Vector')
print('Input')
print(self.tensor_2d_x1)
print(self.tensor_1d_y1)
x = self.naive_add_matrix_and_vector(self.tensor_2d_x1, self.tensor_1d_y1)
print('\nOutput')
print(x)
# Naive Vector Dot (product)
print('\n--- Naive Vector Dot (product)')
print('Input')
print(self.tensor_1d_x1)
print(self.tensor_1d_y1)
x = self.naive_vector_dot(self.tensor_1d_x1, self.tensor_1d_y1)
print('\nOutput')
print(x)
# Naive Matrix Vector Dot (product)
print('\n--- Naive Vector Dot (product)')
print('Input')
print(self.tensor_2d_x1)
print(self.tensor_1d_y1)
x = self.naive_matrix_vector_dot(self.tensor_2d_x1, self.tensor_1d_y1)
print('\nOutput')
print(x)
# Naive Matrix Vector Dot v2 reusing vector dot
print('\n--- Naive Vector Dot V2 reusing vector dot(product)')
print('Input')
print(self.tensor_2d_x1)
print(self.tensor_1d_y1)
x = self.naive_matrix_vector_dot_v2(self.tensor_2d_x1, self.tensor_1d_y1)
print('\nOutput')
print(x)
# Naive Matrix Dot
print('\n--- Naive Matrix Dot')
print('Input')
print(self.tensor_2d_x1)
print(self.tensor_2d_y1)
x = self.naive_matrix_dot(self.tensor_2d_x1, self.tensor_2d_y1)
print('\nOutput')
print(x)
# Reshape
print('\n--- Reshape')
print('Input')
print(self.tensor_2d_x)
x = self.tensor_2d_x.reshape((3, 2))
print('\nOutput after reshape to 3, 2')
print(x)
# Transpose
print('\n--- Transpose (exchanging rows and columns)')
print('Input')
print(self.tensor_2d_x)
x = np.transpose(self.tensor_2d_x)
print('\nOutput after transpose')
print(x)
@staticmethod
def naive_relu(x):
assert len(x.shape) == 2
x = x.copy()
for i in range(x.shape[0]):
for j in range(x.shape[1]):
x[i, j] = max(x[i, j], 0)
return x
@staticmethod
def naive_add(x, y):
assert len(x.shape) == 2
assert x.shape == y.shape
x = x.copy()
for i in range(x.shape[0]):
for j in range(x.shape[1]):
x[i, j] += y[i, j]
return x
@staticmethod
def naive_subtract(x, y):
assert len(x.shape) == 2
assert x.shape == y.shape
x = x.copy()
for i in range(x.shape[0]):
for j in range(x.shape[1]):
x[i, j] -= y[i, j]
return x
@staticmethod
def naive_multiply(x, y):
assert len(x.shape) == 2
assert x.shape == y.shape
x = x.copy()
for i in range(x.shape[0]):
for j in range(x.shape[1]):
x[i, j] *= y[i, j]
return x
# Example broadcasting axes to smaller tensor to match ndim of larger tensor,
# so smaller tensor is repeated along new axes to match full shape of larger tensor
@staticmethod
def naive_add_matrix_and_vector(x, y):
assert len(x.shape) == 2
assert len(y.shape) == 1
assert x.shape[1] == y.shape[0]
x = x.copy()
for i in range(x.shape[0]):
for j in range(x.shape[1]):
x[i, j] += y[j]
return x
# Only works with vectors having same number elements
@staticmethod
def naive_vector_dot(x, y):
assert len(x.shape) == 1
assert len(y.shape) == 1
assert x.shape[0] == y.shape[0]
z = 0
for i in range(x.shape[0]):
z += x[i] * y[i]
return z
# Only works with vectors having same number elements
@staticmethod
def naive_matrix_vector_dot(x, y):
assert len(x.shape) == 2 # Numpy matrix
assert len(y.shape) == 1 # Numpy vector
assert x.shape[1] == y.shape[0]
z = np.zeros(x.shape[0])
for i in range(x.shape[0]):
for j in range(x.shape[1]):
z[i] += x[i, j] * y[j]
return z
def naive_matrix_vector_dot_v2(self, x, y):
z = np.zeros(x.shape[0])
for i in range(x.shape[0]):
z[i] = self.naive_vector_dot(x[i, :], y)
return z
# Dot product between 2 matrices. Result is matrix with shale x.shape[0], y.shape[1],
# where coefficients are the vector products between rows of x and columns of y
def naive_matrix_dot(self, x, y):
assert len(x.shape) == 2 # Numpy matrix
assert len(y.shape) == 2 # Numpy matrix
assert x.shape[1] == y.shape[0]
z = np.zeros((x.shape[0], y.shape[1]))
for i in range(x.shape[0]):
for j in range(y.shape[1]):
row_x = x[i, :]
column_y = y[:, j]
z[i, j] = self.naive_vector_dot(row_x, column_y)
return z
tp = TensorOpsInPython()