Skip to content

Commit dd50a08

Browse files
committed
minor changes
1 parent 8be6a65 commit dd50a08

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

neunet/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@ def tensor(data, requires_grad=False, dtype=float32, device="cpu"):
3232
return Tensor(data, requires_grad=requires_grad, dtype=dtype, device=device)
3333

3434

35-
def ones(*shape, dtype=None, requires_grad=True, device="cpu"):
35+
def ones(*shape, dtype=None, requires_grad=False, device="cpu"):
3636
shape = tuple(*shape) if all(isinstance(arg, (list, tuple)) for arg in shape) else shape
3737

3838
return Tensor(np.ones(shape, dtype=dtype), requires_grad=requires_grad, device=device)
3939

4040

41-
def zeros(*shape, dtype=None, requires_grad=True, device="cpu"):
41+
def zeros(*shape, dtype=None, requires_grad=False, device="cpu"):
4242
shape = tuple(*shape) if all(isinstance(arg, (list, tuple)) for arg in shape) else shape
4343

4444
return Tensor(np.zeros(shape, dtype=dtype), requires_grad=requires_grad, device=device)
4545

4646

47-
def rand(*shape, dtype=None, requires_grad=True, device="cpu"):
47+
def rand(*shape, dtype=None, requires_grad=False, device="cpu"):
4848
shape = tuple(*shape) if all(isinstance(arg, (list, tuple)) for arg in shape) else shape
4949

5050
return Tensor(np.random.rand(*shape).astype(dtype), requires_grad=requires_grad, device=device)
5151

5252

53-
def randn(*shape, dtype=None, requires_grad=True, device="cpu"):
53+
def randn(*shape, dtype=None, requires_grad=False, device="cpu"):
5454
shape = tuple(*shape) if all(isinstance(arg, (list, tuple)) for arg in shape) else shape
5555

5656
return Tensor(
@@ -60,7 +60,7 @@ def randn(*shape, dtype=None, requires_grad=True, device="cpu"):
6060
)
6161

6262

63-
def arange(start=0, end=None, step=1, dtype=None, requires_grad=True, device="cpu"):
63+
def arange(start=0, end=None, step=1, dtype=None, requires_grad=False, device="cpu"):
6464
if end is None:
6565
start, end = 0, start
6666
return Tensor(
@@ -70,11 +70,11 @@ def arange(start=0, end=None, step=1, dtype=None, requires_grad=True, device="cp
7070
)
7171

7272

73-
def ones_like(tensor, dtype=None, requires_grad=True, device="cpu"):
73+
def ones_like(tensor, dtype=None, requires_grad=False, device="cpu"):
7474
return Tensor(np.ones_like(tensor.data, dtype), requires_grad=requires_grad, device=device)
7575

7676

77-
def zeros_like(tensor, dtype=None, requires_grad=True, device="cpu"):
77+
def zeros_like(tensor, dtype=None, requires_grad=False, device="cpu"):
7878
return Tensor(np.zeros_like(tensor.data, dtype), requires_grad=requires_grad, device=device)
7979

8080

@@ -195,6 +195,7 @@ def flip(x, axis):
195195
return x.flip(axis=axis)
196196

197197
def where(condition, x, y):
198+
x = tensor(x, device=condition.device) if not isinstance(x, Tensor) else x
198199
return x.where(condition, y)
199200

200201
def equal(x, y):

neunet/autograd.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def flip(self, axis: Any) -> 'Tensor':
356356
device=self.device,
357357
)
358358

359-
def where(self, condition: Union[Any, 'Tensor'], t: Union[Any, 'Tensor']) -> 'Tensor':
359+
def where(self, condition: 'Tensor', t: Union[Any, 'Tensor']) -> 'Tensor':
360360
condition = self.tensor(condition)
361361
t = self.tensor(t)
362362

@@ -586,8 +586,15 @@ def __getitem__(
586586
"getitem",
587587
requires_grad=self.requires_grad,
588588
device=self.device,
589+
dtype=self.dtype
589590
)
590591

592+
def __setitem__(self, key, value: Union[Any, 'Tensor']):
593+
if self.requires_grad:
594+
raise RuntimeError("Cannot assign values to a tensor with requires_grad=True")
595+
value = self.tensor(value)
596+
self.data[key] = value.data
597+
591598
def __array__(self, dtype: Any=None) -> np.ndarray:
592599
return self.data.astype(dtype, copy=False)
593600

@@ -809,8 +816,8 @@ def backward(
809816
self.args[0].backward(self.xp.flip(grad, axis=self.args[1]))
810817

811818
elif self.op == "where":
812-
self.args[0].backward(grad * self.xp.where(self.args[1].data, grad, self.xp.zeros_like(grad)))
813-
self.args[2].backward(grad * self.xp.where(self.args[1].data, self.xp.zeros_like(grad), grad))
819+
self.args[0].backward(grad * self.xp.where(self.args[1].data, grad, self.xp.zeros_like(grad)))
820+
self.args[2].backward(grad * self.xp.where(self.args[1].data, self.xp.zeros_like(grad), grad))
814821

815822
elif self.op == "neg":
816823
self.args[0].backward(-grad)

neunet/nn/layers/dropout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def forward(self, X: Tensor) -> Tensor:
2323

2424
if self.training:
2525
self.mask = (
26-
X.xp.random.binomial(1, 1 - self.p, size=X.data.shape, dtype=X.data.dtype)
26+
X.xp.random.binomial(1, 1 - self.p, size=X.data.shape).astype(X.data.dtype)
2727
* self.scale
2828
)
2929
else:

0 commit comments

Comments
 (0)