Ejemplo n.º 1
0
    def test_fc_with_dropout(self):
        with tf.Graph().as_default(), tf.Session() as sess:
            tf.set_random_seed(10)
            x_ = tf.ones(shape=[3, 24], dtype=tf.float32)
            with tf.variable_scope("Hidden"):
                h_ = models.fully_connected_layers(x_, [100],
                                                   dropout_rate=0.5,
                                                   is_training=False)
            h_nz_ = tf.reduce_sum(tf.cast(tf.equal(h_, 0), dtype=tf.int32))
            with tf.variable_scope("Hidden", reuse=True):
                h_train_ = models.fully_connected_layers(x_, [100],
                                                         dropout_rate=0.5,
                                                         is_training=True)
            h_train_nz_ = tf.reduce_sum(
                tf.cast(tf.equal(h_train_, 0), dtype=tf.int32))
            self.assertEqual(h_.get_shape().as_list(), [3, 100])
            self.assertEqual(h_train_.get_shape().as_list(), [3, 100])

            sess.run(tf.global_variables_initializer())
            h_nz, h_train_nz = sess.run([h_nz_, h_train_nz_])
            self.assertEqual(h_nz, 0)
            # Check that with dropout, number of zeros is close to expected
            expected_num_zero = 0.5 * np.prod(h_.get_shape().as_list())
            self.assertLessEqual(h_train_nz, expected_num_zero * 1.1)
            self.assertGreaterEqual(h_train_nz, expected_num_zero * 0.9)
Ejemplo n.º 2
0
    def _build_network(self, dim_a, params):
        with tf.variable_scope('base'):
            # Initialize graph
            if params['train_ae']:
                ae_trainer = TrainAE()
                ae_trainer.run(train=True, show_performance=True)

            self.AE = AE(ae_loc=params['ae_loc'])
            ae_encoder = self.AE.latent_space
            self.low_dim_input_shape = ae_encoder.get_shape()[1:]
            self.low_dim_input = tf.placeholder(tf.float32, [
                None, self.low_dim_input_shape[0], self.low_dim_input_shape[1],
                self.low_dim_input_shape[2]
            ],
                                                name='input')

            self.low_dim_input = tf.identity(self.low_dim_input,
                                             name='low_dim_input')

            # Build fully connected layers
            self.y, loss = fully_connected_layers(
                tf.contrib.layers.flatten(self.low_dim_input), dim_a,
                params['fc_layers_neurons'], params['loss_function_type'])

        variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'base')
        self.train_policy = tf.train.GradientDescentOptimizer(
            learning_rate=params['learning_rate']).minimize(loss,
                                                            var_list=variables)

        # Initialize tensorflow
        init = tf.global_variables_initializer()
        self.sess = tf.Session()
        self.sess.run(init)
        self.saver = tf.train.Saver()
    def _build_network(self, dim_a, params):
        # Initialize graph
        with tf.variable_scope('base'):
            # Build autoencoder
            ae_inputs = tf.placeholder(
                tf.float32, (None, self.image_size, self.image_size, 1),
                name='input')
            self.loss_ae, latent_space, self.ae_output = autoencoder(ae_inputs)

            # Build fully connected layers
            self.y, loss_policy = fully_connected_layers(
                tf.contrib.layers.flatten(latent_space), dim_a,
                params['fc_layers_neurons'], params['loss_function_type'])

        variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'base')
        self.train_policy = tf.train.GradientDescentOptimizer(
            learning_rate=params['learning_rate']).minimize(loss_policy,
                                                            var_list=variables)

        self.train_ae = tf.train.AdamOptimizer(
            learning_rate=params['learning_rate']).minimize(self.loss_ae)

        # Initialize tensorflow
        init = tf.global_variables_initializer()
        self.sess = tf.Session()
        self.sess.run(init)
        self.saver = tf.train.Saver()
Ejemplo n.º 4
0
    def test_fc_with_dropout(self):
        with tf.Graph().as_default(), tf.Session() as sess:
            tf.set_random_seed(10)
            x_ = tf.ones(shape=[3, 24], dtype=tf.float32)
            with tf.variable_scope("Hidden"):
                h_ = models.fully_connected_layers(x_, [100], dropout_rate=0.5,
                                                   is_training=False)
            h_nz_ = tf.reduce_sum(tf.cast(tf.equal(h_, 0),
                                          dtype=tf.int32))
            with tf.variable_scope("Hidden", reuse=True):
                h_train_ = models.fully_connected_layers(x_, [100], dropout_rate=0.5,
                                                         is_training=True)
            h_train_nz_ = tf.reduce_sum(tf.cast(tf.equal(h_train_, 0),
                                                dtype=tf.int32))
            self.assertEqual(h_.get_shape().as_list(), [3, 100])
            self.assertEqual(h_train_.get_shape().as_list(), [3, 100])

            sess.run(tf.global_variables_initializer())
            h_nz, h_train_nz = sess.run([h_nz_, h_train_nz_])
            self.assertEqual(h_nz, 0)
            # Check that with dropout, number of zeros is close to expected
            expected_num_zero = 0.5 * np.prod(h_.get_shape().as_list())
            self.assertLessEqual(h_train_nz, expected_num_zero * 1.1)
            self.assertGreaterEqual(h_train_nz, expected_num_zero * 0.9)
Ejemplo n.º 5
0
    def _build_network(self, dim_a, params):
        with tf.variable_scope('base'):
            # Input data
            x = tf.placeholder(tf.float32,
                               [None, params['low_dim_input_shape']],
                               name='input')

            # Build fully connected layers
            self.y, loss = fully_connected_layers(x, dim_a,
                                                  params['fc_layers_neurons'],
                                                  params['loss_function_type'])

        variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'base')
        self.train_step = tf.train.GradientDescentOptimizer(
            learning_rate=params['learning_rate']).minimize(loss,
                                                            var_list=variables)

        # Initialize tensorflow
        init = tf.global_variables_initializer()
        self.sess = tf.Session()
        self.sess.run(init)
        self.saver = tf.train.Saver()