NumPy implementation of the backend - `__name__` is "numpy" - `__type__` is np.ndarray
| 1113 | |
| 1114 | |
| 1115 | class NumpyBackend(Backend): |
| 1116 | """ |
| 1117 | NumPy implementation of the backend |
| 1118 | |
| 1119 | - `__name__` is "numpy" |
| 1120 | - `__type__` is np.ndarray |
| 1121 | """ |
| 1122 | |
| 1123 | __name__ = "numpy" |
| 1124 | __type__ = np.ndarray |
| 1125 | __type_list__ = [np.array(1, dtype=np.float32), np.array(1, dtype=np.float64)] |
| 1126 | |
| 1127 | rng_ = np.random.RandomState() |
| 1128 | |
| 1129 | def _to_numpy(self, a): |
| 1130 | return a |
| 1131 | |
| 1132 | def _from_numpy(self, a, type_as=None): |
| 1133 | if type_as is None: |
| 1134 | return a |
| 1135 | elif isinstance(a, float): |
| 1136 | return a |
| 1137 | else: |
| 1138 | return a.astype(type_as.dtype) |
| 1139 | |
| 1140 | def set_gradients(self, val, inputs, grads): |
| 1141 | # No gradients for numpy |
| 1142 | return val |
| 1143 | |
| 1144 | def _detach(self, a): |
| 1145 | # No gradients for numpy |
| 1146 | return a |
| 1147 | |
| 1148 | def zeros(self, shape, type_as=None): |
| 1149 | if type_as is None: |
| 1150 | return np.zeros(shape) |
| 1151 | else: |
| 1152 | return np.zeros(shape, dtype=type_as.dtype) |
| 1153 | |
| 1154 | def ones(self, shape, type_as=None): |
| 1155 | if type_as is None: |
| 1156 | return np.ones(shape) |
| 1157 | else: |
| 1158 | return np.ones(shape, dtype=type_as.dtype) |
| 1159 | |
| 1160 | def arange(self, stop, start=0, step=1, type_as=None): |
| 1161 | return np.arange(start, stop, step) |
| 1162 | |
| 1163 | def full(self, shape, fill_value, type_as=None): |
| 1164 | if type_as is None: |
| 1165 | return np.full(shape, fill_value) |
| 1166 | else: |
| 1167 | return np.full(shape, fill_value, dtype=type_as.dtype) |
| 1168 | |
| 1169 | def eye(self, N, M=None, type_as=None): |
| 1170 | if type_as is None: |
| 1171 | return np.eye(N, M) |
| 1172 | else: |
no outgoing calls