-
Notifications
You must be signed in to change notification settings - Fork 1
/
generic_ops.py
65 lines (43 loc) · 1.46 KB
/
generic_ops.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
"""Generic Operation types."""
import numpy as np
from .mixins import _ScalarShape, _ShapeAsIs
from .operation import Operation
from .tensor_shape import TensorShape
class Const(Operation):
def __init__(self, value, graph=None, name=None):
self._value = value
super(Const, self).__init__(graph=graph, name=name)
def _run(self):
"""Returns numpy array."""
return self._value
def _compute_shapes(self):
return [TensorShape(list(self._value.shape))]
class ZerosLike(Operation, _ShapeAsIs):
def _run(self, tensor_value):
outputs = np.zeros_like(tensor_value, dtype=tensor_value.dtype)
return outputs
class OnesLike(Operation, _ShapeAsIs):
def _run(self, tensor_value):
outputs = np.ones_like(tensor_value, dtype=tensor_value.dtype)
return outputs
class Shape(Operation):
def _run(self, *tensor_values):
outputs = [
np.asarray(tensor_value.shape, dtype="int32")
for tensor_value in tensor_values
]
return outputs
def _compute_shapes(self):
return [
TensorShape([None])
if tensor.shape.level == 0 else TensorShape([tensor.shape.ndims])
for tensor in self._input_list
]
class Size(Operation, _ScalarShape):
def _run(self, tensor_value):
outputs = np.asarray(np.size(tensor_value), dtype="int32")
return outputs
class Rank(Operation, _ScalarShape):
def _run(self, tensor_value):
outputs = np.asarray(len(tensor_value.shape), dtype="int32")
return outputs