Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update initializers for typing in numpy 1.21+ #516

Merged
merged 1 commit into from
Jul 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions thinc/initializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable
from typing import Callable, cast
import numpy

from .backends import Ops
Expand All @@ -16,7 +16,7 @@

def lecun_normal_init(ops: Ops, shape: Shape) -> FloatsXd:
scale = numpy.sqrt(1.0 / shape[1])
return ops.asarray_f(numpy.random.normal(0, scale, shape))
return ops.asarray_f(cast(FloatsXd, numpy.random.normal(0, scale, shape)))


@registry.initializers("lecun_normal_init.v1")
Expand All @@ -26,7 +26,7 @@ def configure_lecun_normal_init() -> Callable[[Shape], FloatsXd]:

def he_normal_init(ops: Ops, shape: Shape) -> FloatsXd:
scale = numpy.sqrt(2.0 / shape[1])
return ops.asarray_f(numpy.random.normal(0, scale, shape))
return ops.asarray_f(cast(FloatsXd, numpy.random.normal(0, scale, shape)))


@registry.initializers("he_normal_init.v1")
Expand All @@ -36,7 +36,7 @@ def configure_he_normal_init() -> Callable[[Shape], FloatsXd]:

def glorot_normal_init(ops: Ops, shape: Shape) -> FloatsXd:
scale = numpy.sqrt(2.0 / (shape[1] + shape[0]))
return ops.asarray_f(numpy.random.normal(0, scale, shape))
return ops.asarray_f(cast(FloatsXd, numpy.random.normal(0, scale, shape)))


@registry.initializers("glorot_normal_init.v1")
Expand All @@ -46,7 +46,7 @@ def configure_glorot_normal_init() -> Callable[[Shape], FloatsXd]:

def he_uniform_init(ops: Ops, shape: Shape) -> FloatsXd:
scale = numpy.sqrt(6.0 / shape[1])
return ops.asarray_f(numpy.random.uniform(-scale, scale, shape))
return ops.asarray_f(cast(FloatsXd, numpy.random.uniform(-scale, scale, shape)))


@registry.initializers("he_uniform_init.v1")
Expand All @@ -56,7 +56,7 @@ def configure_he_uniform_init() -> Callable[[Shape], FloatsXd]:

def lecun_uniform_init(ops: Ops, shape: Shape) -> FloatsXd:
scale = numpy.sqrt(3.0 / shape[1])
return ops.asarray_f(numpy.random.uniform(-scale, scale, shape))
return ops.asarray_f(cast(FloatsXd, numpy.random.uniform(-scale, scale, shape)))


@registry.initializers("lecun_uniform_init.v1")
Expand All @@ -66,7 +66,7 @@ def configure_lecun_uniform_init() -> Callable[[Shape], FloatsXd]:

def glorot_uniform_init(ops: Ops, shape: Shape) -> FloatsXd:
scale = numpy.sqrt(6.0 / (shape[0] + shape[1]))
return ops.asarray_f(numpy.random.uniform(-scale, scale, shape))
return ops.asarray_f(cast(FloatsXd, numpy.random.uniform(-scale, scale, shape)))


@registry.initializers("glorot_uniform_init.v1")
Expand All @@ -87,7 +87,7 @@ def uniform_init(
ops: Ops, shape: Shape, *, lo: float = -0.1, hi: float = 0.1
) -> FloatsXd:
values = numpy.random.uniform(lo, hi, shape)
return ops.asarray_f(values.astype("float32"))
return ops.asarray_f(cast(FloatsXd, values.astype("float32")))


@registry.initializers("uniform_init.v1")
Expand All @@ -99,7 +99,7 @@ def configure_uniform_init(

def normal_init(ops: Ops, shape: Shape, *, mean: int = 0) -> FloatsXd:
size = int(ops.xp.prod(ops.xp.asarray(shape)))
inits = numpy.random.normal(scale=mean, size=size).astype("float32")
inits = cast(FloatsXd, numpy.random.normal(scale=mean, size=size).astype("float32"))
inits = ops.reshape_f(inits, shape)
return ops.asarray_f(inits)

Expand Down