Exemple #1
0
def conv2d(input_,
           output_dim,
           k_h=5,
           k_w=5,
           d_h=2,
           d_w=2,
           stddev=0.02,
           name="conv2d"):
    with tf.variable_scope(name):
        scope_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       tf.get_variable_scope().name)
        has_summary = any([('w' in v.op.name) for v in scope_vars])
        w = tf.get_variable(
            'w', [k_h, k_w, input_.get_shape()[-1], output_dim],
            initializer=tf.truncated_normal_initializer(stddev=stddev))
        conv = tf.nn.conv2d(input_,
                            w,
                            strides=[1, d_h, d_w, 1],
                            padding='SAME')

        biases = tf.get_variable('biases', [output_dim],
                                 initializer=tf.constant_initializer(0.0))
        conv = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())

        if not has_summary:
            variable_summaries({'W': w, 'b': biases})

        return conv
Exemple #2
0
def deconv2d(input_,
             output_shape,
             k_h=5,
             k_w=5,
             d_h=2,
             d_w=2,
             stddev=0.02,
             name="deconv2d",
             with_w=False,
             data_format='NCHW'):
    with tf.variable_scope(name):
        scope_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       tf.get_variable_scope().name)
        has_summary = any([('w' in v.op.name) for v in scope_vars])
        out_channel, in_channel = get_in_out_shape(
            output_shape,
            input_.get_shape().as_list(), data_format)
        strides = get_strides(d_h, d_w, data_format)

        # filter : [height, width, output_channels, in_channels]
        w = tf.get_variable(
            'w', [k_h, k_w, out_channel, in_channel],
            initializer=tf.random_normal_initializer(stddev=stddev))

        try:
            deconv = tf.nn.conv2d_transpose(input_,
                                            w,
                                            output_shape=output_shape,
                                            strides=strides,
                                            data_format=data_format)

        # Support for verisons of TensorFlow before 0.7.0
        except AttributeError:
            deconv = tf.nn.deconv2d(input_,
                                    w,
                                    output_shape=output_shape,
                                    strides=strides,
                                    data_format=data_format)
        biases = tf.get_variable('biases', [out_channel],
                                 initializer=tf.constant_initializer(0.0))
        deconv = tf.reshape(
            tf.nn.bias_add(deconv, biases, data_format=data_format),
            deconv.get_shape())

        if not has_summary:
            variable_summaries({'W': w, 'b': biases})

        if with_w:
            return deconv, w, biases
        else:
            return deconv
Exemple #3
0
def linear(input_,
           output_size,
           name="Linear",
           stddev=0.01,
           scale=1.0,
           with_learnable_sn_scale=False,
           with_sn=False,
           bias_start=0.0,
           with_w=False,
           update_collection=None,
           with_singular_values=False):
    shape = input_.get_shape().as_list()

    with tf.variable_scope(name):
        scope_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       tf.get_variable_scope().name)
        has_summary = any([('Matrix' in v.op.name) for v in scope_vars])
        matrix = tf.get_variable("Matrix", [shape[1], output_size], tf.float32,
                                 tf.random_normal_initializer(stddev=stddev))

        if with_sn:
            s = tf.get_variable('s',
                                shape=[1],
                                initializer=tf.constant_initializer(scale),
                                trainable=with_learnable_sn_scale,
                                dtype=tf.float32)

            matrix_bar, sigma = spectral_normed_weight(
                matrix, update_collection=update_collection, with_sigma=True)
            matrix_bar = s * matrix_bar
            mul = tf.matmul(input_, matrix_bar)

        else:
            mul = tf.matmul(input_, matrix)

        bias = tf.get_variable("bias", [output_size],
                               initializer=tf.constant_initializer(bias_start))

        if not has_summary:
            if with_sn:
                variable_summaries({'b': bias, 's': s, 'sigma_w': sigma})
                variable_summaries({'W': matrix},
                                   with_singular_values=with_singular_values)
            else:
                variable_summaries({'b': bias})
                variable_summaries({'W': matrix},
                                   with_singular_values=with_singular_values)

        if with_w:
            return mul + bias, matrix, bias
        else:
            return mul + bias
Exemple #4
0
def linear(input_,
           output_size,
           name="Linear",
           stddev=0.01,
           bias_start=0.0,
           with_w=False):
    shape = input_.get_shape().as_list()

    with tf.variable_scope(name):
        scope_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       tf.get_variable_scope().name)
        has_summary = any([('Matrix' in v.op.name) for v in scope_vars])
        matrix = tf.get_variable("Matrix", [shape[1], output_size], tf.float32,
                                 tf.random_normal_initializer(stddev=stddev))
        bias = tf.get_variable("bias", [output_size],
                               initializer=tf.constant_initializer(bias_start))

        if not has_summary:
            variable_summaries({'W': matrix, 'b': bias})

        if with_w:
            return tf.matmul(input_, matrix) + bias, matrix, bias
        else:
            return tf.matmul(input_, matrix) + bias
Exemple #5
0
def linear_one_hot(input_,
                   output_size,
                   num_classes,
                   name="Linear_one_hot",
                   stddev=0.01,
                   scale=1.0,
                   with_learnable_sn_scale=False,
                   with_sn=False,
                   bias_start=0.0,
                   with_w=False,
                   update_collection=None,
                   with_singular_values=False):
    with tf.variable_scope(name):
        scope_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       tf.get_variable_scope().name)
        has_summary = any([('Matrix' in v.op.name) for v in scope_vars])
        matrix = tf.get_variable("Matrix", [num_classes, output_size],
                                 tf.float32,
                                 tf.random_normal_initializer(stddev=stddev))

        if with_sn:
            s = tf.get_variable('s',
                                shape=[1],
                                initializer=tf.constant_initializer(scale),
                                trainable=with_learnable_sn_scale,
                                dtype=tf.float32)

            matrix_bar, sigma = spectral_normed_weight(
                matrix, update_collection=update_collection, with_sigma=True)
            matrix_bar = s * matrix_bar
            embed = tf.nn.embedding_lookup(matrix_bar, input_)

        else:
            embed = tf.nn.embedding_lookup(matrix, input_)

        if not has_summary:
            if with_sn:
                variable_summaries({'s': s, 'sigma_w': sigma})
                variable_summaries({'W': matrix},
                                   with_singular_values=with_singular_values)
            else:
                variable_summaries({'W': matrix},
                                   with_singular_values=with_singular_values)

        if with_w:
            return embed, matrix
        else:
            return embed
Exemple #6
0
def conv2d(input_,
           output_dim,
           k_h=5,
           k_w=5,
           d_h=2,
           d_w=2,
           stddev=0.02,
           scale=1.0,
           with_learnable_sn_scale=False,
           with_sn=False,
           name="snconv2d",
           update_collection=None,
           data_format='NCHW',
           with_singular_values=False):
    with tf.variable_scope(name):
        scope_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       tf.get_variable_scope().name)
        has_summary = any([('w' in v.op.name) for v in scope_vars])
        out_channel, in_channel = get_in_out_shape(
            [output_dim],
            input_.get_shape().as_list(), data_format)
        strides = get_strides(d_h, d_w, data_format)
        w = tf.get_variable(
            'w', [k_h, k_w, in_channel, out_channel],
            initializer=tf.truncated_normal_initializer(stddev=stddev))

        if with_sn:
            s = tf.get_variable('s',
                                shape=[1],
                                initializer=tf.constant_initializer(scale),
                                trainable=with_learnable_sn_scale,
                                dtype=tf.float32)
            w_bar, sigma = spectral_normed_weight(
                w, update_collection=update_collection, with_sigma=True)
            w_bar = s * w_bar
            conv = tf.nn.conv2d(input_,
                                w_bar,
                                strides=strides,
                                padding='SAME',
                                data_format=data_format)
        else:
            conv = tf.nn.conv2d(input_,
                                w,
                                strides=strides,
                                padding='SAME',
                                data_format=data_format)

        biases = tf.get_variable('biases', [output_dim],
                                 initializer=tf.constant_initializer(0.0))
        conv = tf.reshape(
            tf.nn.bias_add(conv, biases, data_format=data_format),
            conv.get_shape())

        if not has_summary:
            if with_sn:
                variable_summaries({'b': biases, 's': s, 'sigma_w': sigma})
                variable_summaries({'W': w},
                                   with_singular_values=with_singular_values)
            else:
                variable_summaries({'b': biases})
                variable_summaries({'W': w},
                                   with_singular_values=with_singular_values)

        return conv
Exemple #7
0
def deconv2d(input_,
             output_shape,
             k_h=5,
             k_w=5,
             d_h=2,
             d_w=2,
             stddev=0.02,
             scale=1.0,
             with_learnable_sn_scale=False,
             with_sn=False,
             name="deconv2d",
             with_w=False,
             update_collection=None,
             data_format='NCHW',
             with_singular_values=False):
    with tf.variable_scope(name):
        scope_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       tf.get_variable_scope().name)
        has_summary = any([('w' in v.op.name) for v in scope_vars])
        out_channel, in_channel = get_in_out_shape(
            output_shape,
            input_.get_shape().as_list(), data_format)
        strides = get_strides(d_h, d_w, data_format)
        # filter : [height, width, output_channels, in_channels]
        w = tf.get_variable(
            'w', [k_h, k_w, out_channel, in_channel],
            initializer=tf.random_normal_initializer(stddev=stddev))

        if with_sn:
            s = tf.get_variable('s',
                                shape=[1],
                                initializer=tf.constant_initializer(scale),
                                trainable=with_learnable_sn_scale,
                                dtype=tf.float32)
            w_bar, sigma = spectral_normed_weight(
                w, update_collection=update_collection, with_sigma=True)
            w_bar = s * w_bar
            deconv = tf.nn.conv2d_transpose(input_,
                                            w_bar,
                                            output_shape=output_shape,
                                            strides=strides,
                                            data_format=data_format)
        else:
            deconv = tf.nn.conv2d_transpose(input_,
                                            w,
                                            output_shape=output_shape,
                                            strides=strides,
                                            data_format=data_format)

        biases = tf.get_variable('biases', [out_channel],
                                 initializer=tf.constant_initializer(0.0))
        deconv = tf.reshape(
            tf.nn.bias_add(deconv, biases, data_format=data_format),
            deconv.get_shape())

        if not has_summary:
            if with_sn:
                variable_summaries({'b': biases, 's': s, 'sigma_w': sigma})
                variable_summaries({'W': w},
                                   with_singular_values=with_singular_values)
            else:
                variable_summaries({'b': biases})
                variable_summaries({'W': w},
                                   with_singular_values=with_singular_values)

        if with_w:
            return deconv, w, biases
        else:
            return deconv