Exemplo n.º 1
0
def model_fn(x, target, mode, params):
    """Model function for Estimator."""

    y_ = tf.cast(target, tf.float32)

    x_image = tf.reshape(x, [-1, 28, 28, 1])

    # first convolutional layer
    h_conv1 = layers.convolution2d(x_image, 32, [5,5])
    h_pool1 = layers.max_pool2d(h_conv1, [2,2])

    # second convolutional layer
    h_conv2 = layers.convolution2d(h_pool1, 64, [5,5])
    h_pool2 = layers.max_pool2d(h_conv2, [2,2])

    # densely connected layer
    h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
    h_fc1 = layers.fully_connected(h_pool2_flat, 1024)
    h_fc1_drop = layers.dropout(
        h_fc1, keep_prob=params["dropout"],
        is_training=(mode == ModeKeys.TRAIN))

    # readout layer
    y_conv = layers.fully_connected(h_fc1_drop, 10, activation_fn=None)

    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
    train_op = tf.contrib.layers.optimize_loss(
        loss=cross_entropy,
        global_step=tf.contrib.framework.get_global_step(),
        learning_rate=params["learning_rate"],
        optimizer="Adam")

    predictions = tf.argmax(y_conv, 1)
    return predictions, cross_entropy, train_op
Exemplo n.º 2
0
def model(img_in, num_actions, scope, noisy=False, reuse=False,
          concat_softmax=False):
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out, num_outputs=32, kernel_size=8,
                                       stride=4, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=4,
                                       stride=2, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=3,
                                       stride=1, activation_fn=tf.nn.relu)
        out = layers.flatten(out)

        with tf.variable_scope("action_value"):
            if noisy:
                # Apply noisy network on fully connected layers
                # ref: https://arxiv.org/abs/1706.10295
                out = noisy_dense(out, name='noisy_fc1', size=512,
                                  activation_fn=tf.nn.relu)
                out = noisy_dense(out, name='noisy_fc2', size=num_actions)
            else:
                out = layers.fully_connected(out, num_outputs=512,
                                             activation_fn=tf.nn.relu)
                out = layers.fully_connected(out, num_outputs=num_actions,
                                             activation_fn=None)
            # V: Softmax - inspired by deep-rl-attack #
            if concat_softmax:
                out = tf.nn.softmax(out)
        return out
Exemplo n.º 3
0
def dueling_model(img_in, num_actions, scope, reuse=False, layer_norm=False):
    """As described in https://arxiv.org/abs/1511.06581"""
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
        conv_out = layers.flatten(out)

        with tf.variable_scope("state_value"):
            state_hidden = layers.fully_connected(conv_out, num_outputs=512, activation_fn=None)
            if layer_norm:
                state_hidden = layer_norm_fn(state_hidden, relu=True)
            else:
                state_hidden = tf.nn.relu(state_hidden)
            state_score = layers.fully_connected(state_hidden, num_outputs=1, activation_fn=None)
        with tf.variable_scope("action_value"):
            actions_hidden = layers.fully_connected(conv_out, num_outputs=512, activation_fn=None)
            if layer_norm:
                actions_hidden = layer_norm_fn(actions_hidden, relu=True)
            else:
                actions_hidden = tf.nn.relu(actions_hidden)
            action_scores = layers.fully_connected(actions_hidden, num_outputs=num_actions, activation_fn=None)
            action_scores_mean = tf.reduce_mean(action_scores, 1)
            action_scores = action_scores - tf.expand_dims(action_scores_mean, 1)
        return state_score + action_scores
Exemplo n.º 4
0
def dueling_model(img_in, num_actions, scope, noisy=False, reuse=False,
                  concat_softmax=False):
    """As described in https://arxiv.org/abs/1511.06581"""
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out, num_outputs=32, kernel_size=8,
                                       stride=4, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=4,
                                       stride=2, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=3,
                                       stride=1, activation_fn=tf.nn.relu)
        out = layers.flatten(out)

        with tf.variable_scope("state_value"):
            if noisy:
                # Apply noisy network on fully connected layers
                # ref: https://arxiv.org/abs/1706.10295
                state_hidden = noisy_dense(out, name='noisy_fc1', size=512,
                                           activation_fn=tf.nn.relu)
                state_score = noisy_dense(state_hidden, name='noisy_fc2',
                                          size=1)
            else:
                state_hidden = layers.fully_connected(
                    out,
                    num_outputs=512,
                    activation_fn=tf.nn.relu
                )
                state_score = layers.fully_connected(state_hidden,
                                                     num_outputs=1,
                                                     activation_fn=None)
        with tf.variable_scope("action_value"):
            if noisy:
                # Apply noisy network on fully connected layers
                # ref: https://arxiv.org/abs/1706.10295
                actions_hidden = noisy_dense(out, name='noisy_fc1', size=512,
                                             activation_fn=tf.nn.relu)
                action_scores = noisy_dense(actions_hidden, name='noisy_fc2',
                                            size=num_actions)
            else:
                actions_hidden = layers.fully_connected(
                    out,
                    num_outputs=512,
                    activation_fn=tf.nn.relu
                )
                action_scores = layers.fully_connected(
                    actions_hidden,
                    num_outputs=num_actions,
                    activation_fn=None
                )
            action_scores_mean = tf.reduce_mean(action_scores, 1)
            action_scores = action_scores - tf.expand_dims(
                action_scores_mean,
                1
            )

        return state_score + action_scores
Exemplo n.º 5
0
    def create_network(self, scope):
        with tf.variable_scope(scope, reuse=False):
            state_input = tf.placeholder('float', [None, 84, 84, 4])
            out = layers.convolution2d(state_input, num_outputs=32, kernel_size=8, stride=1, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)

            conv_out = layers.flatten(out)
            value_out = layers.fully_connected(conv_out, num_outputs=256, activation_fn=tf.nn.relu)
            q_value = layers.fully_connected(value_out, num_outputs=self.action_dim, activation_fn=None)
            return state_input, q_value
Exemplo n.º 6
0
def atari_model(img_in, num_actions, scope, reuse=False):
    # as described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
        out = layers.flatten(out)
        with tf.variable_scope("action_value"):
            out = layers.fully_connected(out, num_outputs=512,         activation_fn=tf.nn.relu)
            out = layers.fully_connected(out, num_outputs=num_actions, activation_fn=None)

        return out
Exemplo n.º 7
0
def _cnn_to_mlp(convs, hiddens, dueling, inpt, num_actions, scope, reuse=False, layer_norm=False):
    with tf.variable_scope(scope, reuse=reuse):
        out = inpt
        with tf.variable_scope("convnet"):
            for num_outputs, kernel_size, stride in convs:
                out = layers.convolution2d(out,
                                           num_outputs=num_outputs,
                                           kernel_size=kernel_size,
                                           stride=stride,
                                           activation_fn=tf.nn.relu)
        conv_out = layers.flatten(out)
        with tf.variable_scope("action_value"):
            action_out = conv_out
            for hidden in hiddens:
                action_out = layers.fully_connected(action_out, num_outputs=hidden, activation_fn=None)
                if layer_norm:
                    action_out = layers.layer_norm(action_out, center=True, scale=True)
                action_out = tf.nn.relu(action_out)
            action_scores = layers.fully_connected(action_out, num_outputs=num_actions, activation_fn=None)

        if dueling:
            with tf.variable_scope("state_value"):
                state_out = conv_out
                for hidden in hiddens:
                    state_out = layers.fully_connected(state_out, num_outputs=hidden, activation_fn=None)
                    if layer_norm:
                        state_out = layers.layer_norm(state_out, center=True, scale=True)
                    state_out = tf.nn.relu(state_out)
                state_score = layers.fully_connected(state_out, num_outputs=1, activation_fn=None)
            action_scores_mean = tf.reduce_mean(action_scores, 1)
            action_scores_centered = action_scores - tf.expand_dims(action_scores_mean, 1)
            q_out = state_score + action_scores_centered
        else:
            q_out = action_scores
        return q_out
Exemplo n.º 8
0
    def terra_module(self, variable_scope, input_tensor, convolution_output_depth, kernel_size=3, dropout_on=False,
                     normalization_function=default_normalization, activation_function=default_activation,
                     strided_max_pool_on=False):
        """
        A basic square 2D convolution layer followed by optional batch norm and dropout.

        :param variable_scope: What to name the module scope in the graph.
        :type variable_scope: str
        :param input_tensor: The input tensor to work on.
        :type input_tensor: tf.Tensor
        :param convolution_output_depth: The output depth of the convolution.
        :type convolution_output_depth: int
        :param kernel_size: The size of the square convolutional kernel.
        :type kernel_size: int
        :param dropout_on: A boolean to choose whether or not dropout should be applied.
        :type dropout_on: bool
        :param normalization_function: A normalization to be applied before activations. Defaults to batch_norm.
        :type normalization_function: tf.Tensor -> tf.Tensor
        :param activation_function: The activation function to be applied.
        :type activation_function: tf.Tensor -> tf.Tensor\
        :param strided_max_pool_on: Whether to include a strided max pool at the end of the module.
        :type strided_max_pool_on: bool
        :return: The output activation tensor.
        :rtype: tf.Tensor
        """
        with tf.variable_scope(variable_scope):
            output_tensor = convolution2d(input_tensor, convolution_output_depth, [kernel_size, kernel_size],
                                          activation_fn=activation_function, normalizer_fn=normalization_function,
                                          padding='same')
            output_tensor = self.general_module_end_operations(output_tensor, dropout_on, strided_max_pool_on)
            return output_tensor
Exemplo n.º 9
0
    def mercury_module(self, variable_scope, input_tensor, aisle_convolution_depth, spatial_convolution_depth,
                       max_pool_depth, dropout_on=False, normalization_function=default_normalization,
                       activation_function=default_activation, strided_max_pool_on=False):
        """
        This module has 4 parts. A simple 1x1 dimensionality shift (the aisle convolution), a 1x3 convolution, a 3x1
        convolution, and a 2x2 max pooling with dimensionality shift. All have stride of 1. The outputs of each part are
        concatenated to form an output tensor.

        :param variable_scope: What to name the module scope in the graph.
        :type variable_scope: str
        :param input_tensor: The input tensor to work on.
        :type input_tensor: tf.Tensor
        :param aisle_convolution_depth: The output depth of the 1x1 convolution.
        :type aisle_convolution_depth: int
        :param spatial_convolution_depth: The output depth of the 1x3 and 3x1 convolutions (each).
        :type spatial_convolution_depth: int
        :param max_pool_depth: The output depth of the (dimensional shifted) max pool.
        :type max_pool_depth: int
        :param dropout_on: A boolean to choose whether or not dropout should be applied.
        :type dropout_on: bool
        :param normalization_function: A normalization to be applied before activations. Defaults to batch_norm.
        :type normalization_function: tf.Tensor -> tf.Tensor
        :param activation_function: The activation function to be applied.
        :type activation_function: tf.Tensor -> tf.Tensor
        :param strided_max_pool_on: Whether to include a strided max pool at the end of the module.
        :type strided_max_pool_on: bool
        :return: The output activation tensor.
        :rtype: tf.Tensor
        """
        with tf.variable_scope(variable_scope):
            part1 = convolution2d(input_tensor, aisle_convolution_depth, [1, 1], activation_fn=activation_function,
                                  normalizer_fn=normalization_function)
            part2 = convolution2d(input_tensor, spatial_convolution_depth, [3, 1], activation_fn=activation_function,
                                  normalizer_fn=normalization_function)
            part3 = convolution2d(input_tensor, spatial_convolution_depth, [1, 3], activation_fn=activation_function,
                                  normalizer_fn=normalization_function)
            max_pool_output = max_pool2d(input_tensor, kernel_size=2, stride=1, padding='SAME')
            part4 = convolution2d(max_pool_output, max_pool_depth, [1, 1], activation_fn=activation_function,
                                  normalizer_fn=normalization_function)
            output_tensor = tf.concat(axis=3, values=[part1, part2, part3, part4])
            output_tensor = self.general_module_end_operations(output_tensor, dropout_on, strided_max_pool_on)
            return output_tensor
Exemplo n.º 10
0
def conv2d(tensor_in,
           n_filters,
           filter_shape,
           strides=None,
           padding="SAME",
           bias=True,
           activation=None,
           batch_norm=False):
  """Creates 2D convolutional subgraph with bank of filters.

  This is deprecated. Please use contrib.layers.convolution2d.

  Uses tf.nn.conv2d under the hood.
  Creates a filter bank:
    [filter_shape[0], filter_shape[1], tensor_in[3], n_filters]
  and applies it to the input tensor.

  Args:
    tensor_in: input Tensor, 4D shape:
      [batch, in_height, in_width, in_depth].
    n_filters: number of filters in the bank.
    filter_shape: Shape of filters, a list of ints, 1-D of length 2.
    strides: A list of ints, 1-D of length 4. The stride of the sliding
      window for each dimension of input.
    padding: A string: 'SAME' or 'VALID'. The type of padding algorthim to use.
      See the [comment here]
      (https://www.tensorflow.org/api_docs/python/nn.html#convolution)
    bias: Boolean, if to add bias.
    activation: Activation Op, optional. If provided applied on the output.
    batch_norm: Whether to apply batch normalization.

  Returns:
    A Tensor with resulting convolution.
  """
  if batch_norm:
    logging.warn("batch_norm will not work with learn.ops.conv2d, "
                 "use tf.contrib.layers.conv2d.")

  if bias:
    bias_init = init_ops.zeros_initializer
  if strides is None:
    strides = [1, 1]
  else:
    strides = strides[1:3]  # only take height and width
    logging.warn("strides may not be passed correctly. Please instead "
                 "use and see documentation for contrib.layers.convolution2d.")
  return convolution2d(
      tensor_in,
      num_outputs=n_filters,
      kernel_size=list(filter_shape),
      stride=strides,
      padding=padding,
      biases_initializer=bias_init,
      activation_fn=activation)
Exemplo n.º 11
0
def make_convs(inpt, convs, padding, scope='convnet', reuse=None):
    out = inpt
    with tf.variable_scope(scope, reuse=reuse):
        for num_outputs, kernel_size, stride in convs:
            out = layers.convolution2d(
                out,
                num_outputs=num_outputs,
                kernel_size=kernel_size,
                stride=stride,
                padding=padding,
                activation_fn=tf.nn.elu
            )
    return out
Exemplo n.º 12
0
def dqn_model(X, S, s_dim, a_dim, k, skip=False):
    print "DQN model"
    S = tf.cast(S, dtype=tf.float32)
    state = tf.concat([X, S], axis=3)
    ch_h = 16
    # store Q(s,a) value
    q_a = tf.Variable(0, dtype=tf.float32, name="q_a", trainable=False)
    # original architecture
    out = layers.convolution2d(state,
                               num_outputs=32,
                               kernel_size=3,
                               stride=1,
                               activation_fn=tf.nn.relu)
    out = layers.convolution2d(out,
                               num_outputs=64,
                               kernel_size=3,
                               stride=1,
                               activation_fn=tf.nn.relu)
    out = layers.convolution2d(out,
                               num_outputs=64,
                               kernel_size=3,
                               stride=1,
                               activation_fn=tf.nn.relu)

    out = layers.flatten(out)

    out = layers.fully_connected(out, num_outputs=64, activation_fn=tf.nn.relu)
    out = layers.fully_connected(out, num_outputs=a_dim, activation_fn=None)

    q_a += out

    # pi mapping from q to action
    pi = layers.fully_connected(q_a, num_outputs=16, activation_fn=None)
    pi_logits = layers.fully_connected(pi,
                                       num_outputs=a_dim,
                                       activation_fn=None)
    pi_action = tf.nn.softmax(pi_logits, name="pi_action")

    return pi_logits, pi_action
Exemplo n.º 13
0
def q_network(X_state, scope):
    prev_layer = X_state
    conv_layers = []
    with tf.variable_scope(scope) as scope:
	for n_maps, kernel_size, stride, padding, activation in zip(
	conv_n_maps, conv_kernel_sizes, conv_strides,
	conv_paddings, conv_activation):
	prev_layer = convolution2d(
	prev_layer, num_outputs=n_maps, kernel_size=kernel_size,
	stride=stride, padding=padding, activation_fn=activation,
	weights_initializer=initializer)
	conv_layers.append(prev_layer)
    last_conv_layer_flat = tf.reshape(prev_layer, shape=[-1, n_hidden_in])
    hidden = fully_connected(
        last_conv_layer_flat, n_hidden, activation_fn=hidden_activation,
        weights_initializer=initializer)
    outputs = fully_connected(
        hidden, n_outputs, activation_fn=None,
        weights_initializer=initializer)
    trainable_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       scope=scope.name)
    trainable_vars_by_name = {var.name[len(scope.name):]: var
                              for var in trainable_vars}
    return outputs, trainable_vars_by_name

X_state = tf.placeholder(tf.float32, shape=[None, input_height, input_width, input_channels])
actor_q_values, actor_vars = q_network(X_state, scope="q_networks/actor")
critic_q_values, critic_vars = q_network(X_state, scope="q_networks/critic")
copy_ops = [actor_var.assign(critic_vars[var_name]) for var_name, actor_var in actor_vars.items()]
copy_critic_to_actor = tf.group(*copy_ops)

X_action = tf.placeholder(tf.int32, shape=[None])
q_value = tf.reduce_sum(critic_q_values * tf.one_hot(X_action, n_outputs), axis=1, keep_dims=True)

y = tf.placeholder(tf.float32, shape=[None, 1])
cost = tf.reduce_mean(tf.square(y - q_value))
global_step = tf.Variable(0, trainable=False, name='global_step')
optimizer = tf.train.AdamOptimizer(learning_rate)
training_op = optimizer.minimize(cost, global_step=global_step)
init = tf.global_variables_initializer()
saver = tf.train.Saver()

def sample_memories(batch_size):
    indices = rnd.permutation(len(replay_memory))[:batch_size]
    cols = [[], [], [], [], []] # state, action, reward, next_state, continue
	for idx in indices:
	    memory = replay_memory[idx]
	    for col, value in zip(cols, memory):
		col.append(value)
    cols = [np.array(col) for col in cols]
    return (cols[0], cols[1], cols[2].reshape(-1, 1), cols[3], cols[4].reshape(-1, 1))
Exemplo n.º 14
0
def _cnn_to_mlp(convs,
                hiddens,
                dueling,
                inpt,
                num_actions,
                scope,
                reuse=False,
                layer_norm=False):
    with tf.variable_scope(scope, reuse=reuse):
        out = inpt
        with tf.variable_scope("convnet"):
            for num_outputs, kernel_size, stride in convs:
                out = layers.convolution2d(
                    out,
                    num_outputs=num_outputs,
                    kernel_size=kernel_size,
                    stride=stride,
                    activation_fn=tf.nn.relu)
        conv_out = layers.flatten(out)
        with tf.variable_scope("action_value"):
            action_out = conv_out
            for hidden in hiddens:
                action_out = layers.fully_connected(
                    action_out, num_outputs=hidden, activation_fn=None)

                if layer_norm:
                    action_out = layers.layer_norm(
                        action_out, center=True, scale=True)
                action_out = tf.nn.relu(action_out)
            action_scores = layers.fully_connected(
                action_out, num_outputs=num_actions, activation_fn=None)

        if dueling:
            with tf.variable_scope("state_value"):
                state_out = conv_out
                for hidden in hiddens:
                    state_out = layers.fully_connected(
                        state_out, num_outputs=hidden, activation_fn=None)
                    if layer_norm:
                        state_out = layers.layer_norm(
                            state_out, center=True, scale=True)
                    state_out = tf.nn.relu(state_out)
                state_score = layers.fully_connected(
                    state_out, num_outputs=1, activation_fn=None)
            action_scores_mean = tf.reduce_mean(action_scores, 1)
            action_scores_centered = action_scores - tf.expand_dims(
                action_scores_mean, 1)
            q_out = state_score + action_scores_centered
        else:
            q_out = action_scores
        return q_out
Exemplo n.º 15
0
def atari_model(img_in,
                num_actions,
                scope,
                reuse=False,
                dropout=False,
                keep_prob=1.0):
    # as described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out,
                                       num_outputs=32,
                                       kernel_size=8,
                                       stride=4,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=4,
                                       stride=2,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=3,
                                       stride=1,
                                       activation_fn=tf.nn.relu)
        out = layers.flatten(out)
        with tf.variable_scope("action_value"):
            out = layers.fully_connected(out,
                                         num_outputs=512,
                                         activation_fn=tf.nn.relu)
            if dropout:
                out = layers.dropout(out, keep_prob=keep_prob)
            out = layers.fully_connected(out,
                                         num_outputs=num_actions,
                                         activation_fn=None)

        return out
Exemplo n.º 16
0
def bootstrap_model(img_in, num_actions, scope, reuse=False, heads=10):
    """ As described in https://arxiv.org/abs/1602.04621"""
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out,
                                       num_outputs=32,
                                       kernel_size=8,
                                       stride=4,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=4,
                                       stride=2,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=3,
                                       stride=1,
                                       activation_fn=tf.nn.relu)
        out = layers.flatten(out)

        out_list = []
        with tf.variable_scope("heads"):
            for _ in range(heads):
                scope_net = "action_value_head_" + str(_)
                with tf.variable_scope(scope_net):
                    out_temp = out
                    out_temp = layers.fully_connected(out_temp,
                                                      num_outputs=512,
                                                      activation_fn=tf.nn.relu)
                    out_temp = layers.fully_connected(out_temp,
                                                      num_outputs=num_actions,
                                                      activation_fn=None)
                out_list.append(out_temp)

        return out_list
Exemplo n.º 17
0
def unit_representation_model_cnn(img_in, num_actions, scope, reuse=False):
    """As described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf"""
    img_size = img_in.shape[1]
    print("img shape", img_in.shape)
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        # coordinate = np.meshgrid()

        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out,
                                       num_outputs=32,
                                       kernel_size=8,
                                       stride=4,
                                       activation_fn=tf.nn.leaky_relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=4,
                                       stride=2,
                                       activation_fn=tf.nn.leaky_relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=3,
                                       stride=1,
                                       activation_fn=tf.nn.leaky_relu)
        out = layers.flatten(out)

        out = layers.fully_connected(out,
                                     num_outputs=32,
                                     activation_fn=tf.nn.leaky_relu)
        z = layers.fully_connected(out, num_outputs=32, activation_fn=None)
        normed_z = z / tf.maximum(1e-10, tf.norm(z, axis=1, keepdims=True))
        # normed_z =
        # print("normed_z:", normed_z.shape)
        # with tf.variable_scope("action_value"):
        #     v_h = layers.fully_connected(z, num_outputs=512, activation_fn=tf.nn.relu)
        #     v = layers.fully_connected(v_h, num_outputs=1, activation_fn=None)
        return normed_z
Exemplo n.º 18
0
    def nature_cnn(self, state, scope, reuse=False):
        num_actions = self.env.action_space.n
        out = state

        # compress the student network
        size1, size2, size3, size4 = (16, 16, 16,
                                      128) if self.student else (32, 64, 64,
                                                                 512)

        # Berkeley Deep RL implementation
        with tf.variable_scope(scope, reuse=reuse):
            # with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out,
                                       num_outputs=size1,
                                       kernel_size=8,
                                       stride=4,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=size2,
                                       kernel_size=4,
                                       stride=2,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=size3,
                                       kernel_size=3,
                                       stride=1,
                                       activation_fn=tf.nn.relu)
            out = layers.flatten(out)
            # with tf.variable_scope("action_value"):
            out = layers.fully_connected(out,
                                         num_outputs=size4,
                                         activation_fn=tf.nn.relu)
            out = layers.fully_connected(out,
                                         num_outputs=num_actions,
                                         activation_fn=None)

            return out
def discriminator(input_img, input_latent):
    # input_img = Input(batch_shape=(None, 3, 32, 32), dtype=im_dtype)
    with tf.variable_scope('Net_Dis') as scope:
        # pdb.set_trace()
        input_latent_d = tf.tile(tf.reshape(input_latent, [-1,1,1,tf.shape(input_latent)[-1]]), [1,tf.shape(input_img)[1],tf.shape(input_img)[2],1]  )
        xx = tf.concat( [input_latent_d, input_img], axis=3)
        xx = tf.nn.dropout(input_img, 0.8)
        xx = layers.convolution2d(xx, 96, kernel_size=(3,3), stride=(1, 1), padding='same', activation_fn=None)
        xx = layers.batch_norm(xx)
        xx = lrelu(xx)
        xx = layers.convolution2d(xx, 96, kernel_size=(3,3), stride=(1, 1), padding='same', activation_fn=None)
        xx = layers.batch_norm(xx)
        xx = lrelu(xx)
        xx = layers.convolution2d(xx, 96, kernel_size=(3,3), stride=(2, 2), padding='same', activation_fn=None)
        xx = layers.batch_norm(xx)
        xx = lrelu(xx)

        xx = tf.nn.dropout(xx, 0.5)
        xx = layers.convolution2d(xx, 192, kernel_size=(3,3), stride=(1, 1), padding='same', activation_fn=None)
        xx = layers.batch_norm(xx)
        xx = lrelu(xx)
        xx = layers.convolution2d(xx, 192, kernel_size=(3,3), stride=(1, 1), padding='same', activation_fn=None)
        xx = layers.batch_norm(xx)
        xx = lrelu(xx)
        xx = layers.convolution2d(xx, 192, kernel_size=(3,3), stride=(2, 2), padding='same', activation_fn=None)
        xx = layers.batch_norm(xx)
        xx = lrelu(xx)

        xx = tf.nn.dropout(xx, 0.5)
        xx = layers.convolution2d(xx, 192, kernel_size=(3,3), stride=(1, 1), padding='same', activation_fn=None)
        xx = layers.batch_norm(xx)
        xx = lrelu(xx)
        xx = layers.convolution2d(xx, 192, kernel_size=(1,1), stride=(1, 1), padding='same', activation_fn=None)
        xx = layers.batch_norm(xx)
        xx = lrelu(xx)
        xx = layers.convolution2d(xx, 192, kernel_size=(1,1), stride=(1, 1), padding='same', activation_fn=None)
        xx = layers.batch_norm(xx)
        xx = lrelu(xx)
        xx0 = tf.reduce_mean(xx, axis=[1, 2])
        xx = layers.fully_connected(xx0, label_size, activation_fn=None)
        logits = layers.batch_norm(xx)

    return  logits, xx0
Exemplo n.º 20
0
 def conv1d(self, net, num_ker, ker_size, stride):
     # 1D-convolution
     net = convolution2d(
         net,
         num_outputs=num_ker,
         kernel_size=[ker_size, 1],
         stride=[stride, 1],
         padding='SAME',
         activation_fn=None,
         normalizer_fn=None,
         weights_initializer=variance_scaling_initializer(),
         weights_regularizer=l2_regularizer(self.weight_decay),
         biases_initializer=tf.zeros_initializer)
     return net
Exemplo n.º 21
0
def make_cnn(convs, padding, inpt, initializer=None):
    if initializer is None:
        initializer = tf.orthogonal_initializer(np.sqrt(2.0))
    out = inpt
    with tf.variable_scope('convnet'):
        for num_outputs, kernel_size, stride in convs:
            out = layers.convolution2d(out,
                                       num_outputs=num_outputs,
                                       kernel_size=kernel_size,
                                       stride=stride,
                                       padding=padding,
                                       activation_fn=tf.nn.relu,
                                       weights_initializer=initializer)
    return out
Exemplo n.º 22
0
def model(img_in, num_actions, scope, noisy=False, reuse=False, concat_softmax=False):
    """As described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf"""
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(
                out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
            out = layers.convolution2d(
                out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
            out = layers.convolution2d(
                out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
        out = layers.flatten(out)

        with tf.variable_scope("action_value"):
            out = layers.fully_connected(
                out, num_outputs=512, activation_fn=tf.nn.relu)
            out = layers.fully_connected(
                out, num_outputs=num_actions, activation_fn=None)
            #V: Softmax - inspired by deep-rl-attack #
            if concat_softmax:
                out = tf.nn.softmax(out)
        return out
Exemplo n.º 23
0
def model(img_in, num_actions, scope, reuse=False, layer_norm=False, distributed=False, atoms=51):
    """As described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf"""
    print("create default model: distributed? ", distributed, "atoms", atoms)
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
            out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
        conv_out = layers.flatten(out)

        with tf.variable_scope("action_value"):
            value_out = layers.fully_connected(conv_out, num_outputs=512, activation_fn=None)
            if layer_norm:
                value_out = layer_norm_fn(value_out, relu=True)
            else:
                value_out = tf.nn.relu(value_out)
            value_out = layers.fully_connected(value_out, num_outputs=num_actions*atoms, activation_fn=None)
            if distributed:
                value_out = tf.reshape(value_out, [-1, num_actions, atoms])
        print("output shape:", tf.shape(value_out))
        return value_out
Exemplo n.º 24
0
    def _encode_conv(self, 
                        x, 
                        conv_params, 
                        scope='obs_conv_encoding', 
                        layer_norm=False, 
                        nonlinearity='swish',
                        spatial_softmax=False, 
                        reuse=False):

        with tf.variable_scope(scope, reuse=reuse):
    
            out = x
        
            for num_outputs, kernel_size, stride, padding in conv_params:
                out = layers.convolution2d( out,
                                            num_outputs=num_outputs,
                                            kernel_size=kernel_size,
                                            stride=stride,
                                            padding=padding,
                                            activation_fn=None)
                
                if layer_norm is True:
                    #out = layers.layer_norm(out, center=True, scale=True)
                    out = layers.layer_norm(out) 
                # Apply the non-linearity after layer-norm

                if nonlinearity == 'swish':
                    out = tf.nn.sigmoid(out)*out #swish non-linearity
                elif nonlinearity == 'relu':
                    out = tf.nn.relu(out)
            if spatial_softmax:
                shape = tf.shape(out)
                static_shape = out.shape
                height, width, num_channels = shape[1], shape[2], static_shape[3]
                pos_x, pos_y = tf.meshgrid(tf.linspace(-1., 1., num=height), 
                                               tf.linspace(-1., 1., num=width),
                                        indexing='ij')
                pos_x = tf.reshape(pos_x, [height*width])
                pos_y = tf.reshape(pos_y, [height*width])
                out = tf.reshape(tf.transpose(out, [0,3,1,2]), [-1, height*width])
                softmax_attention = tf.nn.softmax(out)
                expected_x = tf.reduce_sum(pos_x*softmax_attention, [1], keep_dims=True)
                expected_y = tf.reduce_sum(pos_y*softmax_attention, [1], keep_dims=True)
                expected_xy = tf.concat([expected_x, expected_y], 1)
                feature_keypoints = tf.reshape(expected_xy, [-1, num_channels.value*2])
                feature_keypoints.set_shape([None, num_channels.value*2])
                return feature_keypoints
            else:
                out = layers.flatten(out) # flatten the conv output
                return out
Exemplo n.º 25
0
def discriminator_forward(img, reuse=None, name="discriminator"):
    with tf.variable_scope(name, reuse=reuse):
        # size is 28, 28, 64
        out = layers.convolution2d(img,
                                   num_outputs=64,
                                   kernel_size=3,
                                   stride=1)
        # size is 14, 14, 128
        out = layers.convolution2d(out,
                                   num_outputs=128,
                                   kernel_size=3,
                                   stride=2)
        # size is 7, 7, 256
        out = layers.convolution2d(out,
                                   num_outputs=256,
                                   kernel_size=3,
                                   stride=2)
        # size is 12544
        out = layers.flatten(out)
        prob = layers.fully_connected(out,
                                      num_outputs=1,
                                      activation_fn=tf.nn.sigmoid)
    return prob
Exemplo n.º 26
0
    def _decode(self, feature_map, is_training):
        h = L.convolution2d_transpose(feature_map, 128, [5, 5], [2, 2], activation_fn=tf.nn.relu)
        h = L.dropout(h, keep_prob=0.5, is_training=is_training)

        h = L.convolution2d_transpose(h, 64, [5, 5], [2, 2], activation_fn=tf.nn.relu)
        h = L.dropout(h, keep_prob=0.5, is_training=is_training)

        h = L.convolution2d_transpose(h, 32, [5, 5], [2, 2], activation_fn=tf.nn.relu)
        h = L.dropout(h, keep_prob=0.5, is_training=is_training)

        h = L.convolution2d_transpose(h, 32, [5, 5], [2, 2], activation_fn=tf.nn.relu)

        h = L.convolution2d(h, len(self.classes) + 1, [1, 1], [1, 1], activation_fn=None)
        return h
Exemplo n.º 27
0
def atari_model(img_in, num_actions, scope, reuse=False):
    # as described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope("convnet"):
            # original architecture
            out = layers.convolution2d(out,
                                       num_outputs=32,
                                       kernel_size=8,
                                       stride=4,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=4,
                                       stride=2,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=3,
                                       stride=1,
                                       activation_fn=tf.nn.relu)
        out = layers.flatten(out)
        with tf.variable_scope("action_value"):
            out = layers.fully_connected(out,
                                         num_outputs=512,
                                         activation_fn=tf.nn.relu)
            out = layers.fully_connected(out,
                                         num_outputs=num_actions,
                                         activation_fn=None)
        '''with tf.variable_scope("value_stream"):
            value_out = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
            value_out = layers.fully_connected(value_out, num_outputs=1)
        with tf.variable_scope("advantage_stream"):
            ad_out = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
            ad_out = layers.fully_connected(ad_out, num_outputs=num_actions)
        out = value_out+ad_out-tf.reduce_mean(ad_out,axis=1,keep_dims=True)'''
        return out
Exemplo n.º 28
0
def dueling_model(img_in, num_actions, scope, reuse=False):
    with tf.variable_scope(scope, reuse=reuse):
        out = img_in
        with tf.variable_scope('convnet'):
            out = layers.convolution2d(out,
                                       num_outputs=32,
                                       kernel_size=8,
                                       stride=4,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=4,
                                       stride=2,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=3,
                                       stride=1,
                                       activation_fn=tf.nn.relu)
        out = layers.flatten(out)
        with tf.variable_scope('state_value'):
            v_hidden = layers.fully_connected(out,
                                              num_outputs=512,
                                              activation_fn=tf.nn.relu)
            state_val = layers.fully_connected(v_hidden,
                                               num_outputs=1,
                                               activation_fn=None)
        with tf.variable_scope('adv_value'):
            adv_hidden = layers.fully_connected(out,
                                                num_outputs=512,
                                                activation_fn=tf.nn.relu)
            adv = layers.fully_connected(adv_hidden,
                                         num_outputs=num_actions,
                                         activation_fn=None)
            adv_mean = tf.reduce_mean(adv, axis=1, keepdims=True)
            adv -= adv_mean
        return state_val + adv
def pyramid_pooling_layer(net):
    sd = net.get_shape()[1:3]
    sd1 = [sd[0].value, sd[1].value]
    sd2 = [sd[0].value // 2, sd[1].value // 2]
    sd3 = [sd1[0] // 3, sd1[1] // 3]
    sd4 = [sd1[0] // 6, sd1[1] // 6]
    upsampled_size = [
        FLAGS.img_height // FLAGS.subsample_factor,
        FLAGS.img_width // FLAGS.subsample_factor
    ]

    first = layers.avg_pool2d(net, kernel_size=sd1)
    first_conv = layers.convolution2d(first, 128, kernel_size=1)
    first_up = tf.image.resize_bilinear(
        first_conv, upsampled_size, name='spp-1')

    second = layers.max_pool2d(net, kernel_size=sd2, stride=sd2)
    second_conv = layers.convolution2d(
        second, 128, kernel_size=1, scope='spp-2')
    second_up = tf.image.resize_bilinear(
        second_conv, upsampled_size, name='spp-2')

    third = layers.max_pool2d(net, kernel_size=sd3, stride=sd3)
    third_conv = layers.convolution2d(third, 128, kernel_size=1, scope='spp-3')
    third_up = tf.image.resize_bilinear(
        third_conv, upsampled_size, name='spp-3')

    forth = layers.max_pool2d(net, kernel_size=sd4, stride=sd4)
    forth_conv = layers.convolution2d(forth, 128, kernel_size=1, scope='spp-4')
    forth_up = tf.image.resize_bilinear(
        forth_conv, upsampled_size, name='spp-4')

    stacked = tf.concat(
        3, [first_up, second_up, third_up, forth_up], name='spp')

    print('result shape', stacked.get_shape())
    return stacked
Exemplo n.º 30
0
 def __call__(self, z, reuse=True):
     with tf.variable_scope(self.name) as vs:
         if reuse:
             vs.reuse_variables()
         bs = tf.shape(z)[0]
         z = tf.reshape(z, [bs, 256, 256, 3])
         conv = tcl.convolution2d(
             z,
             32, [4, 4], [2, 2],
             weights_initializer=tf.random_normal_initializer(stddev=0.02),
             activation_fn=tf.identity)
         conv = tcl.max_pool2d(conv, [2, 2])
         #(bs, 64, 64, 32)
         conv = leaky_relu(conv)
         for _ in range(2):
             conv = tcl.convolution2d(
                 conv,
                 64, [4, 4], [2, 2],
                 weights_initializer=tf.random_normal_initializer(
                     stddev=0.02),
                 activation_fn=tf.identity)
             conv = tcl.max_pool2d(conv, [2, 2])
             #conv = tc.layers.batch_norm(conv,decay=0.9,scale=True,updates_collections=None)
             conv = leaky_relu(conv)
         #(bs, 4, 4, 64)
         #fc = tf.reshape(conv, [bs, -1])
         fc = tcl.flatten(conv)
         #(bs, 1568)
         fc = tcl.fully_connected(
             fc,
             1024,
             weights_initializer=tf.random_normal_initializer(stddev=0.02),
             activation_fn=tf.identity)
         #fc = tc.layers.batch_norm(fc,decay=0.9,scale=True,updates_collections=None)
         fc = leaky_relu(fc)
         output = tcl.fully_connected(fc, 1, activation_fn=tf.identity)
         return output
Exemplo n.º 31
0
def _cnn_to_dist_mlp(convs, hiddens, dueling, inpt, n_action, nb_atoms, scope, reuse=False, layer_norm=False):
    with tf.variable_scope(scope, reuse=reuse):
        out = inpt
        with tf.variable_scope("convnet"):
            for num_outputs, kernel_size, stride in convs:
                out = layers.convolution2d(out,
                                           num_outputs=num_outputs,
                                           kernel_size=kernel_size,
                                           stride=stride,
                                           activation_fn=tf.nn.relu)
        conv_out = layers.flatten(out)
        with tf.variable_scope("action_value"):
            action_out = conv_out
            for hidden in hiddens:
                action_out = layers.fully_connected(action_out, num_outputs=hidden, activation_fn=None)
                if layer_norm:
                    action_out = layers.layer_norm(action_out, center=True, scale=True)
                action_out = tf.nn.relu(action_out)
            action_scores = layers.fully_connected(action_out, num_outputs=n_action * 3 * nb_atoms, activation_fn=None)

        if dueling:
            raise ValueError('Dueling not supported')
            # with tf.variable_scope("state_value"):
            #     state_out = conv_out
            #     for hidden in hiddens:
            #         state_out = layers.fully_connected(state_out, num_outputs=hidden, activation_fn=None)
            #         if layer_norm:
            #             state_out = layers.layer_norm(state_out, center=True, scale=True)
            #         state_out = tf.nn.relu(state_out)
            #     state_score = layers.fully_connected(state_out, num_outputs=1, activation_fn=None)
            # action_scores_mean = tf.reduce_mean(action_scores, 1)
            # action_scores_centered = action_scores - tf.expand_dims(action_scores_mean, 1)
            # q_out = state_score + action_scores_centered
        else:
            _out = tf.reshape(action_scores, shape=[-1, n_action, 3*nb_atoms])
            out_pi_hat, out_sigma_hat, out_mu = tf.split(_out, 3, axis=-1)

            out_pi_hat = tf.exp(out_pi_hat - tf.reduce_max(out_pi_hat, -1, keepdims=True))
            nor_pi = tf.reciprocal(tf.reduce_sum(out_pi_hat, -1, keepdims=True))
            out_pi = tf.multiply(nor_pi, out_pi_hat)
            # out_sigma = 10*tf.sigmoid(out_sigma_hat) + 0.1 # + 1e-8
            out_sigma = tf.exp(out_sigma_hat) + 0.01

            #out = tf.map_fn(lambda x: tf.scalar_mul(1.,x), out) # lower sparsity
            #out = tf.map_fn(tf.contrib.sparsemax.sparsemax, out, name='sparsemax')

            #out = tf.nn.softmax(out, dim=-1, name='softmax')

        return out_pi, out_sigma, out_mu
Exemplo n.º 32
0
def _make_network(convs, fcs, use_lstm, padding, inpt, masks, rnn_state,
                  num_actions, lstm_unit, nenvs, step_size, scope):
    with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
        out = inpt
        with tf.variable_scope('convnet'):
            for num_outputs, kernel_size, stride in convs:
                out = layers.convolution2d(
                    out,
                    num_outputs=num_outputs,
                    kernel_size=kernel_size,
                    stride=stride,
                    padding=padding,
                    activation_fn=tf.nn.relu,
                    weights_initializer=tf.orthogonal_initializer(
                        np.sqrt(2.0)))
            out = layers.flatten(out)

        with tf.variable_scope('hiddens'):
            for hidden in fcs:
                out = layers.fully_connected(
                    out,
                    hidden,
                    activation_fn=tf.nn.relu,
                    weights_initializer=tf.orthogonal_initializer(
                        np.sqrt(2.0)))

        with tf.variable_scope('rnn'):
            rnn_in = batch_to_seq(out, nenvs, step_size)
            masks = batch_to_seq(masks, nenvs, step_size)
            rnn_out, rnn_state = lstm(rnn_in, masks, rnn_state, lstm_unit,
                                      np.sqrt(2.0))
            rnn_out = seq_to_batch(rnn_out, nenvs, step_size)

        if use_lstm:
            out = rnn_out

        policy = layers.fully_connected(
            out,
            num_actions,
            activation_fn=tf.nn.softmax,
            weights_initializer=tf.orthogonal_initializer(0.1))

        value = layers.fully_connected(
            out,
            1,
            activation_fn=None,
            weights_initializer=tf.orthogonal_initializer(1.0))

    return policy, value, rnn_state
Exemplo n.º 33
0
def build_model(inputs, labels, num_classes):
    weight_decay = config["weight_decay"]
    conv1sz = 16
    fc3sz = 512
    with tf.contrib.framework.arg_scope(
        [layers.convolution2d],
            kernel_size=5,
            stride=1,
            padding='SAME',
            activation_fn=tf.nn.relu,
            weights_initializer=layers.variance_scaling_initializer(),
            weights_regularizer=layers.l2_regularizer(weight_decay)):
        net = layers.convolution2d(inputs, conv1sz, scope='conv1')
        # ostatak konvolucijskih i pooling slojeva
        net = layers.max_pool2d(net, 2, 2, scope='pool1')
        net = layers.convolution2d(net, 32, scope='conv2')
        net = layers.max_pool2d(net, 2, 2, scope='pool2')

    with tf.contrib.framework.arg_scope(
        [layers.fully_connected],
            activation_fn=tf.nn.relu,
            weights_initializer=layers.variance_scaling_initializer(),
            weights_regularizer=layers.l2_regularizer(weight_decay)):
        # sada definiramo potpuno povezane slojeve
        # ali najprije prebacimo 4D tenzor u matricu
        net = layers.flatten(net)
        net = layers.fully_connected(net, fc3sz, scope='fc3')

    logits = layers.fully_connected(net,
                                    num_classes,
                                    activation_fn=None,
                                    scope='logits')
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))

    return logits, loss
Exemplo n.º 34
0
    def __call__(self, x, reuse=True):
        with tf.variable_scope(self.name) as vs:
            if reuse:
                vs.reuse_variables()
            bs = tf.shape(x)[0]
            if self.dataset == "mnist":
                x = tf.reshape(x, [bs, 28, 28, 1])
            elif self.dataset == "cifar10":
                x = tf.reshape(x, [bs, 32, 32, 3])
            conv = tcl.convolution2d(x,
                                     32, [4, 4], [2, 2],
                                     activation_fn=tf.identity)
            #(bs, 14, 14, 32)
            conv = leaky_relu(conv)
            for _ in range(self.nb_layers - 1):
                conv = tcl.convolution2d(conv,
                                         64, [4, 4], [2, 2],
                                         activation_fn=tf.identity)
                conv = tc.layers.batch_norm(conv,
                                            decay=0.9,
                                            scale=True,
                                            updates_collections=None)
                conv = leaky_relu(conv)
            #(bs, 7, 7, 32)
            #fc = tf.reshape(conv, [bs, -1])
            fc = tcl.flatten(conv)
            #(bs, 1568)
            fc = tcl.fully_connected(fc, 1024, activation_fn=tf.identity)
            fc = tc.layers.batch_norm(fc,
                                      decay=0.9,
                                      scale=True,
                                      updates_collections=None)
            fc = leaky_relu(fc)

            output = tcl.fully_connected(fc, 1, activation_fn=tf.identity)
            return output
Exemplo n.º 35
0
    def _build_net(self):
        with tf.variable_scope("conv"):
            out = layers.convolution2d(self.s,
                                       num_outputs=32,
                                       kernel_size=8,
                                       stride=4,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=4,
                                       stride=2,
                                       activation_fn=tf.nn.relu)
            out = layers.convolution2d(out,
                                       num_outputs=64,
                                       kernel_size=3,
                                       stride=1,
                                       activation_fn=tf.nn.relu)
        out = layers.flatten(out)

        with tf.variable_scope("actor"):
            a_prob = layers.fully_connected(out,
                                            num_outputs=512,
                                            activation_fn=tf.nn.relu)
            a_prob = layers.fully_connected(a_prob,
                                            num_outputs=N_A,
                                            activation_fn=tf.nn.softmax)

        with tf.variable_scope("critic"):
            v = layers.fully_connected(out,
                                       num_outputs=512,
                                       activation_fn=tf.nn.relu)
            v = layers.fully_connected(v,
                                       num_outputs=1,
                                       activation_fn=tf.nn.softmax)

        return a_prob, v
Exemplo n.º 36
0
Arquivo: ltm.py Projeto: thientu/RWMN
    def write_network(self, memory):
        print 'Write-CNN', '-'*70
        print self.write
        num_layer = len(self.write['kernel'])
        for i in range(num_layer):
            with tf.variable_scope('write-CNN-%d' % i):
                memory = layers.convolution2d(
                        inputs=memory, num_outputs=self.write['channel'][i],
                        kernel_size=[self.write['kernel'][i], self.dim_memory],
                        stride=[self.write['stride'][i], 1],
                        weights_initializer=self.initializer_conv,
                        biases_initializer=self.initializer,
                        activation_fn=tf.nn.relu)

        memory = tf.reshape(memory, [-1, self.dim_memory])
        return memory
Exemplo n.º 37
0
    def create_shallow_net_inference_op(self, images):
        """
        Performs a forward pass estimating label maps from RGB images using a (shallow) deep convolution net.

        :param images: The RGB images tensor.
        :type images: tf.Tensor
        :return: The label maps tensor.
        :rtype: tf.Tensor
        """
        module1_output = self.mercury_module('module1', images, 3, 8, 3)
        module2_output = self.mercury_module('module2', module1_output, 8, 16, 16)
        module3_output = self.mercury_module('module3', module2_output, 16, 32, 32)

        predicted_labels = convolution2d(module3_output, 1, kernel_size=1)

        return predicted_labels
Exemplo n.º 38
0
    def forward(self, feature_map, is_training):

        h = L.convolution2d(feature_map,
                            256, [7, 7], [2, 2],
                            activation_fn=tf.nn.relu)
        h = L.dropout(h, keep_prob=0.5, is_training=is_training)
        h = L.max_pool2d(h, [2, 2], [2, 2])

        batch_size, height, width, depth = h.get_shape()

        h = tf.reshape(h, tf.stack([-1, height * width * depth]))
        logits = L.fully_connected(h,
                                   len(self.classes) + 1,
                                   activation_fn=None)

        return logits
Exemplo n.º 39
0
def _make_convolutional_layers(convs, inpt, with_fc=True, reuse=None):
    with tf.variable_scope('convnet', reuse=reuse):
        out = inpt
        for num_outputs, kernel_size, stride, padding in convs:
            out = layers.convolution2d(out,
                                       num_outputs=num_outputs,
                                       kernel_size=kernel_size,
                                       stride=stride,
                                       padding='SAME',
                                       activation_fn=tf.nn.elu)
        conv_out = layers.flatten(out)
        if with_fc:
            conv_out = layers.fully_connected(conv_out,
                                              256,
                                              activation_fn=tf.nn.elu)
    return conv_out
Exemplo n.º 40
0
    def __building_block_B(self, input, ws):
        # Dimension where we want to iteration through with multiple 1D CNN
        dimension = input.get_shape()[2].value

        # Stores the 1d conv output
        convs = []
        # Per dimension iteration
        with tf.variable_scope("building_block_B"):
            for d in xrange(dimension):
                conv = convolution2d(tf.expand_dims(input[:,:,d,:],1), self.conf.num_filters_B, kernel_size=[1,ws], stride=[1,1], \
                    padding='VALID', weights_regularizer=self.regularizer, biases_regularizer=self.regularizer)
                # Removing the dimension with 1
                conv = tf.squeeze(conv, axis=[0,1])
                convs.append(conv)

        return convs
Exemplo n.º 41
0
 def __call__(self, x, reuse=tf.AUTO_REUSE, split=False):
     with tf.variable_scope(self.name,
                            reuse=tf.AUTO_REUSE,
                            regularizer=tcl.l2_regularizer(1e-3)) as vs:
         x = tf.reshape(x, [-1, 200, 200, 2])
         conv0 = tcl.convolution2d(x,
                                   64, [3, 3], [1, 1],
                                   activation_fn=tf.nn.relu)
         conv0 = tcl.convolution2d(conv0,
                                   64, [4, 4], [2, 2],
                                   activation_fn=tf.nn.relu)
         conv0 = tcl.convolution2d(conv0,
                                   128, [4, 4], [2, 2],
                                   activation_fn=tf.nn.relu)
         conv0 = tcl.instance_norm(conv0)
         conv1 = tcl.convolution2d(conv0,
                                   128, [4, 4], [2, 2],
                                   activation_fn=tf.nn.relu)
         conv1 = tcl.convolution2d(conv1,
                                   128, [4, 4], [2, 2],
                                   activation_fn=tf.nn.relu)
         conv1 = tcl.convolution2d(conv1,
                                   128, [3, 3], [1, 1],
                                   activation_fn=tf.nn.relu)
         conv1 = tcl.instance_norm(conv1)
         conv2 = tcl.convolution2d(conv1,
                                   256, [4, 4], [2, 2],
                                   activation_fn=tf.nn.relu)
         conv2 = tcl.convolution2d(conv2,
                                   512, [4, 4], [2, 2],
                                   activation_fn=tf.nn.relu)
         conv2 = tcl.convolution2d(conv2,
                                   512, [3, 3], [1, 1],
                                   activation_fn=tf.nn.relu)
         conv6 = tcl.flatten(conv2)
         fc2 = tcl.fully_connected(conv6,
                                   self.class_num,
                                   activation_fn=tf.identity)
         return fc2
Exemplo n.º 42
0
    def forward(self, input_tensor, is_training):
        dropout_value = 0.5
        input_tensor = tf.expand_dims(input_tensor, -1)

        h = tf.layers.conv3d(input_tensor, 16, [5, 5, 3], padding="same")
        h = tf.layers.batch_normalization(h)
        h = tf.nn.relu(h)
        h = tf.layers.max_pooling3d(h, [2, 2, 2], [2, 2, 2])
        h = L.dropout(h, keep_prob=dropout_value, is_training=is_training)
        print(h)

        h = tf.layers.conv3d(h, 32, [5, 5, 2], padding="same")
        h = tf.layers.batch_normalization(h)
        h = tf.nn.relu(h)
        h = tf.layers.max_pooling3d(h, [2, 2, 1], [2, 2, 1])
        h = L.dropout(h, keep_prob=dropout_value, is_training=is_training)
        print(h)

        h = tf.layers.conv3d(h, 64, [5, 5, 2], padding="same")
        h = tf.layers.batch_normalization(h)
        h = tf.nn.relu(h)
        h = tf.layers.max_pooling3d(h, [2, 2, 2], [2, 2, 2])
        h = L.dropout(h, keep_prob=dropout_value, is_training=is_training)
        print(h)

        h = tf.layers.conv3d(h, 128, [5, 5, 1], padding="same")
        h = tf.layers.batch_normalization(h)
        h = tf.nn.relu(h)
        h = L.dropout(h, keep_prob=dropout_value, is_training=is_training)

        print(h)
        h = tf.squeeze(h, 3)
        print(h)

        h = L.convolution2d_transpose(h, 128, [5, 5], [2, 2], activation_fn=tf.nn.relu)
        h = L.dropout(h, keep_prob=dropout_value, is_training=is_training)

        h = L.convolution2d_transpose(h, 64, [5, 5], [2, 2], activation_fn=tf.nn.relu)
        h = L.dropout(h, keep_prob=dropout_value, is_training=is_training)

        h = L.convolution2d_transpose(h, 32, [5, 5], [2, 2], activation_fn=tf.nn.relu)
        h = L.dropout(h, keep_prob=dropout_value, is_training=is_training)

        h = L.convolution2d(h, len(self.classes) + 1, [1, 1], [1, 1], activation_fn=None)

        return h
Exemplo n.º 43
0
def _cnn_to_mlp(convs,
                hiddens,
                dueling,
                inpt,
                num_actions,
                scope,
                reuse=False):
    with tf.variable_scope(scope, reuse=reuse):
        tf.summary.histogram('input', inpt)
        out = inpt
        with tf.variable_scope("convnet"):
            for num_outputs, kernel_size, stride in convs:
                out = layers.convolution2d(out,
                                           num_outputs=num_outputs,
                                           kernel_size=kernel_size,
                                           stride=stride,
                                           activation_fn=tf.nn.relu)
        out = layers.flatten(out)
        with tf.variable_scope("action_value"):
            action_out = out
            for hidden in hiddens:
                action_out = layers.fully_connected(action_out,
                                                    num_outputs=hidden,
                                                    activation_fn=tf.nn.relu)
            action_scores = layers.fully_connected(action_out,
                                                   num_outputs=num_actions,
                                                   activation_fn=None)

        if dueling:
            with tf.variable_scope("state_value"):
                state_out = out
                for hidden in hiddens:
                    state_out = layers.fully_connected(
                        state_out,
                        num_outputs=hidden,
                        activation_fn=tf.nn.relu)
                state_score = layers.fully_connected(state_out,
                                                     num_outputs=1,
                                                     activation_fn=None)
            action_scores_mean = tf.reduce_mean(action_scores, 1)
            action_scores_centered = action_scores - tf.expand_dims(
                action_scores_mean, 1)
            return state_score + action_scores_centered
        else:
            return action_scores
        return out
Exemplo n.º 44
0
def res_net(x, y, activation=tf.nn.relu):
  """Builds a residual network.

  Note that if the input tensor is 2D, it must be square in order to be
  converted to a 4D tensor.

  Borrowed structure from:
  github.com/pkmital/tensorflow_tutorials/blob/master/10_residual_network.py

  Args:
    x: Input of the network
    y: Output of the network
    activation: Activation function to apply after each convolution

  Returns:
    Predictions and loss tensors.
  """

  # Configurations for each bottleneck group.
  BottleneckGroup = namedtuple(
      'BottleneckGroup', ['num_blocks', 'num_filters', 'bottleneck_size'])
  groups = [BottleneckGroup(3, 128, 32),
            BottleneckGroup(3, 256, 64),
            BottleneckGroup(3, 512, 128),
            BottleneckGroup(3, 1024, 256)]

  input_shape = x.get_shape().as_list()

  # Reshape the input into the right shape if it's 2D tensor
  if len(input_shape) == 2:
    ndim = int(sqrt(input_shape[1]))
    x = tf.reshape(x, [-1, ndim, ndim, 1])

  # First convolution expands to 64 channels
  with tf.variable_scope('conv_layer1'):
    net = convolution2d(x, 64, 7,
                        normalizer_fn=batch_norm,
                        activation_fn=activation)

  # Max pool
  net = tf.nn.max_pool(
      net, [1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')

  # First chain of resnets
  with tf.variable_scope('conv_layer2'):
    net = convolution2d(net, groups[0].num_filters, 1,
                        padding='VALID')

  # Create the bottleneck groups, each of which contains `num_blocks`
  # bottleneck groups.
  for group_i, group in enumerate(groups):
    for block_i in range(group.num_blocks):
      name = 'group_%d/block_%d' % (group_i, block_i)

      # 1x1 convolution responsible for reducing dimension
      with tf.variable_scope(name + '/conv_in'):
        conv = convolution2d(net, group.bottleneck_size, 1,
                             padding='VALID',
                             activation_fn=activation,
                             normalizer_fn=batch_norm)

      with tf.variable_scope(name + '/conv_bottleneck'):
        conv = convolution2d(conv, group.bottleneck_size, 3,
                             padding='SAME',
                             activation_fn=activation,
                             normalizer_fn=batch_norm)

      # 1x1 convolution responsible for restoring dimension
      with tf.variable_scope(name + '/conv_out'):
        input_dim = net.get_shape()[-1].value
        conv = convolution2d(conv, input_dim, 1,
                             padding='VALID',
                             activation_fn=activation,
                             normalizer_fn=batch_norm)

      # shortcut connections that turn the network into its counterpart
      # residual function (identity shortcut)
      net = conv + net

    try:
      # upscale to the next group size
      next_group = groups[group_i + 1]
      with tf.variable_scope('block_%d/conv_upscale' % group_i):
        net = convolution2d(net, next_group.num_filters, 1,
                            activation_fn=None,
                            biases_initializer=None,
                            padding='SAME')
    except IndexError:
      pass

  net_shape = net.get_shape().as_list()
  net = tf.nn.avg_pool(net,
                       ksize=[1, net_shape[1], net_shape[2], 1],
                       strides=[1, 1, 1, 1], padding='VALID')

  net_shape = net.get_shape().as_list()
  net = tf.reshape(net, [-1, net_shape[1] * net_shape[2] * net_shape[3]])

  target = tf.one_hot(y, depth=10, dtype=tf.float32)
  logits = tf.contrib.layers.fully_connected(net, 10, activation_fn=None)
  loss = tf.contrib.losses.softmax_cross_entropy(logits, target)
  return tf.softmax(logits), loss
Exemplo n.º 45
0
import tensorflow as tf
# from tensorflow.contrib import learn
from tensorflow.contrib.layers import convolution2d
from tensorflow.contrib.framework.python.ops import variables
from tensorflow.contrib.layers.python.layers import utils
from tensorflow.contrib.layers.python.layers import initializers

# method 1: contirb API
net = convolution2d(net,
                    num_outputs=num_ker,
                    kernel_size=[ker_size,1],
                    stride=[stride,1]
                    padding='SAME',
                    activation_fn=None,
                    normalizer_fn=None,
                    weights_initializer=initializers.variance_scaling_initializer(),
                    biases_initializer=init_ops.zeros_initializer,)

# method 2: original conv2d API
weights = variables.model_variable(
    'weights',
    shape=[ker_size, 1, num_ker_in, num_ker],
    dtype=dtype,
    initializer=initializers.variance_scaling_initializer(),
    trainable=True)
biases = variables.model_variable(
    'biases',
    shape=[num_ker],
    dtype=dtype,
    initializer=tf.zeros_initializer,
    trainable=True)
Exemplo n.º 46
0
def run_network(inpt, string, is_training, debug=False, strip_batchnorm_from_last_layer=False):
    maybe_fc_batch_norm   = layers.batch_norm
    maybe_conv_batch_norm = conv_batch_norm

    if debug:
        print ("%s architecture" % (tf.get_variable_scope().name,))

    layer_idx = 0

    out = inpt
    layer_strs = string.split(",")
    for i, layer in enumerate(layer_strs):
        if i + 1 == len(layer_strs) and strip_batchnorm_from_last_layer:
            maybe_fc_batch_norm   = None
            maybe_conv_batch_norm = None

        if layer.startswith("conv:"):
            nkernels, stride, num_outputs, nonlinearity_str = parse_conv_params(layer[len("conv:"):].split(":"))
            nonlinearity = NONLINEARITY_NAME_TO_F[nonlinearity_str]

            out = layers.convolution2d(
                out,
                num_outputs=num_outputs,
                kernel_size=nkernels,
                stride=stride,
                normalizer_params={"is_training": is_training},
                normalizer_fn=maybe_conv_batch_norm,
                activation_fn=nonlinearity,
                scope='layer_%d' % (layer_idx,)
            )
            layer_idx += 1

            if debug:
                print ("Convolution with nkernels=%d, stride=%d, num_outputs=%d followed by %s" %
                        (nkernels, stride, num_outputs, nonlinearity_str))

        elif layer.startswith("deconv:"):
            nkernels, stride, num_outputs, nonlinearity_str = parse_conv_params(layer[len("deconv:"):].split(":"))
            nonlinearity = NONLINEARITY_NAME_TO_F[nonlinearity_str]

            out = layers.convolution2d_transpose(
                out,
                num_outputs=num_outputs,
                kernel_size=nkernels,
                stride=stride,
                activation_fn=nonlinearity,
                normalizer_fn=maybe_conv_batch_norm,
                normalizer_params={"is_training": is_training},
                scope='layer_%d' % (layer_idx,)
            )
            layer_idx += 1
            if debug:
                print ("Deconvolution with nkernels=%d, stride=%d, num_outputs=%d followed by %s" %
                        (nkernels, stride, num_outputs, nonlinearity_str))
        elif layer.startswith("fc:"):
            params = layer[len("fc:"):].split(":")
            nonlinearity_str = 'relu'
            if len(params) == 2:
                params, nonlinearity_str = params[:-1], params[-1]
            num_outputs = parse_math(params[0])
            nonlinearity = NONLINEARITY_NAME_TO_F[nonlinearity_str]

            out = layers.fully_connected(
                out,
                num_outputs=num_outputs,
                activation_fn=nonlinearity,
                normalizer_fn=maybe_fc_batch_norm,
                normalizer_params={"is_training": is_training, "updates_collections": None},
                scope='layer_%d' % (layer_idx,)
            )
            layer_idx += 1
            if debug:
                print ("Fully connected with num_outputs=%d followed by %s" %
                        (num_outputs, nonlinearity_str))
        elif layer.startswith("reshape:"):
            params = layer[len("reshape:"):].split(":")
            dims = [parse_math(dim) for dim in params]
            out = tf.reshape(out, [-1] + dims)
            if debug:
                print("Reshape to %r" % (dims,))
        else:
            raise ValueError("Could not parse layer description: %r" % (layer,))
    if debug:
        print("")
    return out
Exemplo n.º 47
0
import tensorflow as tf
import tensorflow.contrib.layers as layers
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("data/mnist", one_hot=True)
num_train = mnist.train.num_examples
num_test = mnist.test.num_examples
batch_size = 100
num_train_batches = num_train / batch_size
num_test_batches = num_test / batch_size
num_epochs = 10

x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
x_img = tf.reshape(x, [-1,28,28,1])
conv1 = layers.max_pool2d(
        layers.convolution2d(x_img, 32, [5, 5]),
        [2, 2], [2, 2])
conv2 = layers.max_pool2d(
        layers.convolution2d(conv1, 64, [5, 5]),
        [2, 2], [2, 2])
y = layers.fully_connected(layers.flatten(conv2),
        10, activation_fn=tf.nn.softmax)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y), reduction_indices=[1]))

train_step = tf.train.AdamOptimizer().minimize(cross_entropy)
correct = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

import time
init = tf.initialize_all_variables()
with tf.Session() as sess:
Exemplo n.º 48
0
 def layer_function(*args, **kwargs):
     kwargs['activation_fn'] = f
     return convolution2d(*args, **kwargs)
Exemplo n.º 49
0
    def resnn(self, sequences):
        """Build the resnn model.
        Args:
            page_batch: Sequences returned from inputs_train() or inputs_eval.
        Returns:
            Logits.
        """

        # [batch_size, html_len, 1, we_dim]
        target_expanded = tf.expand_dims(sequences, 2)

        # Configurations for each bottleneck block.
        BottleneckBlock = namedtuple(
            'BottleneckBlock',
            ['num_layers', 'num_filters', 'bottleneck_size'])
        # blocks = [BottleneckBlock(3, 128, 32),
        #           BottleneckBlock(3, 256, 64),
        #           BottleneckBlock(3, 512, 128),
        #           BottleneckBlock(3, 1024, 256)]
        # blocks = [BottleneckBlock(3, 128, 32),
        #           BottleneckBlock(3, 256, 64)]
        blocks = [BottleneckBlock(3, 64, 32), BottleneckBlock(6, 128, 64),
                  BottleneckBlock(3, 256, 128)]
        # BottleneckBlock(3, 512, 256)]

        # First convolution expands to 64 channels
        with tf.variable_scope('conv_layer1'):
            net = convolution2d(target_expanded,
                                64,
                                [7, 1],
                                stride=[2, 1],
                                activation_fn=self.activation,
                                normalizer_fn=batch_norm,
                                normalizer_params={'decay': self.norm_decay})

        # Max pool
        net = tf.nn.max_pool(net,
                             [1, 3, 1, 1],
                             strides=[1, 2, 1, 1],
                             padding='SAME')

        # First chain of resnets
        with tf.variable_scope('conv_layer2'):
            net = convolution2d(net,
                                blocks[0].num_filters,
                                [1, 1],
                                padding='VALID',
                                activation_fn=self.activation,
                                normalizer_fn=batch_norm,
                                normalizer_params={'decay': self.norm_decay})

        # Create each bottleneck building block for each layer
        for block_i, block in enumerate(blocks):
            for layer_i in range(block.num_layers):

                name = 'block_%d/layer_%d' % (block_i, layer_i)

                # 1x1 convolution responsible for reducing dimension
                with tf.variable_scope(name + '/conv_in'):
                    conv = convolution2d(
                        net,
                        block.bottleneck_size,
                        [1, 1],
                        padding='VALID',
                        activation_fn=self.activation,
                        normalizer_fn=batch_norm,
                        normalizer_params={'decay': self.norm_decay})

                with tf.variable_scope(name + '/conv_bottleneck'):
                    conv = convolution2d(
                        conv,
                        block.bottleneck_size,
                        [3, 1],
                        padding='SAME',
                        activation_fn=self.activation,
                        normalizer_fn=batch_norm,
                        normalizer_params={'decay': self.norm_decay})

                # 1x1 convolution responsible for restoring dimension
                with tf.variable_scope(name + '/conv_out'):
                    conv = convolution2d(
                        conv,
                        block.num_filters,
                        [1, 1],
                        padding='VALID',
                        activation_fn=self.activation,
                        normalizer_fn=batch_norm,
                        normalizer_params={'decay': self.norm_decay})

                # shortcut connections that turn the network into its counterpart
                # residual function (identity shortcut)
                net = conv + net

            try:
                # upscale to the next block size
                next_block = blocks[block_i + 1]
                with tf.variable_scope('block_%d/conv_upscale' % block_i):
                    net = convolution2d(
                        net,
                        next_block.num_filters,
                        [1, 1],
                        activation_fn=self.activation,
                        normalizer_fn=batch_norm,
                        normalizer_params={'decay': self.norm_decay})
            except IndexError:
                pass

        net_shape = net.get_shape().as_list()
        net = tf.nn.avg_pool(net,
                             ksize=[1, net_shape[1], net_shape[2], 1],
                             strides=[1, 1, 1, 1],
                             padding='VALID')

        net_shape = net.get_shape().as_list()
        softmax_len = net_shape[1] * net_shape[2] * net_shape[3]
        net = tf.reshape(net, [-1, softmax_len])

        # softmax, i.e. softmax(WX + b)
        with tf.variable_scope('softmax_linear'):
            WW = tf.get_variable(
                "WW",
                shape=[softmax_len, self.num_cats],
                initializer=tf.contrib.layers.xavier_initializer())
            b = self._variable_on_cpu('b',
                                      [self.num_cats],
                                      tf.constant_initializer(value=0.1))
            softmax_linear = tf.nn.xw_plus_b(net, WW, b, name="scores")

        return softmax_linear