Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Sample python bilinear initializer at integral points in y-direction #12983

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions python/mxnet/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def _init_bilinear(self, _, arr):
c = (2 * f - 1 - f % 2) / (2. * f)
for i in range(np.prod(shape)):
x = i % shape[3]
y = (i / shape[3]) % shape[2]
y = (i // shape[3]) % shape[2]
weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
arr[:] = weight.reshape(shape)

Expand Down Expand Up @@ -656,7 +656,7 @@ def _init_weight(self, _, arr):
c = (2 * f - 1 - f % 2) / (2. * f)
for i in range(np.prod(shape)):
x = i % shape[3]
y = (i / shape[3]) % shape[2]
y = (i // shape[3]) % shape[2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a _init_bilinear method in the base Initializer class - /~https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/initializer.py#L212 . Is this class redundant?

Also I see that many of the initializers do not have tests. Could you please add a test for this change by creating a new file test_initializer.py in the tests folder. Tests for other initializers can be added to the same file later.

weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
arr[:] = weight.reshape(shape)

Expand Down
9 changes: 9 additions & 0 deletions tests/python/unittest/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,17 @@ def check_rsp_const_init(init, val):
check_rsp_const_init(mx.initializer.Zero(), 0.)
check_rsp_const_init(mx.initializer.One(), 1.)

def test_bilinear_init():
bili = mx.init.Bilinear()
bili_weight = mx.ndarray.empty((1,1,4,4))
bili._init_weight(None, bili_weight)
bili_1d = np.array([[1/float(4), 3/float(4), 3/float(4), 1/float(4)]])
bili_2d = bili_1d * np.transpose(bili_1d)
assert (bili_2d == bili_weight.asnumpy()).all()

if __name__ == '__main__':
test_variable_init()
test_default_init()
test_aux_init()
test_rsp_const_init()
test_bilinear_init()