-
Notifications
You must be signed in to change notification settings - Fork 69
/
tensor.py
538 lines (423 loc) · 15.8 KB
/
tensor.py
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
import numpy as np
import MLlib.functional as F
import MLlib.autograd as autograd
class Tensor:
"""
Tensor object which acts as a wrapper around a NumPy array.
"""
__slots__ = ('data', 'requires_grad', 'is_leaf', 'grad_fn', '_grad',
'is_parameter')
def __init__(self, data, requires_grad=False, is_leaf=True,
is_parameter=False, dtype=None):
"""
PARAMETERS
==========
data: list, tuple or np.array
The actual data of the tensor
requires_grad: boolean
If true, accumulate gradient in `.grad`
is_leaf: boolean
If true, this is a leaf tensor.
is_parameter: boolean
If true, data contains trainable params.
RETURNS
=======
A Tensor (MLlib.Tensor) object
"""
if not (isinstance(data, np.ndarray)):
data = np.array(data, dtype)
self.data = data
self.requires_grad = requires_grad
self.is_leaf = is_leaf
self.grad_fn = None # Set during forward pass
self._grad = None
self.is_parameter = is_parameter
def __getitem__(self, *args):
"""
Allows tensor to be accessed via indices. Indexing works just like
numpy arrays. This operation is not connected to the computation graph.
RETURNS
-------
a numpy array with desired elements.
Example:
>>> a = MLlib.Tensor([2., 4., 6.], requires_grad=True)
>>> a[2]
6.
"""
return self.data.__getitem__(*args)
def get_grad(self):
return self._grad
def del_grad(self):
del self._grad
def set_grad(self, val):
if val is None or type(val).__name__ == 'Tensor':
self._grad = val
else:
raise Exception("Expected the gradient to be NoneType object or a Tensor\
(got {})".format(type(val).__name__))
grad = property(get_grad, set_grad, del_grad, 'The gradient of the tensor')
# why do we need the grad as property?
# because the user may set the grad property to 0 and we want our gradients
# to be nothing else but Tensors. So, having grad as property helps us to
# define a custom `setter` function for the _grad attribute that ensures
# that only Tensors are stored in _grad
# ----------------------
# for printing tensors
# ----------------------
def __str__(self):
"""
This function is called whenever we call print() on an instance of
Tensor.
"""
sgf = self.grad_fn
return "{}{}".format(
str(self.data),
", grad_fn={}".format(
self.grad_fn.__class__.__name__) if sgf is not None else ""
)
def __repr__(self):
return self.__str__()
# ----------------------
# Tensor operations
# ----------------------
@property
def shape(self):
"""
Returns the shape of data array in a tuple
"""
return self.data.shape
def copy(self, **kwargs):
"""
Returns a copy of data associated with the tensor as a new tensor.
Parameters of tensors like is_leaf and grad_fn can be associated
with this copy by using appropriate **kwargs.
"""
return Tensor(self.data, **kwargs)
def numpy(self):
"""
Returns the data stored in Tensor as np.array
"""
return self.data
# ----------------------------------------------------------------
# Tensor creation methods, can be used WITHOUT creating a tensor
# ----------------------------------------------------------------
@staticmethod
def ones(shape, **kwargs):
"""
Similar to np.ones(...)
PARAMETERS
==========
shape: int or tuple of ints
Used for defining shape of the Tensor
**kwargs
RETURNS
=======
a Tensor filled with ones of given `shape`
"""
return Tensor(np.ones(shape), **kwargs)
@staticmethod
def zeros(*shape, **kwargs):
"""
Similar to np.zeros(...)
PARAMETERS
==========
shape: int or tuple of ints
Used for defining shape of the Tensor
**kwargs
RETURNS
=======
a Tensor filled with zeros of given `shape`
"""
return Tensor(np.zeros(shape), **kwargs)
@staticmethod
def randn(*shape, **kwargs):
"""
Similar to np.random.randn(...)
Generates a Tensor filled with ones.
PARAMETERS
==========
shape: int or tuple of ints
Used for defining shape of the Tensor
**kwargs
RETURNS
=======
a Tensor of `shape` sampled from Gaussian Distribution with
mu=0 and sigma=1
"""
return Tensor(np.random.randn(*shape), **kwargs)
@staticmethod
def arange(*interval, **kwargs):
"""
Similar to np.arange(...)
PARAMETERS
==========
interval: int or tuple of ints
Used to define interval for values inside Tensor
**kwargs
RETURNS
=======
a Tensor with values from `interval`
"""
return Tensor(np.arange(*interval), **kwargs)
@staticmethod
def empty(*shape, **kwargs):
"""
Similar to np.empty(...)
Generates a Tensor with uninitialized data.
PARAMETERS
==========
shape: int or tuple of ints
Used for defining shape of the Tensor
**kwargs
RETURNS
=======
a Tensor containing uninitialized data with given shape and properties
"""
return Tensor(np.empty(shape), **kwargs)
# ---------------------------------
# Autograd backward initialization
# ----------------------------------
def backward(self, grad_of_output=None):
"""
This method is called to initiate the backward pass on the computation
graph.
This method initiates MLlib.autograd.backward() method which
accumulates the gradient(s) to the Leaf Tensors (`is_leaf=True`) with
`requires_grad=True` with respect to the root of the computation graph.
PARAMETERS
==========
grad_of_output: None or MLlib.Tensor
The gradient of root node with respect to the current
tensor('s node)
\t\t When nothing is passed, the gradients are calculated with respect\
\t to the tensor through which this method is being called. To put it
\t simply, if nothing is passed this tensor is assumed to be the root
\t of the computation graph.
\t\t If the gradient is passed, it must be of the same shape as that\
\t of the tensor through which this method is being called. If the
\t gradient is passed, this tensor is assumed to be an intermediate
\t node in the computation graph and the gradient is assumed to be\
with respect to some root node.
RETURNS
=======
`None`
"""
if grad_of_output is None:
grad_of_output = Tensor.ones(self.shape)
if grad_of_output.shape != self.shape:
# this block will be executed only when graient is supplied
raise Exception('The shape of gradient and variable must match')
if self.grad_fn is None:
raise Exception('backward should not be called on tensors '
+ 'without grad_fn')
return autograd.backward(self.grad_fn, grad_of_output)
# --------------------------------------------------------------
# Tensor operations that get reflected on the computation graph
# --------------------------------------------------------------
@property
def T(self):
"""
Transposes a 2-D Tensor.
Usage:
>>> a = MLlib.Tensor([[2., 4.], [8., 6,]])
>>> a.T
[[2. 8.]
[4. 6.]], grad_fn=BackwardFunction
"""
return F.Transpose.apply(self)
def reshape(self, *shape):
"""
Reshapes a tensor to desired shape.
Usage:
>>> a = MLlib.Tensor.randn(5, 6, 8)
>>> b = a.reshape(30, 8)
"""
return F.Reshape.apply(self, shape)
def __add__(self, other):
"""
This function is called internally by python whenever addition
operation (`+`) is performed and the left operand is an instance of
MLlib.Tensor.
If the right operand (`other` arguement passed to this function) is
int or float we should convert that to a Tensor because our
computation graph is built using Tensors.
"""
if type(other) == int:
other = float(other)
if type(other) == float:
other = Tensor(other)
return F.Add.apply(self, other)
def __radd__(self, other):
"""
This function is called internally by python whenever addition
operation (`+`) is performed and the right operand is an instance of
MLlib.Tensor.
Since `other + Tensor` should be equivalent to `Tensor + other`
if the operation is valid, so we call __add__ method for the Tensor.
"""
return self.__add__(other)
def __sub__(self, other):
"""
This function is called internally by python whenever subtraction
operation (`-`) is performed and the left operand is an instance of
MLlib.Tensor.
If the right operand (`other` arguement passed to this function) is
int or float we should convert that to a Tensor because our
computation graph is built using Tensors.
"""
if type(other) == int:
other = float(other)
if type(other) == float:
other = Tensor(other)
return F.Sub.apply(self, other)
def __rsub__(self, other):
"""
This function is called internally by python whenever subtraction
operation (`-`) is performed and the right operand is an instance of
MLlib.Tensor.
If the left operand (`other` arguement passed to this function) is
int or float we should convert that to a Tensor because our
computation graph is built using Tensors.
"""
if type(other) == int:
other = float(other)
if type(other) == float:
other = Tensor(other)
return F.Sub.apply(other, self)
def __neg__(self):
"""
This function is called internally by python whenever we perform
`-a` operation where the variable a is an instance of MLlib.Tensor
class
"""
return (-1)*self
def __matmul__(self, other):
"""
This function is called internally by python whenever
'matrix multiplication' (`@`) is performed and the left operand is an
instance of MLlib.Tensor class.
NOTE: We should only perform this operation on Tensors
(MLlib.Tensor's instances)
The __matmul__ operation is denoted by '@'
>>> x @ y
"""
return F.MatMul.apply(self, other)
def __truediv__(self, other):
"""
This function is called internally by python whenever division
operation (`/`) is performed and the left operand is an instance of
MLlib.Tensor.
If the right operand (`other` arguement passed to this function) is
int or float we should convert that to a Tensor because our
computation graph is built using Tensors.
"""
if type(other) == int:
other = float(other)
if type(other) == float:
other = Tensor(other)
return F.Div.apply(self, other)
def __rtruediv__(self, other):
"""
This function is called internally by python whenever division
operation (`/`) is performed and the right operand is an instance of
MLlib.Tensor.
If the left operand (`other` arguement passed to this function) is
int or float we should convert that to a Tensor because our
computation graph is built using Tensors.
"""
if type(other) == int:
other = float(other)
if type(other) == float:
other = Tensor(other)
return F.Div.apply(other, self)
def __mul__(self, other):
"""
This function is called internally by python whenever multiplication
operation (`*`) is performed and the left operand is an instance of
MLlib.Tensor.
If the right operand (`other` arguement passed to this function) is
int or float we should convert that to a Tensor because our
computation graph is built using Tensors.
"""
if type(other) == int:
other = float(other)
if type(other) == float:
other = Tensor(other)
return F.Mul.apply(self, other)
def __rmul__(self, other):
"""
This function is called internally by python whenever multiplication
operation (`*`) is performed and the right operand is an instance of
MLlib.Tensor.
Since `other * Tensor` should be equivalent to `Tensor * other`
if the operation is valid, so we call __mul__ method for the Tensor.
"""
return self.__mul__(other)
def __pow__(self, other):
"""
This function is called internally by python whenever power
operation (`**`) is performed and the left operand is an instance of
MLlib.Tensor.
Internally, np.power(...) method is used.
If the right operand (`other` arguement passed to this function) is
int or float we should convert that to a Tensor because our
computation graph is built using Tensors.
>>> a**2 or a**b
"""
if type(other) == int:
other = float(other)
if type(other) == float:
other = Tensor(other)
return F.Pow.apply(self, other)
def __rpow__(self, other):
"""
This function is called internally by python whenever power
operation (`**`) is performed and the right operand is an instance of
MLlib.Tensor.
Internally, np.power(...) method is used.
If the left operand (`other` arguement passed to this function) is
int or float we should convert that to a Tensor because our
computation graph is built using Tensors.
>>> 2**b or a**b
"""
if type(other) == int:
other = float(other)
if type(other) == float:
other = Tensor(other)
return F.Pow.apply(other, self)
def dot(self, other):
"""
Vector dot product.
PARAMETERS
==========
other: MLlib.Tensor
The Tensor with which the dot product is to be computed.
RETURNS
=======
MLlib.Tensor which is a dot product of given input.
NOTE: Should be used only for vectors (Tensors of shape `(n,)`).
For matrices and n-dimensional tensors, usage of `@` (matmul operation)
is recommended.
"""
return F.Dot.apply(self, other)
def sum(self, axis=None, keepdims=False):
"""
Computes the sum of elements of the Tensor.
PARAMETERS
==========
axis: int
index of axis of Tensor along which sum of elements is to be
computed.
keepdims: boolean
if True, the shape of Tensor is retained.
RETURNS
=======
A Tensor with the sum of elements along the given axis having shape
governed by the `keepdims` arguement.
"""
return F.Sum.apply(self, axis, keepdims)
def log(self):
"""
Returns the element-wise log of the Tensor.
"""
return F.Log.apply(self)