import tensorflow as tf


        if init:
            x = tf.nn.conv2d_transpose(x, tf.nn.l2_normalize(V.initialized_value(), [0, 1, 3]), target_shape, [1] + list(stride) + [1], padding=pad)

            init_scale = .01
            m_init, v_init = tf.nn.moments(x, [0, 1, 2])
            scale_init = init_scale / tf.sqrt(v_init + 1e-10)
            with tf.control_dependencies([g.assign(g * scale_init), b.assign_add(-m_init * scale_init)]):
                x = tf.reshape(scale_init, [1, 1, 1, num_filters]) * (x - tf.reshape(m_init, [1, 1, 1, num_filters]))

        else:
            V = maybe_avg(V)
            g = maybe_avg(g)
            b = maybe_avg(b)

            W = tf.reshape(g, [1, 1, num_filters, 1]) * tf.nn.l2_normalize(V, [0, 1, 3])
            # calculate convolutional layer output
            x = tf.nn.conv2d_transpose(x, W, target_shape, [1] + list(stride) + [1], padding=pad)
            x = tf.nn.bias_add(x, b)

    return x


def transposed_gated_resnet(x, aux, dim=(32, [3, 3], [1, 1]), activation=tf.nn.elu, scope="transposed_gated_resnet", residual=True, dropout=.0, conv=conv2d, training=True, ema=None, init=False):
    out = conv(activation(x), [dim[0], dim[1], [1, 1]], scope="%s_conv_in" % scope, training=training, ema=ema, init=init)
    in_shp = x.get_shape().as_list()
    assert in_shp[1] == in_shp[2]

    if aux is not None:
        aux_shp = aux.get_shape().as_list()
