Skip to content

Commit

Permalink
Constant Initializer Error fixed (#479)
Browse files Browse the repository at this point in the history
* Constant Init tests

* Comments fixed
  • Loading branch information
adi-kmt authored Jul 15, 2023
1 parent ba1d659 commit c32c3c6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions keras_core/initializers/constant_initalizers_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import numpy as np

from keras_core import backend
from keras_core import initializers
from keras_core import testing


class ConstantInitializersTest(testing.TestCase):
def test_zeros_initializer(self):
shape = (3, 3)

initializer = initializers.Zeros()
values = initializer(shape=shape)
self.assertEqual(values.shape, shape)
np_values = backend.convert_to_numpy(values).data
self.assertEqual(np_values, np.zeros(shape=shape))

self.run_class_serialization_test(initializer)

def test_ones_initializer(self):
shape = (3, 3)

initializer = initializers.Ones()
values = initializer(shape=shape)
self.assertEqual(values.shape, shape)
np_values = backend.convert_to_numpy(values).data
self.assertEqual(np_values, np.ones(shape=shape))

self.run_class_serialization_test(initializer)

def test_constant_initializer(self):
shape = (3, 3)
constant_value = 6.0

initializer = initializers.Constant(value=constant_value)
values = initializer(shape=shape)
self.assertEqual(values.shape, shape)
np_values = backend.convert_to_numpy(values).data
self.assertEqual(
np_values, np.full(shape=shape, fill_value=constant_value)
)

self.run_class_serialization_test(initializer)
1 change: 1 addition & 0 deletions keras_core/initializers/constant_initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, value=0.0):
self.value = float(value)

def __call__(self, shape, dtype=None):
dtype = standardize_dtype(dtype)
return self.value * ops.ones(shape=shape, dtype=dtype)

def get_config(self):
Expand Down

0 comments on commit c32c3c6

Please sign in to comment.