Ejemplo n.º 1
0
 def testGain(self):
   shape = (10, 10)
   for dtype in [dtypes.float32, dtypes.float64]:
     init_default = init_ops.identity_initializer(dtype=dtype)
     init_custom = init_ops.identity_initializer(gain=0.9, dtype=dtype)
     with self.test_session(graph=ops.Graph(), use_gpu=True):
       self.assertAllClose(init_default(shape).eval(), np.eye(*shape))
     with self.test_session(graph=ops.Graph(), use_gpu=True):
       self.assertAllClose(init_custom(shape).eval(), np.eye(*shape) * 0.9)
Ejemplo n.º 2
0
 def testGain(self):
   shape = (10, 10)
   for dtype in [dtypes.float32, dtypes.float64]:
     init_default = init_ops.identity_initializer(dtype=dtype)
     init_custom = init_ops.identity_initializer(gain=0.9, dtype=dtype)
     with self.test_session(graph=ops.Graph(), use_gpu=True):
       self.assertAllClose(init_default(shape).eval(), np.eye(*shape))
     with self.test_session(graph=ops.Graph(), use_gpu=True):
       self.assertAllClose(init_custom(shape).eval(), np.eye(*shape) * 0.9)
Ejemplo n.º 3
0
 def build(self, inputs_shape):
     self._kernel_w = self.add_variable(
         "{}_w".format(_WEIGHTS_VARIABLE_NAME),
         [self.output_size, self.output_size],
         dtype=self.dtype,
         initializer=init_ops.identity_initializer(gain=.05))
     self._kernel_c = self.add_variable(
         "{}_c".format(_WEIGHTS_VARIABLE_NAME),
         [inputs_shape[1], self.output_size],
         dtype=self.dtype,
         initializer=init_ops.identity_initializer(gain=.05))
     if self._use_bias:
         self._bias_c = self.add_variable(
             "{}_c".format(_BIAS_VARIABLE_NAME), [self.output_size],
             dtype=self.dtype)
Ejemplo n.º 4
0
    def test_cell_call(self, *args, **kwargs):
        """Test that the state transition works properly on hidden-to-hidden states."""
        batch_size = 2
        num_units = 3
        with self.test_session() as sess:
            cell_0layer = stlstm.STLSTMCell(num_units, st_num_layers=0)
            cell_1layer = stlstm.STLSTMCell(
                num_units,
                st_num_layers=1,
                st_kernel_initializer=init_ops.identity_initializer())

            init_state0 = cell_0layer.zero_state(batch_size,
                                                 dtype=dtypes.float32)
            init_state1 = cell_1layer.zero_state(batch_size,
                                                 dtype=dtypes.float32)

            inputs = random_ops.random_uniform([batch_size, 5])
            h0, st0 = cell_0layer(inputs, init_state0)
            h1, st1 = cell_1layer(inputs, init_state1)
            variables.global_variables_initializer().run()

            out0, newst0 = sess.run([h0, st0])
            out1, newst1 = sess.run([h1, st1])

            # NOTE: assert that a stlstm with 0 st layers output is the same as the new hidden state
            self.assertAllEqual(out0, newst0[1])
            # NOTE: assert the same for 1 layer st with an identity kernel and zero biases
            self.assertAllEqual(out1, newst1[1])
Ejemplo n.º 5
0
 def testPartitions(self):
   shape = (10, 10)
   init = init_ops.identity_initializer()
   partitioner = partitioned_variables.variable_axis_size_partitioner(1)
   with self.test_session(graph=ops.Graph(), use_gpu=True):
     with variable_scope.variable_scope(
         "foo", partitioner=partitioner, initializer=init):
       v = array_ops.identity(variable_scope.get_variable("bar", shape=shape))
     variables.global_variables_initializer().run()
     self.assertAllClose(v.eval(), np.eye(*shape))
Ejemplo n.º 6
0
 def testPartitions(self):
   shape = (10, 10)
   init = init_ops.identity_initializer()
   partitioner = partitioned_variables.variable_axis_size_partitioner(1)
   with self.test_session(graph=ops.Graph(), use_gpu=True):
     with variable_scope.variable_scope(
         "foo", partitioner=partitioner, initializer=init):
       v = array_ops.identity(variable_scope.get_variable("bar", shape=shape))
     variables.global_variables_initializer().run()
     self.assertAllClose(v.eval(), np.eye(*shape))
Ejemplo n.º 7
0
    def test_cell_residual(self, *args, **kwargs):
        num_units = 3
        # test residual and bias
        # NOTE: test that 2 layer identity kernel with ones biases, new hidden state = output + output + 2
        with self.test_session() as sess:
            cell = stlstm.STLSTMCell(
                num_units,
                st_num_layers=2,
                st_kernel_initializer=init_ops.identity_initializer(),
                st_bias_initializer=init_ops.constant_initializer(1.),
                st_residual=True)
            init_state = cell.zero_state(2, dtype=dtypes.float32)
            inputs = random_ops.random_uniform([2, 5])
            h, st = cell(inputs, init_state)
            variables.global_variables_initializer().run()

            out, newst = sess.run([h, st])

            self.assertAllClose(2. * out + 2, newst[1])
Ejemplo n.º 8
0
    def test_cell_build(self, *args, **kwargs):
        """Check that state transition variables are created properly."""
        num_units = 5
        num_layers = 2
        inputs_shape = [2, 8]
        st_kernel_initializer = init_ops.identity_initializer()

        # NOTE: test building with num_layers=0
        with self.test_session():
            cell = stlstm.STLSTMCell(num_units, st_num_layers=0)

            self.assertIsNone(cell._st_kernels)
            self.assertIsNone(cell._st_biases)

        with self.test_session():
            cell = stlstm.STLSTMCell(
                num_units,
                st_kernel_initializer=st_kernel_initializer,
                st_num_layers=num_layers)
            cell.build(inputs_shape)
            variables.global_variables_initializer().run()
            # check cell._st_kernels/biases
            # NOTE: check length of new variable arrays
            self.assertEqual(num_layers, len(cell._st_kernels))
            self.assertEqual(len(cell._st_kernels), len(cell._st_biases))

            # NOTE: check sizes of new variable arrays
            for layer in range(num_layers):
                self.assertAllEqual(cell._st_kernels[layer].shape,
                                    [num_units, num_units])
                self.assertEqual(cell._st_biases[layer].shape[0], num_units)

            # NOTE: check variables are initialied correctly
            for layer in range(num_layers):
                self.assertAllEqual(cell._st_kernels[layer].eval(),
                                    np.eye(num_units))
                self.assertAllEqual(cell._st_biases[layer].eval(),
                                    np.zeros(num_units))
Ejemplo n.º 9
0
def identity():
    return init_ops.identity_initializer()
Ejemplo n.º 10
0
 def testNonSquare(self):
   init = init_ops.identity_initializer()
   shape = (10, 5)
   with self.test_session(graph=ops.Graph(), use_gpu=True):
     self.assertAllClose(init(shape).eval(), np.eye(*shape))
Ejemplo n.º 11
0
 def testInvalidShape(self):
   init = init_ops.identity_initializer()
   with self.test_session(graph=ops.Graph(), use_gpu=True):
     self.assertRaises(ValueError, init, shape=[5, 7, 7])
     self.assertRaises(ValueError, init, shape=[5])
     self.assertRaises(ValueError, init, shape=[])
Ejemplo n.º 12
0
 def testNonSquare(self):
     init = init_ops.identity_initializer()
     shape = (10, 5)
     with self.test_session(graph=ops.Graph(), use_gpu=True):
         self.assertAllClose(init(shape).eval(), np.eye(*shape))
Ejemplo n.º 13
0
 def testInvalidShape(self):
     init = init_ops.identity_initializer()
     with self.test_session(graph=ops.Graph(), use_gpu=True):
         self.assertRaises(ValueError, init, shape=[5, 7, 7])
         self.assertRaises(ValueError, init, shape=[5])
         self.assertRaises(ValueError, init, shape=[])