def test_build_fc_layers(self, is_training): batch_size = 5 num_units_fc_layers = [128, 64, 32] images = _construct_images(batch_size) image_shape = tuple(images.get_shape().as_list()[1:]) net_input = tf.layers.flatten(images) number_params = 0 num_units_prev = np.prod(image_shape) for num_units in num_units_fc_layers: filter_size = (num_units_prev, num_units) # Add filter parameters count. number_params += np.prod(filter_size) output_size = num_units num_units_prev = num_units # Add batch norm mean and variance parameters count. number_params += num_units * 4 final_shape = (batch_size, output_size) net, _ = model_utils.build_fc_layers( net_input, num_units_fc_layers=num_units_fc_layers, activation=tf.nn.relu, batch_norm=True, regularizer=tf.nn.l2_loss, is_training=is_training) vars_list = tf.global_variables() self.assertEqual(_count_parameters(vars_list), number_params) init = tf.global_variables_initializer() self.evaluate(init) self.assertEqual(final_shape, self.evaluate(net).shape)
def __call__(self, input_state): """Builds classification network. Args: input_state: 2-D Tensor of shape [batch, state dimensionality] Returns: logits: Network logits. endpoints: Dictionary with activations at different layers. """ if self.var_list: reuse = True else: reuse = False tf.logging.info("Builds Classification Network") endpoints = {} net = input_state # Fully connected layers. with tf.variable_scope("classification_network", reuse=reuse): net, endpoints_ = model_utils.build_fc_layers( net, self.num_units_fc_layers, activation=self.activation, regularizer=self.regularizer) endpoints.update(endpoints_) # Linear output layer. with tf.variable_scope("classification_network/output", reuse=reuse): logits, _ = model_utils.build_fc_layers( net, [self.num_classes], activation=None, regularizer=self.regularizer) endpoints["logits"] = logits if not reuse: self.collect_variables() return logits, endpoints
def __call__(self, input_state, location_scale, prev_locations=None, is_training=False, policy="learned", sampling_stddev=1e-5): """Builds emission network. Args: input_state: 2-D Tensor of shape [batch, state dimensionality] location_scale: <= 1. and >= 0. the normalized location range [-location_scale, location_scale] prev_locations: if not None add prev_location to current proposed location (ie using relative locations) is_training: (Boolean) to indicate training or inference modes. policy: (String) 'learned': uses learned policy, 'random': uses random policy, or 'center': uses center look policy. sampling_stddev: Sampling distribution standard deviation. Returns: locations: network output reflecting next location to look at (normalized to range [-location_scale, location_scale]). The image locations mapping to locs are as follows: (-1, -1): upper left corner. (-1, 1): upper right corner. (1, 1): lower right corner. (1, -1): lower left corner. endpoints: dictionary with activations at different layers. """ if self.var_list: reuse = True else: reuse = False batch_size = input_state.shape.as_list()[0] tf.logging.info("BUILD Emission Network") endpoints = {} net = input_state # Fully connected layers. with tf.variable_scope("emission_network", reuse=reuse): net, endpoints_ = model_utils.build_fc_layers( net, self.num_units_fc_layers, activation=self.activation, regularizer=self.regularizer) endpoints.update(endpoints_) # Tanh output layer. with tf.variable_scope("emission_network/output", reuse=reuse): output, _ = model_utils.build_fc_layers( net, [self.location_dims], activation=tf.nn.tanh, regularizer=self.regularizer) # scale location ([-location_scale, location_scale] range mean_locations = location_scale * output if prev_locations is not None: mean_locations = prev_locations + mean_locations if policy == "learned": endpoints["mean_locations"] = mean_locations if is_training: # At training samples random location. locations = mean_locations + tf.random_normal( shape=(batch_size, self.location_dims), stddev=sampling_stddev) # Ensures range [-location_scale, location_scale] locations = tf.clip_by_value(locations, -location_scale, location_scale) tf.logging.info("Sampling locations.") tf.logging.info( "====================================================") else: # At inference uses the mean value for the location. locations = mean_locations locations = tf.stop_gradient(locations) elif policy == "random": # Use random policy for location. locations = tf.random_uniform(shape=(batch_size, self.location_dims), minval=-location_scale, maxval=location_scale) endpoints["mean_locations"] = mean_locations elif policy == "center": # Use center look policy. locations = tf.zeros(shape=(batch_size, self.location_dims)) endpoints["mean_locations"] = mean_locations else: raise ValueError( "policy can be either 'learned', 'random', or 'center'") if not reuse: self.collect_variables() return locations, endpoints