-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_numpy.py
49 lines (36 loc) · 981 Bytes
/
_numpy.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
"""
numpy backend
"""
try:
import numpy as np
except ImportError:
np = None
from ._backend import Backend, assert_backend_available
class NumpyBackend(Backend):
def __str__(self):
return "numpy"
def is_available(self):
return np is not None
@assert_backend_available
def is_compatible(self, args):
if list(filter(lambda t: isinstance(args, t), [
np.ndarray,
np.matrix
])) != []:
return True
# , "numpy backend requires input to be an instance of `np.ndarray` or `np.matrix`"
return False
def concatenate(self, tensor, axis):
return np.concatenate(tensor, axis = axis)
def ones_like(self, tensor):
return np.ones_like(tensor)
def multiply(self, x, y):
return x*y
def expand_dims(self, tensor, axis):
return np.expand_dims(tensor, axis)
def get_dims(self, tensor):
return tensor.shape
def reshape(self, tensor, shape):
return np.reshape(tensor, shape)
def matmul(self, tensor1, tensor2):
return np.dot(tensor1, tensor2)