def generator(input_shape, dimH, dimZ, dimY, n_channel, last_activation, name):

    # first construct p(y|z)
    fc_layers = [dimZ, dimH, dimY]
    pyz_mlp_layers = []
    N_layers = len(fc_layers) - 1
    l = 0
    for i in range(N_layers):
        name_layer = name + '_pyz_mlp_l%d' % l
        if i + 1 == N_layers:
            activation = 'linear'
        else:
            activation = 'relu'
        pyz_mlp_layers.append(
            mlp_layer(fc_layers[i], fc_layers[i + 1], activation, name_layer))

    def pyz_params(z):
        out = z
        for layer in pyz_mlp_layers:
            out = layer(out)
        return out

    # now construct p(z|x)
    # encoder for z (low res)
    layer_channels = [n_channel for i in range(3)]
    filter_width = 5
    filter_shapes = construct_filter_shapes(layer_channels, filter_width)
    fc_layer_sizes = [dimH]
    gen_conv2, conv_output_shape = ConvNet(name+'_pzx_conv', input_shape, filter_shapes, \
                                     fc_layer_sizes, 'relu',
                                     last_activation = 'relu')
    print('generator shared Conv net ' + ' network architecture:', \
            conv_output_shape, fc_layer_sizes)

    fc_layers = [dimH, dimH, dimZ * 2]
    pzx_mlp_layers = []
    N_layers = len(fc_layers) - 1
    l = 0
    for i in range(N_layers):
        name_layer = name + '_pzx_mlp_l%d' % l
        if i + 1 == N_layers:
            activation = 'linear'
        else:
            activation = 'relu'
        pzx_mlp_layers.append(
            mlp_layer(fc_layers[i], fc_layers[i + 1], activation, name_layer))

    def pzx_params(x):
        out = gen_conv2(x)
        for layer in pzx_mlp_layers:
            out = layer(out)
        mu, log_sig = tf.split(out, 2, axis=1)
        return mu, log_sig

    return pyz_params, pzx_params
예제 #2
0
def generator(dimX, dimH, dimZ, dimY, last_activation, name):

    # first construct p(y|z)
    fc_layers = [dimZ, dimH, dimY]
    pyz_mlp_layers = []
    N_layers = len(fc_layers) - 1
    l = 0
    for i in range(N_layers):
        name_layer = name + '_pyz_mlp_l%d' % l
        if i + 1 == N_layers:
            activation = 'linear'
        else:
            activation = 'relu'
        with tf.variable_scope('vae'):
            pyz_mlp_layers.append(
                mlp_layer(fc_layers[i], fc_layers[i + 1], activation,
                          name_layer))

    def pyz_params(z):
        out = z
        for layer in pyz_mlp_layers:
            out = layer(out)
        return out

    # now construct p(z|x)
    fc_layers = [dimX, dimH, dimH, dimZ * 2]
    l = 0
    pzx_mlp_layers = []
    N_layers = len(fc_layers) - 1
    for i in range(N_layers):
        if i < N_layers - 1:
            activation = 'relu'
        else:
            activation = 'linear'
        name_layer = name + '_pzx_mlp_l%d' % l
        with tf.variable_scope('vae'):
            pzx_mlp_layers.append(
                mlp_layer(fc_layers[i], fc_layers[i + 1], activation,
                          name_layer))
        l += 1

    def pzx_params(x):
        out = x
        for layer in pzx_mlp_layers:
            out = layer(out)
        mu, log_sig = tf.split(out, 2, axis=1)
        return mu, log_sig

    return pyz_params, pzx_params
def generator(dimX, dimH, dimZ, dimY, last_activation, name):

    # first construct p(y|z)
    fc_layers = [dimZ, dimH, dimH, dimY]
    pyz_mlp_layers = []
    N_layers = len(fc_layers) - 1
    l = 0
    for i in range(N_layers):
        name_layer = name + '_pyz_mlp_l%d' % l
        if i + 1 == N_layers:
            activation = 'linear'
        else:
            activation = 'relu'
        with tf.variable_scope('vae'):
            pyz_mlp_layers.append(
                mlp_layer(fc_layers[i], fc_layers[i + 1], activation,
                          name_layer))

    def pyz_params(z):
        out = z
        for layer in pyz_mlp_layers:
            out = layer(out)
        return out

    # now construct p(x|z)
    fc_layers = [dimZ, dimH, dimH, dimX]
    l = 0
    pxz_mlp_layers = []
    N_layers = len(fc_layers) - 1
    for i in range(N_layers):
        if i < N_layers - 1:
            activation = 'relu'
        else:
            activation = last_activation
        name_layer = name + '_pxz_mlp_l%d' % l
        with tf.variable_scope('vae'):
            pxz_mlp_layers.append(
                mlp_layer(fc_layers[i], fc_layers[i + 1], activation,
                          name_layer))
        l += 1

    def pxz_params(z):
        out = z
        for layer in pxz_mlp_layers:
            out = layer(out)
        return out

    return pyz_params, pxz_params
def generator(dimX, dimH, dimZ, last_activation, name, bin_num=2):
    # construct p(x|z)
    fc_layers = [dimZ, dimH, dimH, dimX * bin_num]
    l = 0
    pxz_mlp_layers = []
    N_layers = len(fc_layers) - 1
    for i in range(N_layers):
        if i < N_layers - 1:
            activation = 'relu'
        else:
            activation = last_activation
        name_layer = name + '_pxz_mlp_l%d' % l
        with tf.variable_scope('vae'):
            pxz_mlp_layers.append(
                mlp_layer(fc_layers[i], fc_layers[i + 1], activation,
                          name_layer))
        l += 1

    def pxz_params(z):
        out = z
        for layer in pxz_mlp_layers:
            out = layer(out)
        out = tf.reshape(out, [-1, 3514, bin_num])
        return out

    return pxz_params
예제 #5
0
def encoder_convnet(input_shape, dimH, dimZ, dimY, n_channel, dropout, name):

    # encoder for z (low res)
    layer_channels = [n_channel for i in range(3)]
    filter_width = 5
    filter_shapes = construct_filter_shapes(layer_channels, filter_width)
    fc_layer_sizes = [dimH]
    enc_conv, conv_output_shape = ConvNet(name+'_conv', input_shape, filter_shapes, \
                                     fc_layer_sizes, 'relu',
                                     last_activation = 'relu',
                                     dropout = dropout)
    print('encoder shared Conv net ' + ' network architecture:', \
            conv_output_shape, fc_layer_sizes)

    fc_layer = [dimH + dimY, dimH, dimZ]
    enc_mlp = []
    for i in range(len(fc_layer) - 1):
        if i + 2 < len(fc_layer):
            activation = 'relu'
        else:
            activation = 'linear'
        name_layer = name + '_mlp_l%d' % i
        enc_mlp.append(
            mlp_layer(fc_layer[i], fc_layer[i + 1], activation, name_layer))

    def apply_conv(x):
        return enc_conv(x)

    def apply_mlp(x, y):
        out = tf.concat([x, y], 1)
        for layer in enc_mlp:
            out = layer(out)
        return out

    return apply_conv, apply_mlp
예제 #6
0
def generator_head(dimZ, dimH, n_layers, name):
    fc_layer_sizes = [dimZ] + [dimH for i in range(n_layers)]
    layers = []
    N_layers = len(fc_layer_sizes) - 1
    for i in range(N_layers):
        d_in = fc_layer_sizes[i]; d_out = fc_layer_sizes[i+1]
        name_layer = name + '_head_l%d' % i
        layers.append(mlp_layer(d_in, d_out, 'relu', name_layer))
    
    print 'decoder head MLP of size', fc_layer_sizes
    
    def apply(x):
        for layer in layers:
            x = layer(x)
        return x
        
    return apply
def encoder_shared(dimX, dimH, n_layers, name):
 
    fc_layer_sizes = [dimX] + [dimH for i in range(n_layers)]
    layers = []
    N_layers = len(fc_layer_sizes) - 1
    for i in range(N_layers):
        d_in = fc_layer_sizes[i]; d_out = fc_layer_sizes[i+1]
        name_layer = name + '_shared_l%d' % i
        layers.append(mlp_layer(d_in, d_out, 'relu', name_layer))
        
    print 'encoder shared MLP of size', fc_layer_sizes
    
    def apply(x):
        for layer in layers:
            x = layer(x)
        return x
        
    return apply
예제 #8
0
def encoder_net(dimX, dimH, dimZ, n_layers, name):
    fc_layer = [dimX] + [dimH for i in range(n_layers)] + [dimZ]
    enc_mlp = []
    for i in range(len(fc_layer) - 1):
        if i + 2 < len(fc_layer):
            activation = 'relu'
        else:
            activation = 'linear'
        name_layer = name + '_mlp_l%d' % i
        with tf.variable_scope('vae'):
            enc_mlp.append(
                mlp_layer(fc_layer[i], fc_layer[i + 1], activation,
                          name_layer))

    def apply_mlp(x):
        out = tf.concat([x], 1)
        for layer in enc_mlp:
            out = layer(out)
        return out

    return apply_mlp
def encoder_head(dimH, dimZ, n_layers, name):
    fc_layer_sizes = [dimH for i in range(n_layers)] + [dimZ*2]
    layers = []
    N_layers = len(fc_layer_sizes) - 1
    for i in range(N_layers):
        d_in = fc_layer_sizes[i]; d_out = fc_layer_sizes[i+1]
        name_layer = name + '_head_l%d' % i
        if i < N_layers - 1:
            activation = 'relu'
        else:
            activation = 'linear'
        layers.append(mlp_layer(d_in, d_out, activation, name_layer))
        
    print 'encoder head MLP of size', fc_layer_sizes
    
    def apply(x):
        for layer in layers:
            x = layer(x)
        return x
        
    return apply
def encoder(dimX, dimH, dimZ, n_layers, name):
    fc_layer_sizes = [dimX] + [dimH for i in xrange(n_layers)] + [dimZ*2]
    layers = []
    N_layers = len(fc_layer_sizes) - 1
    for i in xrange(N_layers):
        d_in = fc_layer_sizes[i]; d_out = fc_layer_sizes[i+1]
        name_layer = name + '_l%d' % i
        if i < N_layers - 1:
            activation = 'relu'
        else:
            activation = 'linear'
        layers.append(mlp_layer(d_in, d_out, activation, name_layer))
        
    print 'encoder shared MLP of size', fc_layer_sizes
    
    def apply(x):
        for layer in layers:
            x = layer(x)
        mu, log_sig = tf.split(x, 2, axis=1)
        return mu, log_sig
        
    return apply
예제 #11
0
def generator_shared(dimX, dimH, n_layers, last_activation, name):
    # now construct a decoder
    fc_layer_sizes = [dimH for i in range(n_layers)] + [dimX]
    layers = []
    N_layers = len(fc_layer_sizes) - 1
    for i in range(N_layers):
        d_in = fc_layer_sizes[i]; d_out = fc_layer_sizes[i+1]
        if i < N_layers - 1:
            activation = 'relu'
        else:
            activation = last_activation
        name_layer = name + '_shared_l%d' % i
        layers.append(mlp_layer(d_in, d_out, activation, name_layer))
    
    print 'decoder shared MLP of size', fc_layer_sizes
    
    def apply(x):
        for layer in layers:
            x = layer(x)
        return x
        
    return apply
예제 #12
0
def generator(input_shape, dimH, dimZ, dimY, n_channel, last_activation, name):
    # first construct p(y|z)
    fc_layers = [dimZ, dimH, dimY]
    pyz_mlp_layers = []
    N_layers = len(fc_layers) - 1
    l = 0
    for i in range(N_layers):
        name_layer = name + '_pzy_l%d' % l
        if i+1 == N_layers:
            activation = 'linear'
        else:
            activation = 'relu'
        pyz_mlp_layers.append(mlp_layer(fc_layers[i], fc_layers[i+1], activation, name_layer))

    def pyz_params(z):
        out = z
        for layer in pyz_mlp_layers:
            out = layer(out)
        return out

    # now construct p(x|z, y)
    filter_width = 5
    decoder_input_shape = [(4, 4, n_channel), (7, 7, n_channel), (14, 14, n_channel)]
    decoder_input_shape.append(input_shape)
    fc_layers = [dimZ+dimY, dimH, int(np.prod(decoder_input_shape[0]))]
    l = 0
    # first include the MLP
    mlp_layers = []
    N_layers = len(fc_layers) - 1
    for i in range(N_layers):
        name_layer = name + '_l%d' % l
        mlp_layers.append(mlp_layer(fc_layers[i], fc_layers[i+1], 'relu', name_layer))
        l += 1
    
    conv_layers = []
    N_layers = len(decoder_input_shape) - 1
    for i in range(N_layers):
        if i < N_layers - 1:
            activation = 'relu'
        else:
            activation = last_activation
        name_layer = name + '_l%d' % l
        output_shape = decoder_input_shape[i+1]
        input_shape = decoder_input_shape[i]
        up_height = int(np.ceil(output_shape[0]/float(input_shape[0])))
        up_width = int(np.ceil(output_shape[1]/float(input_shape[1])))
        strides = (1, up_height, up_width, 1)       
        if activation in ['logistic_cdf', 'gaussian'] and i == N_layers - 1:	# ugly wrapping for logistic cdf likelihoods
            activation = 'split'
            output_shape = (output_shape[0], output_shape[1], output_shape[2]*2)
        
        filter_shape = (filter_width, filter_width, output_shape[-1], input_shape[-1])
        
        conv_layers.append(deconv_layer(output_shape, filter_shape, activation, \
                                            strides, name_layer))
        l += 1
    
    print('decoder shared Conv Net of size', decoder_input_shape)
    
    def pxzy_params(z, y):
        x = tf.concat([z, y], 1)
        for layer in mlp_layers:
            x = layer(x)
        x = tf.reshape(x, (x.get_shape().as_list()[0],)+decoder_input_shape[0])
        for layer in conv_layers:
            x = layer(x)
        return x
        
    return pyz_params, pxzy_params
def generator(input_shape, dimH, dimZ, dimY, n_channel, last_activation, name):

    # first construct p(y|z, x)
    # encoder for z (low res)
    layer_channels = [n_channel, n_channel * 2, n_channel * 4]
    filter_width = 5
    filter_shapes = construct_filter_shapes(layer_channels, filter_width)
    fc_layer_sizes = [dimH]
    gen_conv, conv_output_shape = ConvNet(name+'_pyzx_conv', input_shape, filter_shapes, \
                                     fc_layer_sizes, 'relu',
                                     last_activation = 'relu')
    print('generator shared Conv net ' + ' network architecture:', \
            conv_output_shape, fc_layer_sizes)

    fc_layers = [dimZ + dimH, dimH, dimY]
    pyzx_mlp_layers = []
    N_layers = len(fc_layers) - 1
    l = 0
    for i in range(N_layers):
        name_layer = name + '_pyzx_mlp_l%d' % l
        if i + 1 == N_layers:
            activation = 'linear'
        else:
            activation = 'relu'
        pyzx_mlp_layers.append(
            mlp_layer(fc_layers[i], fc_layers[i + 1], activation, name_layer))

    def pyzx_params(z, x):
        fea = gen_conv(x)
        out = tf.concat([fea, z], axis=1)
        for layer in pyzx_mlp_layers:
            out = layer(out)
        return out

    # now construct p(x|z)
    filter_width = 5
    decoder_input_shape = [(4, 4, n_channel * 4), (8, 8, n_channel * 2),
                           (16, 16, n_channel)]
    decoder_input_shape.append(input_shape)
    fc_layers = [dimZ, dimH, int(np.prod(decoder_input_shape[0]))]
    l = 0
    # first include the MLP
    mlp_layers = []
    N_layers = len(fc_layers) - 1
    for i in range(N_layers):
        name_layer = name + '_l%d' % l
        mlp_layers.append(
            mlp_layer(fc_layers[i], fc_layers[i + 1], 'relu', name_layer))
        l += 1

    conv_layers = []
    N_layers = len(decoder_input_shape) - 1
    for i in range(N_layers):
        if i < N_layers - 1:
            activation = 'relu'
        else:
            activation = last_activation
        name_layer = name + '_l%d' % l
        output_shape = decoder_input_shape[i + 1]
        input_shape = decoder_input_shape[i]
        up_height = int(np.ceil(output_shape[0] / float(input_shape[0])))
        up_width = int(np.ceil(output_shape[1] / float(input_shape[1])))
        strides = (1, up_height, up_width, 1)
        if activation in [
                'logistic_cdf', 'gaussian'
        ] and i == N_layers - 1:  # ugly wrapping for logistic cdf likelihoods
            activation = 'split'
            output_shape = (output_shape[0], output_shape[1],
                            output_shape[2] * 2)

        filter_shape = (filter_width, filter_width, output_shape[-1],
                        input_shape[-1])

        conv_layers.append(deconv_layer(output_shape, filter_shape, activation, \
                                            strides, name_layer))
        l += 1

    print('decoder shared Conv Net of size', decoder_input_shape)

    def pxz_params(z):
        x = z
        for layer in mlp_layers:
            x = layer(x)
        x = tf.reshape(x,
                       (x.get_shape().as_list()[0], ) + decoder_input_shape[0])
        for layer in conv_layers:
            x = layer(x)
        return x

    return pyzx_params, pxz_params