mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-08-30 14:06:07 +00:00
fix (some) requires_grad [pr] (#8342)
This commit is contained in:
+12
-9
@@ -171,6 +171,10 @@ class Tensor(SimpleMathTrait):
|
||||
assert data.device == device, f"MultiLazyBuffer device mismatch, {data.device} != {device}"
|
||||
self.lazydata = data
|
||||
|
||||
def requires_grad_(self, requires_grad=True) -> Tensor:
|
||||
self.requires_grad = requires_grad
|
||||
return self
|
||||
|
||||
class train(ContextDecorator):
|
||||
def __init__(self, mode:bool = True): self.mode = mode
|
||||
def __enter__(self): self.prev, Tensor.training = Tensor.training, self.mode
|
||||
@@ -719,7 +723,7 @@ class Tensor(SimpleMathTrait):
|
||||
# ***** rng hlops *****
|
||||
|
||||
@staticmethod
|
||||
def randn(*shape, dtype:Optional[DTypeLike]=None, **kwargs) -> Tensor:
|
||||
def randn(*shape, dtype:Optional[DTypeLike]=None, requires_grad:Optional[bool]=None, **kwargs) -> Tensor:
|
||||
"""
|
||||
Creates a tensor with the given shape, filled with random values from a normal distribution with mean `0` and standard deviation `1`.
|
||||
If `dtype` is not specified, the default type is used.
|
||||
@@ -734,10 +738,10 @@ class Tensor(SimpleMathTrait):
|
||||
"""
|
||||
# https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
|
||||
src = Tensor.rand((2, *argfix(*shape)), **{**kwargs, "dtype": dtypes.float32})
|
||||
return src[0].mul(2*math.pi).cos().mul((1 - src[1]).log().mul(-2).sqrt()).cast(dtype or dtypes.default_float)
|
||||
return (src[0].mul(2*math.pi).cos().mul((1 - src[1]).log().mul(-2).sqrt()).cast(dtype or dtypes.default_float)).requires_grad_(requires_grad)
|
||||
|
||||
@staticmethod
|
||||
def randint(*shape, low=0, high=10, **kwargs) -> Tensor:
|
||||
def randint(*shape, low=0, high=10, dtype=dtypes.int32, **kwargs) -> Tensor:
|
||||
"""
|
||||
Creates a tensor with the given shape, filled with random integer values generated uniformly from the interval `[low, high)`.
|
||||
If `dtype` is not specified, the default type is used.
|
||||
@@ -751,12 +755,12 @@ class Tensor(SimpleMathTrait):
|
||||
```
|
||||
"""
|
||||
if not isinstance(low, int) or not isinstance(high, int): raise TypeError(f"{low=} and {high=} must be integers")
|
||||
dtype = to_dtype(kwargs.pop("dtype", dtypes.int32))
|
||||
dtype = to_dtype(dtype)
|
||||
if not dtypes.is_int(dtype): raise TypeError(f"{dtype=} must be int")
|
||||
return Tensor.uniform(*shape, low=low, high=high, dtype=dtype, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
def normal(*shape, mean=0.0, std=1.0, **kwargs) -> Tensor:
|
||||
def normal(*shape, mean=0.0, std=1.0, requires_grad:Optional[bool]=None, **kwargs) -> Tensor:
|
||||
"""
|
||||
Creates a tensor with the given shape, filled with random values from a normal distribution with the given `mean` and standard deviation `std`.
|
||||
|
||||
@@ -768,10 +772,10 @@ class Tensor(SimpleMathTrait):
|
||||
print(Tensor.normal(2, 3, mean=10, std=2).numpy())
|
||||
```
|
||||
"""
|
||||
return (std * Tensor.randn(*shape, **kwargs)) + mean
|
||||
return ((std * Tensor.randn(*shape, **kwargs)) + mean).requires_grad_(requires_grad)
|
||||
|
||||
@staticmethod
|
||||
def uniform(*shape, low=0.0, high=1.0, **kwargs) -> Tensor:
|
||||
def uniform(*shape, low=0.0, high=1.0, dtype:Optional[DTypeLike]=None, requires_grad:Optional[bool]=None, **kwargs) -> Tensor:
|
||||
"""
|
||||
Creates a tensor with the given shape, filled with random values from a uniform distribution over the interval `[low, high)`.
|
||||
|
||||
@@ -783,8 +787,7 @@ class Tensor(SimpleMathTrait):
|
||||
print(Tensor.uniform(2, 3, low=2, high=10).numpy())
|
||||
```
|
||||
"""
|
||||
dtype = kwargs.pop("dtype", dtypes.default_float)
|
||||
return ((high-low) * Tensor.rand(*shape, **kwargs)).cast(dtype) + low
|
||||
return (((high-low) * Tensor.rand(*shape, **kwargs)).cast(dtype or dtypes.default_float) + low).requires_grad_(requires_grad)
|
||||
|
||||
@staticmethod
|
||||
def scaled_uniform(*shape, **kwargs) -> Tensor:
|
||||
|
||||
Reference in New Issue
Block a user