DepthTensor 2.7.0__tar.gz → 2.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {depthtensor-2.7.0/src/depthtensor.egg-info → depthtensor-2.8.0}/PKG-INFO +1 -1
- {depthtensor-2.7.0 → depthtensor-2.8.0}/pyproject.toml +1 -1
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/__init__.py +1 -1
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/comparison.py +14 -4
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/elementwise.py +23 -8
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/reduction.py +70 -13
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/tensor.py +396 -24
- {depthtensor-2.7.0 → depthtensor-2.8.0/src/depthtensor.egg-info}/PKG-INFO +1 -1
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/depthtensor.egg-info/SOURCES.txt +0 -1
- {depthtensor-2.7.0 → depthtensor-2.8.0}/tests/test_autodiff.py +117 -0
- depthtensor-2.7.0/tests/test_autodiff_extended.py +0 -126
- {depthtensor-2.7.0 → depthtensor-2.8.0}/LICENSE +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/README.md +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/setup.cfg +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/__init__.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/exceptions.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/__init__.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/creation.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/ops/function.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/random/__init__.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/random/generator.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/_core/utils.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/autodiff.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/DepthTensor/typing.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/depthtensor.egg-info/dependency_links.txt +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/depthtensor.egg-info/requires.txt +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/src/depthtensor.egg-info/top_level.txt +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/tests/test_ops_forward.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/tests/test_tensor_errors.py +0 -0
- {depthtensor-2.7.0 → depthtensor-2.8.0}/tests/test_tensor_init.py +0 -0
|
@@ -59,7 +59,7 @@ def where(
|
|
|
59
59
|
/,
|
|
60
60
|
*,
|
|
61
61
|
device: Device | None = None,
|
|
62
|
-
requires_grad: bool =
|
|
62
|
+
requires_grad: bool | None = None,
|
|
63
63
|
) -> Union[tuple[TensorType, ...], TensorType]:
|
|
64
64
|
"""
|
|
65
65
|
Return elements chosen from `x` or `y` depending on `condition`.
|
|
@@ -73,9 +73,9 @@ def where(
|
|
|
73
73
|
y : TensorLike | None, optional
|
|
74
74
|
Values from which to choose.
|
|
75
75
|
device : Device | None, optional
|
|
76
|
-
The device to place the result on.
|
|
77
|
-
requires_grad : bool, optional
|
|
78
|
-
Whether the result requires gradient computation.
|
|
76
|
+
The device to place the result on. If None, inferred from inputs.
|
|
77
|
+
requires_grad : bool | None, optional
|
|
78
|
+
Whether the result requires gradient computation. If None, inferred from inputs (x, y).
|
|
79
79
|
|
|
80
80
|
Returns
|
|
81
81
|
-------
|
|
@@ -91,6 +91,9 @@ def where(
|
|
|
91
91
|
|
|
92
92
|
# * One parameter overload
|
|
93
93
|
if (x is None) and (y is None):
|
|
94
|
+
if requires_grad is None:
|
|
95
|
+
requires_grad = False
|
|
96
|
+
|
|
94
97
|
data = to_tensordata(condition, device=device)
|
|
95
98
|
if device == "cpu":
|
|
96
99
|
result = np.where(data) # type: ignore
|
|
@@ -101,6 +104,13 @@ def where(
|
|
|
101
104
|
return tuple([Tensor._fast_init(array, device=device, requires_grad=requires_grad) for array in result])
|
|
102
105
|
# * Two parameters overload
|
|
103
106
|
elif x is not None and y is not None:
|
|
107
|
+
if requires_grad is None:
|
|
108
|
+
requires_grad = False
|
|
109
|
+
if isinstance(x, Tensor):
|
|
110
|
+
requires_grad = requires_grad or x.requires_grad
|
|
111
|
+
if isinstance(y, Tensor):
|
|
112
|
+
requires_grad = requires_grad or y.requires_grad
|
|
113
|
+
|
|
104
114
|
if (
|
|
105
115
|
not (get_device(x) == get_device(y) == device)
|
|
106
116
|
and not isinstance(x, (int, float, list, tuple))
|
|
@@ -100,6 +100,10 @@ def wrapper_2in_1out(
|
|
|
100
100
|
y = getattr(cp, func_name)(a1, a2, out=out, dtype=dtype, casting=casting)
|
|
101
101
|
|
|
102
102
|
if in_place and x1_is_tensor:
|
|
103
|
+
if x1.requires_grad:
|
|
104
|
+
raise RuntimeError(
|
|
105
|
+
"In-place operations are forbidden on differentiable tensors."
|
|
106
|
+
)
|
|
103
107
|
x1.data = y
|
|
104
108
|
return x1
|
|
105
109
|
|
|
@@ -147,6 +151,10 @@ def wrapper_1in_1out(
|
|
|
147
151
|
requires_grad = False
|
|
148
152
|
if isinstance(x, Tensor):
|
|
149
153
|
if in_place:
|
|
154
|
+
if x.requires_grad:
|
|
155
|
+
raise RuntimeError(
|
|
156
|
+
"In-place operations are forbidden on differentiable tensors."
|
|
157
|
+
)
|
|
150
158
|
x.data = y
|
|
151
159
|
return x
|
|
152
160
|
requires_grad = x.requires_grad
|
|
@@ -1198,7 +1206,7 @@ def clip(
|
|
|
1198
1206
|
/,
|
|
1199
1207
|
out: TensorData | None = None,
|
|
1200
1208
|
*,
|
|
1201
|
-
requires_grad: bool =
|
|
1209
|
+
requires_grad: bool | None = None,
|
|
1202
1210
|
device: Device | None = None,
|
|
1203
1211
|
where: TensorDataBool | bool = True,
|
|
1204
1212
|
casting: Casting = "same_kind",
|
|
@@ -1225,10 +1233,10 @@ def clip(
|
|
|
1225
1233
|
Not more than one of `a_min` and `a_max` may be None.
|
|
1226
1234
|
out : TensorData | None, optional
|
|
1227
1235
|
The results will be placed in this array.
|
|
1228
|
-
requires_grad : bool, optional
|
|
1229
|
-
Whether the result requires gradient computation.
|
|
1236
|
+
requires_grad : bool | None, optional
|
|
1237
|
+
Whether the result requires gradient computation. If None, inferred from inputs.
|
|
1230
1238
|
device : Device | None, optional
|
|
1231
|
-
The device to place the result on.
|
|
1239
|
+
The device to place the result on. If None, inferred from inputs.
|
|
1232
1240
|
where : TensorDataBool | bool, optional
|
|
1233
1241
|
This condition is broadcast over the input.
|
|
1234
1242
|
casting : Casting, optional
|
|
@@ -1248,7 +1256,6 @@ def clip(
|
|
|
1248
1256
|
"""
|
|
1249
1257
|
from ...tensor import Tensor
|
|
1250
1258
|
|
|
1251
|
-
is_tensor_op = False
|
|
1252
1259
|
if (
|
|
1253
1260
|
isinstance(a, Tensor)
|
|
1254
1261
|
and isinstance(a_min, Tensor)
|
|
@@ -1256,13 +1263,21 @@ def clip(
|
|
|
1256
1263
|
):
|
|
1257
1264
|
if not (a.device == a_min.device == a_max.device):
|
|
1258
1265
|
raise DeviceMismatch(DEVICE_MISMATCH_MSG)
|
|
1259
|
-
is_tensor_op = True
|
|
1260
1266
|
|
|
1261
|
-
if
|
|
1262
|
-
device_op = a
|
|
1267
|
+
if device is None:
|
|
1268
|
+
device_op = get_device(a)
|
|
1263
1269
|
else:
|
|
1264
1270
|
device_op = device
|
|
1265
1271
|
|
|
1272
|
+
if requires_grad is None:
|
|
1273
|
+
requires_grad = False
|
|
1274
|
+
if isinstance(a, Tensor):
|
|
1275
|
+
requires_grad = requires_grad or a.requires_grad
|
|
1276
|
+
if isinstance(a_min, Tensor):
|
|
1277
|
+
requires_grad = requires_grad or a_min.requires_grad
|
|
1278
|
+
if isinstance(a_max, Tensor):
|
|
1279
|
+
requires_grad = requires_grad or a_max.requires_grad
|
|
1280
|
+
|
|
1266
1281
|
arr_a, arr_min, arr_max = (
|
|
1267
1282
|
to_tensordata(a, device=device_op),
|
|
1268
1283
|
to_tensordata(a_min, device=device_op),
|
|
@@ -10,6 +10,7 @@ from ...typing import (
|
|
|
10
10
|
Axis,
|
|
11
11
|
TensorLike,
|
|
12
12
|
Device,
|
|
13
|
+
Shape,
|
|
13
14
|
)
|
|
14
15
|
|
|
15
16
|
from ..exceptions import (
|
|
@@ -33,12 +34,49 @@ except (ImportError, ModuleNotFoundError):
|
|
|
33
34
|
###
|
|
34
35
|
|
|
35
36
|
|
|
37
|
+
def _link_sum_backward(
|
|
38
|
+
y: TensorType, x: TensorLike, axis: Axis | None, keepdims: bool
|
|
39
|
+
) -> None:
|
|
40
|
+
from ...tensor import Tensor
|
|
41
|
+
|
|
42
|
+
if not isinstance(x, Tensor):
|
|
43
|
+
return
|
|
44
|
+
|
|
45
|
+
def callback(y_grad: TensorData, x_shape: Shape, device: Device) -> TensorData:
|
|
46
|
+
if device == "cpu":
|
|
47
|
+
xp = np
|
|
48
|
+
else:
|
|
49
|
+
if cp is None:
|
|
50
|
+
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
51
|
+
xp = cp
|
|
52
|
+
|
|
53
|
+
grad = y_grad
|
|
54
|
+
if not keepdims and axis is not None:
|
|
55
|
+
grad = xp.expand_dims(grad, axis)
|
|
56
|
+
|
|
57
|
+
return xp.broadcast_to(grad, x_shape)
|
|
58
|
+
|
|
59
|
+
def backward() -> None:
|
|
60
|
+
if y.grad is None:
|
|
61
|
+
y.zero_grad()
|
|
62
|
+
if x.grad is None:
|
|
63
|
+
x.zero_grad()
|
|
64
|
+
|
|
65
|
+
# y.grad is TensorData | None, but zero_grad ensures it's not None
|
|
66
|
+
if y.grad is None: return
|
|
67
|
+
|
|
68
|
+
x.grad += callback(y.grad, x.shape, y.device)
|
|
69
|
+
|
|
70
|
+
y.prev = (x,)
|
|
71
|
+
y.backward = backward
|
|
72
|
+
|
|
73
|
+
|
|
36
74
|
def sum(
|
|
37
75
|
a: TensorLike,
|
|
38
76
|
/,
|
|
39
77
|
*,
|
|
40
78
|
device: Device | None = None,
|
|
41
|
-
requires_grad: bool =
|
|
79
|
+
requires_grad: bool | None = None,
|
|
42
80
|
axis: Axis | None = None,
|
|
43
81
|
dtype: DTypeLike | None = None,
|
|
44
82
|
out: TensorData | None = None,
|
|
@@ -54,9 +92,9 @@ def sum(
|
|
|
54
92
|
a : TensorLike
|
|
55
93
|
Elements to sum.
|
|
56
94
|
device : Device | None, optional
|
|
57
|
-
The device to place the result on.
|
|
58
|
-
requires_grad : bool, optional
|
|
59
|
-
Whether the result requires gradient computation.
|
|
95
|
+
The device to place the result on. If None, inferred from input.
|
|
96
|
+
requires_grad : bool | None, optional
|
|
97
|
+
Whether the result requires gradient computation. If None, inferred from input.
|
|
60
98
|
axis : Axis | None, optional
|
|
61
99
|
Axis or axes along which a sum is performed.
|
|
62
100
|
dtype : DTypeLike | None, optional
|
|
@@ -84,6 +122,9 @@ def sum(
|
|
|
84
122
|
else:
|
|
85
123
|
device_op = device
|
|
86
124
|
|
|
125
|
+
if requires_grad is None:
|
|
126
|
+
requires_grad = a.requires_grad if isinstance(a, Tensor) else False
|
|
127
|
+
|
|
87
128
|
arr = to_tensordata(a, device=device_op)
|
|
88
129
|
if device_op == "cpu":
|
|
89
130
|
kwds = {"axis": axis, "dtype": dtype, "keepdims": keepdims, "where": where}
|
|
@@ -96,7 +137,13 @@ def sum(
|
|
|
96
137
|
if cp is None:
|
|
97
138
|
raise CuPyNotFound(CUPY_NOT_FOUND_MSG)
|
|
98
139
|
y = cp.sum(arr, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
|
|
99
|
-
|
|
140
|
+
|
|
141
|
+
result = Tensor._fast_init(y, device=device_op, requires_grad=requires_grad)
|
|
142
|
+
|
|
143
|
+
if requires_grad and isinstance(a, Tensor) and a.requires_grad:
|
|
144
|
+
_link_sum_backward(result, a, axis, keepdims)
|
|
145
|
+
|
|
146
|
+
return result
|
|
100
147
|
|
|
101
148
|
|
|
102
149
|
def max(
|
|
@@ -104,7 +151,7 @@ def max(
|
|
|
104
151
|
/,
|
|
105
152
|
*,
|
|
106
153
|
device: Device | None = None,
|
|
107
|
-
requires_grad: bool =
|
|
154
|
+
requires_grad: bool | None = None,
|
|
108
155
|
axis: Axis | None = None,
|
|
109
156
|
out: TensorData | None = None,
|
|
110
157
|
keepdims: bool = False,
|
|
@@ -119,9 +166,9 @@ def max(
|
|
|
119
166
|
a : TensorLike
|
|
120
167
|
Input data.
|
|
121
168
|
device : Device | None, optional
|
|
122
|
-
The device to place the result on.
|
|
123
|
-
requires_grad : bool, optional
|
|
124
|
-
Whether the result requires gradient computation.
|
|
169
|
+
The device to place the result on. If None, inferred from input.
|
|
170
|
+
requires_grad : bool | None, optional
|
|
171
|
+
Whether the result requires gradient computation. If None, inferred from input.
|
|
125
172
|
axis : Axis | None, optional
|
|
126
173
|
Axis or axes along which to operate.
|
|
127
174
|
out : TensorData | None, optional
|
|
@@ -146,6 +193,9 @@ def max(
|
|
|
146
193
|
else:
|
|
147
194
|
device_op = device
|
|
148
195
|
|
|
196
|
+
if requires_grad is None:
|
|
197
|
+
requires_grad = a.requires_grad if isinstance(a, Tensor) else False
|
|
198
|
+
|
|
149
199
|
arr = to_tensordata(a, device=device_op)
|
|
150
200
|
if device_op == "cpu":
|
|
151
201
|
kwargs = {"axis": axis, "out": out, "keepdims": keepdims, "where": where}
|
|
@@ -168,7 +218,7 @@ def maximum(
|
|
|
168
218
|
out: TensorData | None = None,
|
|
169
219
|
*,
|
|
170
220
|
device: Device | None = None,
|
|
171
|
-
requires_grad: bool =
|
|
221
|
+
requires_grad: bool | None = None,
|
|
172
222
|
where: TensorDataBool | bool = True,
|
|
173
223
|
casting: Casting = "same_kind",
|
|
174
224
|
order: Order = "K",
|
|
@@ -185,9 +235,9 @@ def maximum(
|
|
|
185
235
|
out : TensorData | None, optional
|
|
186
236
|
The results will be placed in this array.
|
|
187
237
|
device : Device | None, optional
|
|
188
|
-
The device to place the result on.
|
|
189
|
-
requires_grad : bool, optional
|
|
190
|
-
Whether the result requires gradient computation.
|
|
238
|
+
The device to place the result on. If None, inferred from inputs.
|
|
239
|
+
requires_grad : bool | None, optional
|
|
240
|
+
Whether the result requires gradient computation. If None, inferred from inputs.
|
|
191
241
|
where : TensorDataBool | bool, optional
|
|
192
242
|
This condition is broadcast over the input.
|
|
193
243
|
casting : Casting, optional
|
|
@@ -212,6 +262,13 @@ def maximum(
|
|
|
212
262
|
x1, x2, x1_is_tensor=x1_is_tensor, x2_is_tensor=x2_is_tensor, device=device
|
|
213
263
|
)
|
|
214
264
|
|
|
265
|
+
if requires_grad is None:
|
|
266
|
+
requires_grad = False
|
|
267
|
+
if x1_is_tensor:
|
|
268
|
+
requires_grad = requires_grad or x1.requires_grad
|
|
269
|
+
if x2_is_tensor:
|
|
270
|
+
requires_grad = requires_grad or x2.requires_grad
|
|
271
|
+
|
|
215
272
|
_x1: TensorData = to_tensordata(x1, device=device_op)
|
|
216
273
|
_x2: TensorData = to_tensordata(x2, device=device_op)
|
|
217
274
|
|
|
@@ -27,6 +27,11 @@ from ._core import (
|
|
|
27
27
|
clip,
|
|
28
28
|
abs,
|
|
29
29
|
mean,
|
|
30
|
+
sqrt,
|
|
31
|
+
square,
|
|
32
|
+
log,
|
|
33
|
+
exp,
|
|
34
|
+
sign,
|
|
30
35
|
# * comparison
|
|
31
36
|
equal,
|
|
32
37
|
not_equal,
|
|
@@ -510,6 +515,370 @@ class Tensor:
|
|
|
510
515
|
### Element-wise
|
|
511
516
|
###
|
|
512
517
|
|
|
518
|
+
def sqrt(
|
|
519
|
+
self,
|
|
520
|
+
out: TensorData | None = None,
|
|
521
|
+
*,
|
|
522
|
+
device: Device | None = None,
|
|
523
|
+
in_place: bool = False,
|
|
524
|
+
where: TensorDataBool | bool = True,
|
|
525
|
+
casting: Casting = "same_kind",
|
|
526
|
+
order: Order = "K",
|
|
527
|
+
dtype: DTypeLike | None = None,
|
|
528
|
+
subok: bool = True,
|
|
529
|
+
) -> Tensor:
|
|
530
|
+
"""
|
|
531
|
+
Return the non-negative square-root of the tensor, element-wise.
|
|
532
|
+
|
|
533
|
+
Parameters
|
|
534
|
+
----------
|
|
535
|
+
out : TensorData | None, optional
|
|
536
|
+
A location into which the result is stored.
|
|
537
|
+
device : Device | None, optional
|
|
538
|
+
The device to place the result on.
|
|
539
|
+
in_place : bool, optional
|
|
540
|
+
Whether to perform the operation in-place.
|
|
541
|
+
where : TensorDataBool | bool, optional
|
|
542
|
+
This condition is broadcast over the input.
|
|
543
|
+
casting : Casting, optional
|
|
544
|
+
Controls what kind of data casting may occur.
|
|
545
|
+
order : Order, optional
|
|
546
|
+
Controls the memory layout of the result.
|
|
547
|
+
dtype : DTypeLike | None, optional
|
|
548
|
+
Overrides the data type of the result.
|
|
549
|
+
subok : bool, optional
|
|
550
|
+
If True, then sub-classes will be passed-through.
|
|
551
|
+
|
|
552
|
+
Returns
|
|
553
|
+
-------
|
|
554
|
+
Tensor
|
|
555
|
+
A new tensor with the square-root of each element.
|
|
556
|
+
"""
|
|
557
|
+
return sqrt(
|
|
558
|
+
self,
|
|
559
|
+
out=out,
|
|
560
|
+
device=device,
|
|
561
|
+
in_place=in_place,
|
|
562
|
+
where=where,
|
|
563
|
+
casting=casting,
|
|
564
|
+
order=order,
|
|
565
|
+
dtype=dtype,
|
|
566
|
+
subok=subok,
|
|
567
|
+
differentiate=self.requires_grad,
|
|
568
|
+
)
|
|
569
|
+
|
|
570
|
+
def square(
|
|
571
|
+
self,
|
|
572
|
+
out: TensorData | None = None,
|
|
573
|
+
*,
|
|
574
|
+
device: Device | None = None,
|
|
575
|
+
in_place: bool = False,
|
|
576
|
+
where: TensorDataBool | bool = True,
|
|
577
|
+
casting: Casting = "same_kind",
|
|
578
|
+
order: Order = "K",
|
|
579
|
+
dtype: DTypeLike | None = None,
|
|
580
|
+
subok: bool = True,
|
|
581
|
+
) -> Tensor:
|
|
582
|
+
"""
|
|
583
|
+
Return the element-wise square of the tensor.
|
|
584
|
+
|
|
585
|
+
Parameters
|
|
586
|
+
----------
|
|
587
|
+
out : TensorData | None, optional
|
|
588
|
+
A location into which the result is stored.
|
|
589
|
+
device : Device | None, optional
|
|
590
|
+
The device to place the result on.
|
|
591
|
+
in_place : bool, optional
|
|
592
|
+
Whether to perform the operation in-place.
|
|
593
|
+
where : TensorDataBool | bool, optional
|
|
594
|
+
This condition is broadcast over the input.
|
|
595
|
+
casting : Casting, optional
|
|
596
|
+
Controls what kind of data casting may occur.
|
|
597
|
+
order : Order, optional
|
|
598
|
+
Controls the memory layout of the result.
|
|
599
|
+
dtype : DTypeLike | None, optional
|
|
600
|
+
Overrides the data type of the result.
|
|
601
|
+
subok : bool, optional
|
|
602
|
+
If True, then sub-classes will be passed-through.
|
|
603
|
+
|
|
604
|
+
Returns
|
|
605
|
+
-------
|
|
606
|
+
Tensor
|
|
607
|
+
A new tensor with the square of each element.
|
|
608
|
+
"""
|
|
609
|
+
return square(
|
|
610
|
+
self,
|
|
611
|
+
out=out,
|
|
612
|
+
device=device,
|
|
613
|
+
in_place=in_place,
|
|
614
|
+
where=where,
|
|
615
|
+
casting=casting,
|
|
616
|
+
order=order,
|
|
617
|
+
dtype=dtype,
|
|
618
|
+
subok=subok,
|
|
619
|
+
differentiate=self.requires_grad,
|
|
620
|
+
)
|
|
621
|
+
|
|
622
|
+
def log(
|
|
623
|
+
self,
|
|
624
|
+
out: TensorData | None = None,
|
|
625
|
+
*,
|
|
626
|
+
device: Device | None = None,
|
|
627
|
+
in_place: bool = False,
|
|
628
|
+
where: TensorDataBool | bool = True,
|
|
629
|
+
casting: Casting = "same_kind",
|
|
630
|
+
order: Order = "K",
|
|
631
|
+
dtype: DTypeLike | None = None,
|
|
632
|
+
subok: bool = True,
|
|
633
|
+
) -> Tensor:
|
|
634
|
+
"""
|
|
635
|
+
Natural logarithm, element-wise.
|
|
636
|
+
|
|
637
|
+
Parameters
|
|
638
|
+
----------
|
|
639
|
+
out : TensorData | None, optional
|
|
640
|
+
A location into which the result is stored.
|
|
641
|
+
device : Device | None, optional
|
|
642
|
+
The device to place the result on.
|
|
643
|
+
in_place : bool, optional
|
|
644
|
+
Whether to perform the operation in-place.
|
|
645
|
+
where : TensorDataBool | bool, optional
|
|
646
|
+
This condition is broadcast over the input.
|
|
647
|
+
casting : Casting, optional
|
|
648
|
+
Controls what kind of data casting may occur.
|
|
649
|
+
order : Order, optional
|
|
650
|
+
Controls the memory layout of the result.
|
|
651
|
+
dtype : DTypeLike | None, optional
|
|
652
|
+
Overrides the data type of the result.
|
|
653
|
+
subok : bool, optional
|
|
654
|
+
If True, then sub-classes will be passed-through.
|
|
655
|
+
|
|
656
|
+
Returns
|
|
657
|
+
-------
|
|
658
|
+
Tensor
|
|
659
|
+
A new tensor with the natural logarithm of each element.
|
|
660
|
+
"""
|
|
661
|
+
return log(
|
|
662
|
+
self,
|
|
663
|
+
out=out,
|
|
664
|
+
device=device,
|
|
665
|
+
in_place=in_place,
|
|
666
|
+
where=where,
|
|
667
|
+
casting=casting,
|
|
668
|
+
order=order,
|
|
669
|
+
dtype=dtype,
|
|
670
|
+
subok=subok,
|
|
671
|
+
differentiate=self.requires_grad,
|
|
672
|
+
)
|
|
673
|
+
|
|
674
|
+
def exp(
|
|
675
|
+
self,
|
|
676
|
+
out: TensorData | None = None,
|
|
677
|
+
*,
|
|
678
|
+
device: Device | None = None,
|
|
679
|
+
in_place: bool = False,
|
|
680
|
+
where: TensorDataBool | bool = True,
|
|
681
|
+
casting: Casting = "same_kind",
|
|
682
|
+
order: Order = "K",
|
|
683
|
+
dtype: DTypeLike | None = None,
|
|
684
|
+
subok: bool = True,
|
|
685
|
+
) -> Tensor:
|
|
686
|
+
"""
|
|
687
|
+
Calculate the exponential of all elements in the tensor.
|
|
688
|
+
|
|
689
|
+
Parameters
|
|
690
|
+
----------
|
|
691
|
+
out : TensorData | None, optional
|
|
692
|
+
A location into which the result is stored.
|
|
693
|
+
device : Device | None, optional
|
|
694
|
+
The device to place the result on.
|
|
695
|
+
in_place : bool, optional
|
|
696
|
+
Whether to perform the operation in-place.
|
|
697
|
+
where : TensorDataBool | bool, optional
|
|
698
|
+
This condition is broadcast over the input.
|
|
699
|
+
casting : Casting, optional
|
|
700
|
+
Controls what kind of data casting may occur.
|
|
701
|
+
order : Order, optional
|
|
702
|
+
Controls the memory layout of the result.
|
|
703
|
+
dtype : DTypeLike | None, optional
|
|
704
|
+
Overrides the data type of the result.
|
|
705
|
+
subok : bool, optional
|
|
706
|
+
If True, then sub-classes will be passed-through.
|
|
707
|
+
|
|
708
|
+
Returns
|
|
709
|
+
-------
|
|
710
|
+
Tensor
|
|
711
|
+
A new tensor with the exponential of each element.
|
|
712
|
+
"""
|
|
713
|
+
return exp(
|
|
714
|
+
self,
|
|
715
|
+
out=out,
|
|
716
|
+
device=device,
|
|
717
|
+
in_place=in_place,
|
|
718
|
+
where=where,
|
|
719
|
+
casting=casting,
|
|
720
|
+
order=order,
|
|
721
|
+
dtype=dtype,
|
|
722
|
+
subok=subok,
|
|
723
|
+
differentiate=self.requires_grad,
|
|
724
|
+
)
|
|
725
|
+
|
|
726
|
+
def sign(
|
|
727
|
+
self,
|
|
728
|
+
out: TensorData | None = None,
|
|
729
|
+
*,
|
|
730
|
+
device: Device | None = None,
|
|
731
|
+
in_place: bool = False,
|
|
732
|
+
where: TensorDataBool | bool = True,
|
|
733
|
+
casting: Casting = "same_kind",
|
|
734
|
+
order: Order = "K",
|
|
735
|
+
dtype: DTypeLike | None = None,
|
|
736
|
+
subok: bool = True,
|
|
737
|
+
) -> Tensor:
|
|
738
|
+
"""
|
|
739
|
+
Returns an element-wise indication of the sign of a number.
|
|
740
|
+
|
|
741
|
+
Parameters
|
|
742
|
+
----------
|
|
743
|
+
out : TensorData | None, optional
|
|
744
|
+
A location into which the result is stored.
|
|
745
|
+
device : Device | None, optional
|
|
746
|
+
The device to place the result on.
|
|
747
|
+
in_place : bool, optional
|
|
748
|
+
Whether to perform the operation in-place.
|
|
749
|
+
where : TensorDataBool | bool, optional
|
|
750
|
+
This condition is broadcast over the input.
|
|
751
|
+
casting : Casting, optional
|
|
752
|
+
Controls what kind of data casting may occur.
|
|
753
|
+
order : Order, optional
|
|
754
|
+
Controls the memory layout of the result.
|
|
755
|
+
dtype : DTypeLike | None, optional
|
|
756
|
+
Overrides the data type of the result.
|
|
757
|
+
subok : bool, optional
|
|
758
|
+
If True, then sub-classes will be passed-through.
|
|
759
|
+
|
|
760
|
+
Returns
|
|
761
|
+
-------
|
|
762
|
+
Tensor
|
|
763
|
+
A new tensor with the sign of each element.
|
|
764
|
+
"""
|
|
765
|
+
return sign(
|
|
766
|
+
self,
|
|
767
|
+
out=out,
|
|
768
|
+
device=device,
|
|
769
|
+
in_place=in_place,
|
|
770
|
+
where=where,
|
|
771
|
+
casting=casting,
|
|
772
|
+
order=order,
|
|
773
|
+
dtype=dtype,
|
|
774
|
+
subok=subok,
|
|
775
|
+
differentiate=self.requires_grad,
|
|
776
|
+
)
|
|
777
|
+
|
|
778
|
+
def abs(
|
|
779
|
+
self,
|
|
780
|
+
out: TensorData | None = None,
|
|
781
|
+
*,
|
|
782
|
+
device: Device | None = None,
|
|
783
|
+
in_place: bool = False,
|
|
784
|
+
where: TensorDataBool | bool = True,
|
|
785
|
+
casting: Casting = "same_kind",
|
|
786
|
+
order: Order = "K",
|
|
787
|
+
dtype: DTypeLike | None = None,
|
|
788
|
+
subok: bool = True,
|
|
789
|
+
) -> Tensor:
|
|
790
|
+
"""
|
|
791
|
+
Calculate the absolute value element-wise.
|
|
792
|
+
|
|
793
|
+
Parameters
|
|
794
|
+
----------
|
|
795
|
+
out : TensorData | None, optional
|
|
796
|
+
A location into which the result is stored.
|
|
797
|
+
device : Device | None, optional
|
|
798
|
+
The device to place the result on.
|
|
799
|
+
in_place : bool, optional
|
|
800
|
+
Whether to perform the operation in-place.
|
|
801
|
+
where : TensorDataBool | bool, optional
|
|
802
|
+
This condition is broadcast over the input.
|
|
803
|
+
casting : Casting, optional
|
|
804
|
+
Controls what kind of data casting may occur.
|
|
805
|
+
order : Order, optional
|
|
806
|
+
Controls the memory layout of the result.
|
|
807
|
+
dtype : DTypeLike | None, optional
|
|
808
|
+
Overrides the data type of the result.
|
|
809
|
+
subok : bool, optional
|
|
810
|
+
If True, then sub-classes will be passed-through.
|
|
811
|
+
|
|
812
|
+
Returns
|
|
813
|
+
-------
|
|
814
|
+
Tensor
|
|
815
|
+
A new tensor with the absolute value of each element.
|
|
816
|
+
"""
|
|
817
|
+
return abs(
|
|
818
|
+
self,
|
|
819
|
+
out=out,
|
|
820
|
+
device=device,
|
|
821
|
+
in_place=in_place,
|
|
822
|
+
where=where,
|
|
823
|
+
casting=casting,
|
|
824
|
+
order=order,
|
|
825
|
+
dtype=dtype,
|
|
826
|
+
subok=subok,
|
|
827
|
+
differentiate=self.requires_grad,
|
|
828
|
+
)
|
|
829
|
+
|
|
830
|
+
def neg(
|
|
831
|
+
self,
|
|
832
|
+
out: TensorData | None = None,
|
|
833
|
+
*,
|
|
834
|
+
device: Device | None = None,
|
|
835
|
+
in_place: bool = False,
|
|
836
|
+
where: TensorDataBool | bool = True,
|
|
837
|
+
casting: Casting = "same_kind",
|
|
838
|
+
order: Order = "K",
|
|
839
|
+
dtype: DTypeLike | None = None,
|
|
840
|
+
subok: bool = True,
|
|
841
|
+
) -> Tensor:
|
|
842
|
+
"""
|
|
843
|
+
Numerical negative, element-wise.
|
|
844
|
+
|
|
845
|
+
Parameters
|
|
846
|
+
----------
|
|
847
|
+
out : TensorData | None, optional
|
|
848
|
+
A location into which the result is stored.
|
|
849
|
+
device : Device | None, optional
|
|
850
|
+
The device to place the result on.
|
|
851
|
+
in_place : bool, optional
|
|
852
|
+
Whether to perform the operation in-place.
|
|
853
|
+
where : TensorDataBool | bool, optional
|
|
854
|
+
This condition is broadcast over the input.
|
|
855
|
+
casting : Casting, optional
|
|
856
|
+
Controls what kind of data casting may occur.
|
|
857
|
+
order : Order, optional
|
|
858
|
+
Controls the memory layout of the result.
|
|
859
|
+
dtype : DTypeLike | None, optional
|
|
860
|
+
Overrides the data type of the result.
|
|
861
|
+
subok : bool, optional
|
|
862
|
+
If True, then sub-classes will be passed-through.
|
|
863
|
+
|
|
864
|
+
Returns
|
|
865
|
+
-------
|
|
866
|
+
Tensor
|
|
867
|
+
A new tensor with the negative of each element.
|
|
868
|
+
"""
|
|
869
|
+
return negative(
|
|
870
|
+
self,
|
|
871
|
+
out=out,
|
|
872
|
+
device=device,
|
|
873
|
+
in_place=in_place,
|
|
874
|
+
where=where,
|
|
875
|
+
casting=casting,
|
|
876
|
+
order=order,
|
|
877
|
+
dtype=dtype,
|
|
878
|
+
subok=subok,
|
|
879
|
+
differentiate=self.requires_grad,
|
|
880
|
+
)
|
|
881
|
+
|
|
513
882
|
def clip(
|
|
514
883
|
self,
|
|
515
884
|
a_min: TensorLike,
|
|
@@ -517,8 +886,8 @@ class Tensor:
|
|
|
517
886
|
/,
|
|
518
887
|
out: TensorData | None = None,
|
|
519
888
|
*,
|
|
520
|
-
requires_grad: bool =
|
|
521
|
-
device: Device =
|
|
889
|
+
requires_grad: bool | None = None,
|
|
890
|
+
device: Device | None = None,
|
|
522
891
|
where: TensorDataBool | bool = True,
|
|
523
892
|
casting: Casting = "same_kind",
|
|
524
893
|
order: Order = "K",
|
|
@@ -536,10 +905,10 @@ class Tensor:
|
|
|
536
905
|
Maximum value.
|
|
537
906
|
out : TensorData | None, optional
|
|
538
907
|
The results will be placed in this array.
|
|
539
|
-
requires_grad : bool, optional
|
|
540
|
-
Whether the result requires gradient computation.
|
|
541
|
-
device : Device, optional
|
|
542
|
-
The device to place the result on.
|
|
908
|
+
requires_grad : bool | None, optional
|
|
909
|
+
Whether the result requires gradient computation. If None, inferred from inputs.
|
|
910
|
+
device : Device | None, optional
|
|
911
|
+
The device to place the result on. If None, inferred from inputs.
|
|
543
912
|
where : TensorDataBool | bool, optional
|
|
544
913
|
This condition is broadcast over the input. At locations where the
|
|
545
914
|
condition is True, the out array will be set to the ufunc result.
|
|
@@ -630,8 +999,8 @@ class Tensor:
|
|
|
630
999
|
self,
|
|
631
1000
|
/,
|
|
632
1001
|
*,
|
|
633
|
-
device: Device =
|
|
634
|
-
requires_grad: bool =
|
|
1002
|
+
device: Device | None = None,
|
|
1003
|
+
requires_grad: bool | None = None,
|
|
635
1004
|
axis: Axis | None = None,
|
|
636
1005
|
dtype: DTypeLike | None = None,
|
|
637
1006
|
out: TensorData | None = None,
|
|
@@ -644,10 +1013,10 @@ class Tensor:
|
|
|
644
1013
|
|
|
645
1014
|
Parameters
|
|
646
1015
|
----------
|
|
647
|
-
device : Device, optional
|
|
648
|
-
The device to place the result on.
|
|
649
|
-
requires_grad : bool, optional
|
|
650
|
-
Whether the result requires gradient computation.
|
|
1016
|
+
device : Device | None, optional
|
|
1017
|
+
The device to place the result on. If None, inferred from input.
|
|
1018
|
+
requires_grad : bool | None, optional
|
|
1019
|
+
Whether the result requires gradient computation. If None, inferred from input.
|
|
651
1020
|
axis : Axis | None, optional
|
|
652
1021
|
Axis or axes along which a sum is performed.
|
|
653
1022
|
dtype : DTypeLike | None, optional
|
|
@@ -684,8 +1053,8 @@ class Tensor:
|
|
|
684
1053
|
self,
|
|
685
1054
|
/,
|
|
686
1055
|
*,
|
|
687
|
-
device: Device =
|
|
688
|
-
requires_grad: bool =
|
|
1056
|
+
device: Device | None = None,
|
|
1057
|
+
requires_grad: bool | None = None,
|
|
689
1058
|
axis: Axis | None = None,
|
|
690
1059
|
out: TensorData | None = None,
|
|
691
1060
|
keepdims: bool = False,
|
|
@@ -697,10 +1066,10 @@ class Tensor:
|
|
|
697
1066
|
|
|
698
1067
|
Parameters
|
|
699
1068
|
----------
|
|
700
|
-
device : Device, optional
|
|
701
|
-
The device to place the result on.
|
|
702
|
-
requires_grad : bool, optional
|
|
703
|
-
Whether the result requires gradient computation.
|
|
1069
|
+
device : Device | None, optional
|
|
1070
|
+
The device to place the result on. If None, inferred from input.
|
|
1071
|
+
requires_grad : bool | None, optional
|
|
1072
|
+
Whether the result requires gradient computation. If None, inferred from input.
|
|
704
1073
|
axis : Axis | None, optional
|
|
705
1074
|
Axis or axes along which to operate.
|
|
706
1075
|
out : TensorData | None, optional
|
|
@@ -735,8 +1104,8 @@ class Tensor:
|
|
|
735
1104
|
/,
|
|
736
1105
|
out: TensorData | None = None,
|
|
737
1106
|
*,
|
|
738
|
-
device: Device =
|
|
739
|
-
requires_grad: bool =
|
|
1107
|
+
device: Device | None = None,
|
|
1108
|
+
requires_grad: bool | None = None,
|
|
740
1109
|
where: TensorDataBool | bool = True,
|
|
741
1110
|
casting: Casting = "same_kind",
|
|
742
1111
|
order: Order = "K",
|
|
@@ -752,10 +1121,10 @@ class Tensor:
|
|
|
752
1121
|
The array holding the elements to compare.
|
|
753
1122
|
out : TensorData | None, optional
|
|
754
1123
|
The results will be placed in this array.
|
|
755
|
-
device : Device, optional
|
|
756
|
-
The device to place the result on.
|
|
757
|
-
requires_grad : bool, optional
|
|
758
|
-
Whether the result requires gradient computation.
|
|
1124
|
+
device : Device | None, optional
|
|
1125
|
+
The device to place the result on. If None, inferred from inputs.
|
|
1126
|
+
requires_grad : bool | None, optional
|
|
1127
|
+
Whether the result requires gradient computation. If None, inferred from inputs.
|
|
759
1128
|
where : TensorDataBool | bool, optional
|
|
760
1129
|
This condition is broadcast over the input.
|
|
761
1130
|
casting : Casting, optional
|
|
@@ -889,6 +1258,9 @@ class Tensor:
|
|
|
889
1258
|
def __neg__(self) -> Tensor:
|
|
890
1259
|
return negative(self, differentiate=self.requires_grad)
|
|
891
1260
|
|
|
1261
|
+
def __abs__(self) -> Tensor:
|
|
1262
|
+
return abs(self, differentiate=self.requires_grad)
|
|
1263
|
+
|
|
892
1264
|
###
|
|
893
1265
|
### Misc dunder
|
|
894
1266
|
###
|
|
@@ -22,7 +22,6 @@ src/depthtensor.egg-info/dependency_links.txt
|
|
|
22
22
|
src/depthtensor.egg-info/requires.txt
|
|
23
23
|
src/depthtensor.egg-info/top_level.txt
|
|
24
24
|
tests/test_autodiff.py
|
|
25
|
-
tests/test_autodiff_extended.py
|
|
26
25
|
tests/test_ops_forward.py
|
|
27
26
|
tests/test_tensor_errors.py
|
|
28
27
|
tests/test_tensor_init.py
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import pytest
|
|
2
2
|
import numpy as np
|
|
3
3
|
from src.depthtensor import Tensor, differentiate
|
|
4
|
+
import src.depthtensor._core.ops.elementwise as el
|
|
4
5
|
import src.depthtensor as dt
|
|
5
6
|
|
|
6
7
|
def test_autodiff_add():
|
|
@@ -146,3 +147,119 @@ def test_accumulate_grad():
|
|
|
146
147
|
# Grad should accumulate: 4.0 + 4.0 = 8.0
|
|
147
148
|
assert np.allclose(x.grad, [8.0])
|
|
148
149
|
|
|
150
|
+
def test_autodiff_sub():
|
|
151
|
+
x = Tensor([5.0], requires_grad=True)
|
|
152
|
+
y = Tensor([2.0], requires_grad=True)
|
|
153
|
+
z = x - y
|
|
154
|
+
differentiate(z)
|
|
155
|
+
assert np.allclose(x.grad, [1.0])
|
|
156
|
+
assert np.allclose(y.grad, [-1.0])
|
|
157
|
+
|
|
158
|
+
def test_autodiff_div():
|
|
159
|
+
x = Tensor([10.0], requires_grad=True)
|
|
160
|
+
y = Tensor([2.0], requires_grad=True)
|
|
161
|
+
z = x / y
|
|
162
|
+
differentiate(z)
|
|
163
|
+
# z = x * y^-1
|
|
164
|
+
# dz/dx = 1/y = 0.5
|
|
165
|
+
# dz/dy = -x/y^2 = -10/4 = -2.5
|
|
166
|
+
assert np.allclose(x.grad, [0.5])
|
|
167
|
+
assert np.allclose(y.grad, [-2.5])
|
|
168
|
+
|
|
169
|
+
def test_autodiff_exp():
|
|
170
|
+
x = Tensor([2.0], requires_grad=True)
|
|
171
|
+
z = el.exp(x, differentiate=True)
|
|
172
|
+
differentiate(z)
|
|
173
|
+
# z = e^x
|
|
174
|
+
# dz/dx = e^x
|
|
175
|
+
assert np.allclose(x.grad, np.exp([2.0]))
|
|
176
|
+
|
|
177
|
+
def test_autodiff_log():
|
|
178
|
+
x = Tensor([2.0], requires_grad=True)
|
|
179
|
+
z = el.log(x, differentiate=True)
|
|
180
|
+
differentiate(z)
|
|
181
|
+
# z = ln(x)
|
|
182
|
+
# dz/dx = 1/x = 0.5
|
|
183
|
+
assert np.allclose(x.grad, [0.5])
|
|
184
|
+
|
|
185
|
+
def test_autodiff_sqrt():
|
|
186
|
+
x = Tensor([4.0], requires_grad=True)
|
|
187
|
+
z = el.sqrt(x, differentiate=True)
|
|
188
|
+
differentiate(z)
|
|
189
|
+
# z = sqrt(x)
|
|
190
|
+
# dz/dx = 1/(2sqrt(x)) = 1/4 = 0.25
|
|
191
|
+
assert np.allclose(x.grad, [0.25])
|
|
192
|
+
|
|
193
|
+
def test_autodiff_square():
|
|
194
|
+
x = Tensor([3.0], requires_grad=True)
|
|
195
|
+
z = el.square(x, differentiate=True)
|
|
196
|
+
differentiate(z)
|
|
197
|
+
# z = x^2
|
|
198
|
+
# dz/dx = 2x = 6
|
|
199
|
+
assert np.allclose(x.grad, [6.0])
|
|
200
|
+
|
|
201
|
+
def test_autodiff_abs():
|
|
202
|
+
x = Tensor([-2.0], requires_grad=True)
|
|
203
|
+
z = el.abs(x, differentiate=True)
|
|
204
|
+
differentiate(z)
|
|
205
|
+
# z = |x|
|
|
206
|
+
# dz/dx = sign(x) = -1
|
|
207
|
+
assert np.allclose(x.grad, [-1.0])
|
|
208
|
+
|
|
209
|
+
def test_autodiff_transpose():
|
|
210
|
+
x = Tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
|
|
211
|
+
z = x.transpose(None) # Transpose
|
|
212
|
+
differentiate(z)
|
|
213
|
+
# dz/dx_ij should correspond to mapping.
|
|
214
|
+
# z_ji = x_ij
|
|
215
|
+
# if we sum(z) implicitly (grad=ones), then d(sum z)/dx_ij = 1.
|
|
216
|
+
assert np.allclose(x.grad, [[1.0, 1.0], [1.0, 1.0]])
|
|
217
|
+
|
|
218
|
+
def test_autodiff_sum():
|
|
219
|
+
x = Tensor([1.0, 2.0], requires_grad=True)
|
|
220
|
+
z = x.sum(requires_grad=True)
|
|
221
|
+
differentiate(z)
|
|
222
|
+
|
|
223
|
+
# z = x[0] + x[1]
|
|
224
|
+
# dz/dx = [1.0, 1.0]
|
|
225
|
+
assert np.allclose(x.grad, [1.0, 1.0])
|
|
226
|
+
|
|
227
|
+
def test_max_not_linked():
|
|
228
|
+
x = Tensor([1.0, 5.0], requires_grad=True)
|
|
229
|
+
z = x.max(requires_grad=True)
|
|
230
|
+
differentiate(z)
|
|
231
|
+
assert x.grad is None or np.all(x.grad == 0)
|
|
232
|
+
|
|
233
|
+
def test_maximum_not_linked():
|
|
234
|
+
x = Tensor([1.0, 5.0], requires_grad=True)
|
|
235
|
+
y = Tensor([2.0, 2.0], requires_grad=True)
|
|
236
|
+
z = x.maximum(y, requires_grad=True)
|
|
237
|
+
differentiate(z)
|
|
238
|
+
assert x.grad is None or np.all(x.grad == 0)
|
|
239
|
+
assert y.grad is None or np.all(y.grad == 0)
|
|
240
|
+
|
|
241
|
+
def test_clip_not_linked():
|
|
242
|
+
x = Tensor([1.0, 10.0], requires_grad=True)
|
|
243
|
+
z = x.clip(2, 5, requires_grad=True)
|
|
244
|
+
differentiate(z)
|
|
245
|
+
assert x.grad is None or np.all(x.grad == 0)
|
|
246
|
+
|
|
247
|
+
def test_autodiff_power_tensor():
|
|
248
|
+
x = Tensor([2.0], requires_grad=True)
|
|
249
|
+
y = Tensor([3.0], requires_grad=True)
|
|
250
|
+
z = x ** y
|
|
251
|
+
differentiate(z)
|
|
252
|
+
# z = x^y
|
|
253
|
+
# dz/dx = y * x^(y-1) = 3 * 2^2 = 12
|
|
254
|
+
# dz/dy = x^y * ln(x) = 8 * ln(2)
|
|
255
|
+
assert np.allclose(x.grad, [12.0])
|
|
256
|
+
assert np.allclose(y.grad, [8.0 * np.log(2.0)])
|
|
257
|
+
|
|
258
|
+
def test_autodiff_mean_axis():
|
|
259
|
+
x = Tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
|
|
260
|
+
# Mean over axis 0 -> [2.0, 3.0]
|
|
261
|
+
z = x.mean(axis=0)
|
|
262
|
+
differentiate(z)
|
|
263
|
+
# z_j = 0.5 * (x_0j + x_1j)
|
|
264
|
+
# dz_j / dx_ij = 0.5
|
|
265
|
+
assert np.allclose(x.grad, [[0.5, 0.5], [0.5, 0.5]])
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import pytest
|
|
2
|
-
import numpy as np
|
|
3
|
-
from src.depthtensor import Tensor, differentiate
|
|
4
|
-
import src.depthtensor._core.ops.elementwise as el
|
|
5
|
-
|
|
6
|
-
def test_autodiff_sub():
|
|
7
|
-
x = Tensor([5.0], requires_grad=True)
|
|
8
|
-
y = Tensor([2.0], requires_grad=True)
|
|
9
|
-
z = x - y
|
|
10
|
-
differentiate(z)
|
|
11
|
-
assert np.allclose(x.grad, [1.0])
|
|
12
|
-
assert np.allclose(y.grad, [-1.0])
|
|
13
|
-
|
|
14
|
-
def test_autodiff_div():
|
|
15
|
-
x = Tensor([10.0], requires_grad=True)
|
|
16
|
-
y = Tensor([2.0], requires_grad=True)
|
|
17
|
-
z = x / y
|
|
18
|
-
differentiate(z)
|
|
19
|
-
# z = x * y^-1
|
|
20
|
-
# dz/dx = 1/y = 0.5
|
|
21
|
-
# dz/dy = -x/y^2 = -10/4 = -2.5
|
|
22
|
-
assert np.allclose(x.grad, [0.5])
|
|
23
|
-
assert np.allclose(y.grad, [-2.5])
|
|
24
|
-
|
|
25
|
-
def test_autodiff_exp():
|
|
26
|
-
x = Tensor([2.0], requires_grad=True)
|
|
27
|
-
z = el.exp(x, differentiate=True)
|
|
28
|
-
differentiate(z)
|
|
29
|
-
# z = e^x
|
|
30
|
-
# dz/dx = e^x
|
|
31
|
-
assert np.allclose(x.grad, np.exp([2.0]))
|
|
32
|
-
|
|
33
|
-
def test_autodiff_log():
|
|
34
|
-
x = Tensor([2.0], requires_grad=True)
|
|
35
|
-
z = el.log(x, differentiate=True)
|
|
36
|
-
differentiate(z)
|
|
37
|
-
# z = ln(x)
|
|
38
|
-
# dz/dx = 1/x = 0.5
|
|
39
|
-
assert np.allclose(x.grad, [0.5])
|
|
40
|
-
|
|
41
|
-
def test_autodiff_sqrt():
|
|
42
|
-
x = Tensor([4.0], requires_grad=True)
|
|
43
|
-
z = el.sqrt(x, differentiate=True)
|
|
44
|
-
differentiate(z)
|
|
45
|
-
# z = sqrt(x)
|
|
46
|
-
# dz/dx = 1/(2sqrt(x)) = 1/4 = 0.25
|
|
47
|
-
assert np.allclose(x.grad, [0.25])
|
|
48
|
-
|
|
49
|
-
def test_autodiff_square():
|
|
50
|
-
x = Tensor([3.0], requires_grad=True)
|
|
51
|
-
z = el.square(x, differentiate=True)
|
|
52
|
-
differentiate(z)
|
|
53
|
-
# z = x^2
|
|
54
|
-
# dz/dx = 2x = 6
|
|
55
|
-
assert np.allclose(x.grad, [6.0])
|
|
56
|
-
|
|
57
|
-
def test_autodiff_abs():
|
|
58
|
-
x = Tensor([-2.0], requires_grad=True)
|
|
59
|
-
z = el.abs(x, differentiate=True)
|
|
60
|
-
differentiate(z)
|
|
61
|
-
# z = |x|
|
|
62
|
-
# dz/dx = sign(x) = -1
|
|
63
|
-
assert np.allclose(x.grad, [-1.0])
|
|
64
|
-
|
|
65
|
-
def test_autodiff_transpose():
|
|
66
|
-
x = Tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
|
|
67
|
-
z = x.transpose(None) # Transpose
|
|
68
|
-
differentiate(z)
|
|
69
|
-
# dz/dx_ij should correspond to mapping.
|
|
70
|
-
# z_ji = x_ij
|
|
71
|
-
# if we sum(z) implicitly (grad=ones), then d(sum z)/dx_ij = 1.
|
|
72
|
-
assert np.allclose(x.grad, [[1.0, 1.0], [1.0, 1.0]])
|
|
73
|
-
|
|
74
|
-
def test_autodiff_sum_not_linked():
|
|
75
|
-
# Confirming sum is not currently differentiable in the graph sense
|
|
76
|
-
# It returns a Tensor with requires_grad=True but no backward link in this version
|
|
77
|
-
x = Tensor([1.0, 2.0], requires_grad=True)
|
|
78
|
-
z = x.sum(requires_grad=True)
|
|
79
|
-
|
|
80
|
-
# differentiate(z) should probably fail or do nothing if no backward is attached
|
|
81
|
-
# implementation: if t.backward is None and prev is empty, it continues.
|
|
82
|
-
# sum returns a tensor with NO prev (it's created fresh).
|
|
83
|
-
# So differentiate stops immediately.
|
|
84
|
-
differentiate(z)
|
|
85
|
-
|
|
86
|
-
assert x.grad is None or np.all(x.grad == 0) # Should be None actually if not initialized
|
|
87
|
-
|
|
88
|
-
def test_max_not_linked():
|
|
89
|
-
x = Tensor([1.0, 5.0], requires_grad=True)
|
|
90
|
-
z = x.max(requires_grad=True)
|
|
91
|
-
differentiate(z)
|
|
92
|
-
assert x.grad is None or np.all(x.grad == 0)
|
|
93
|
-
|
|
94
|
-
def test_maximum_not_linked():
|
|
95
|
-
x = Tensor([1.0, 5.0], requires_grad=True)
|
|
96
|
-
y = Tensor([2.0, 2.0], requires_grad=True)
|
|
97
|
-
z = x.maximum(y, requires_grad=True)
|
|
98
|
-
differentiate(z)
|
|
99
|
-
assert x.grad is None or np.all(x.grad == 0)
|
|
100
|
-
assert y.grad is None or np.all(y.grad == 0)
|
|
101
|
-
|
|
102
|
-
def test_clip_not_linked():
|
|
103
|
-
x = Tensor([1.0, 10.0], requires_grad=True)
|
|
104
|
-
z = x.clip(2, 5, requires_grad=True)
|
|
105
|
-
differentiate(z)
|
|
106
|
-
assert x.grad is None or np.all(x.grad == 0)
|
|
107
|
-
|
|
108
|
-
def test_autodiff_power_tensor():
|
|
109
|
-
x = Tensor([2.0], requires_grad=True)
|
|
110
|
-
y = Tensor([3.0], requires_grad=True)
|
|
111
|
-
z = x ** y
|
|
112
|
-
differentiate(z)
|
|
113
|
-
# z = x^y
|
|
114
|
-
# dz/dx = y * x^(y-1) = 3 * 2^2 = 12
|
|
115
|
-
# dz/dy = x^y * ln(x) = 8 * ln(2)
|
|
116
|
-
assert np.allclose(x.grad, [12.0])
|
|
117
|
-
assert np.allclose(y.grad, [8.0 * np.log(2.0)])
|
|
118
|
-
|
|
119
|
-
def test_autodiff_mean_axis():
|
|
120
|
-
x = Tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
|
|
121
|
-
# Mean over axis 0 -> [2.0, 3.0]
|
|
122
|
-
z = x.mean(axis=0)
|
|
123
|
-
differentiate(z)
|
|
124
|
-
# z_j = 0.5 * (x_0j + x_1j)
|
|
125
|
-
# dz_j / dx_ij = 0.5
|
|
126
|
-
assert np.allclose(x.grad, [[0.5, 0.5], [0.5, 0.5]])
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|