google-research
64 строки · 2.0 Кб
1# coding=utf-8
2# Copyright 2024 The Google Research Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Tests for weight initializers."""
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import tensorflow.compat.v1 as tf
22
23from state_of_sparsity.sparse_transformer.layers import common_init
24
25
26class SparseGlorotUniformTest(tf.test.TestCase):
27
28def testSparseGlorotUniform_OutputShape(self):
29initializer = common_init.SparseGlorotUniform(.5)
30x = tf.get_variable(
31"x",
32shape=[512, 1024],
33initializer=initializer,
34dtype=tf.float32)
35with self.test_session() as sess:
36sess.run(tf.global_variables_initializer())
37res = sess.run(x)
38self.assertEqual(res.shape, (512, 1024))
39
40def testSparseGlorotUniform_NoSparsity(self):
41initializer = common_init.SparseGlorotUniform(0, seed=5)
42initializer_base = tf.glorot_uniform_initializer(seed=5)
43
44x = tf.get_variable(
45"x",
46shape=[512, 1024],
47initializer=initializer,
48dtype=tf.float32)
49y = tf.get_variable(
50"y",
51shape=[512, 1024],
52initializer=initializer_base,
53dtype=tf.float32)
54
55with self.test_session() as sess:
56sess.run(tf.global_variables_initializer())
57res_x = sess.run(x)
58res_y = sess.run(y)
59self.assertEqual(res_x.shape, (512, 1024))
60self.assertEqual(res_y.shape, (512, 1024))
61self.assertAllEqual(res_x, res_y)
62
63if __name__ == "__main__":
64tf.test.main()
65