Beispiel #1
0
def make_generator_model():
    model = tf.keras.Sequential()

    model.add(layers.Input(shape=(47, 1)))  # Noise shape

    model.add(layers.Bidirectional(layers.LSTM(64, return_sequences=True)))

    model.add(layers.Conv1D(filters=128, kernel_size=16, strides=1, padding='same'))
    model.add(layers.LeakyReLU())

    model.add(layers.Conv1D(filters=64, kernel_size=16, strides=1, padding='same'))
    model.add(layers.LeakyReLU())

    model.add(layers.UpSampling1D(2))

    model.add(layers.Conv1D(filters=32, kernel_size=16, strides=1, padding='same'))
    model.add(layers.LeakyReLU())

    model.add(layers.Conv1D(filters=16, kernel_size=16, strides=1, padding='same'))
    model.add(layers.LeakyReLU())

    model.add(layers.UpSampling1D(2))

    model.add(layers.Conv1D(filters=1, kernel_size=16, strides=1, padding='same', activation='tanh'))

    model.add(layers.Permute((2, 1)))

    return model
Beispiel #2
0
def define_generator(input_shape, n_class=4):
    in_latent = tf.keras.Input(shape=input_shape)
    n_nodes = 15*128

    in_label = layers.Input(shape=(1,))
    lb = layers.Embedding(n_class, 50)(in_label)
    lb = layers.Dense(15)(lb)
    lb = layers.Reshape((15, 1))(lb)
    gen = layers.Dense(n_nodes)(in_latent)
    gen = layers.LeakyReLU(alpha=0.2)(gen)
    gen = layers.Reshape((15, 128))(gen)

    gen = layers.Concatenate()([gen, lb])

    gen = layers.Conv1D(128, 16, padding='same')(gen)
    gen = layers.LeakyReLU(alpha=0.2)(gen)
    gen = layers.UpSampling1D(2)(gen)
    gen = layers.BatchNormalization()(gen)

    gen = layers.Conv1D(64, 16, padding='same')(gen)
    gen = layers.LeakyReLU(alpha=0.2)(gen)
    gen = layers.UpSampling1D(3)(gen)
    gen = layers.BatchNormalization()(gen)

    gen = layers.Conv1D(32, 16, padding='same')(gen)
    gen = layers.LeakyReLU(alpha=0.2)(gen)
    gen = layers.UpSampling1D(2)(gen)
    gen = layers.BatchNormalization()(gen)

    g_out = layers.Conv1D(1, 16, activation='tanh',padding='same')(gen)
    # g_out = layers.Permute((2,1))(gen)

    g_model = tf.keras.Model([in_latent, in_label], g_out)
    # model.summary()
    return g_model
Beispiel #3
0
def cnn_1(N):
    layer_in = tfkl.Input(shape=[N, 1])
    enc = tfkl.Conv1D(8,
                      8,
                      strides=1,
                      padding='SAME',
                      activation='relu',
                      use_bias=False)(layer_in)
    enc = tfkl.MaxPooling1D(2)(enc)
    enc = tfkl.Conv1D(16,
                      8,
                      strides=1,
                      padding='SAME',
                      activation='relu',
                      use_bias=False)(enc)
    enc = tfkl.MaxPooling1D(2)(enc)
    enc = tfkl.Conv1D(32,
                      8,
                      strides=1,
                      padding='SAME',
                      activation='relu',
                      use_bias=False)(enc)
    enc = tfkl.MaxPooling1D(2)(enc)
    enc = tfkl.Conv1D(64,
                      8,
                      strides=1,
                      padding='SAME',
                      activation='relu',
                      use_bias=False)(enc)
    dec = tfkl.UpSampling1D(2)(enc)
    dec = tfkl.Conv1D(32,
                      8,
                      strides=1,
                      padding='SAME',
                      activation='relu',
                      use_bias=False)(dec)
    dec = tfkl.UpSampling1D(2)(dec)
    dec = tfkl.Conv1D(16,
                      8,
                      strides=1,
                      padding='SAME',
                      activation='relu',
                      use_bias=False)(dec)
    dec = tfkl.UpSampling1D(2)(dec)
    dec = tfkl.Conv1D(8,
                      8,
                      strides=1,
                      padding='SAME',
                      activation='relu',
                      use_bias=False)(dec)
    out = tfkl.Conv1D(1, 8, strides=1, padding='SAME', use_bias=False)(dec)
    model = tf.keras.Model(inputs=[layer_in], outputs=[out])
    return model
Beispiel #4
0
def build_model(wav_size=16384, chunk_size=256, chunk_hop=128, emb_size=32):
    N_WIN = (wav_size - chunk_size) // chunk_hop + 1

    hann_win = tf.signal.hann_window(chunk_size)
    wav_input = tf.keras.Input(shape=(wav_size, ))

    encoder = tf.signal.frame(wav_input, chunk_size, chunk_hop, axis=1)
    encoder = tf.multiply(encoder, hann_win)
    encoder = tf.expand_dims(encoder, axis=-1)
    encoder = tfkl.TimeDistributed(tfkl.Conv1D(64, 8, padding='SAME'))(encoder)
    encoder = tfkl.TimeDistributed(
        tfkl.Conv1D(64, 8, padding='SAME', strides=8))(encoder)
    encoder = tfkl.TimeDistributed(
        tfkl.Conv1D(64, 8, padding='SAME', strides=4))(encoder)
    encoder = tfkl.TimeDistributed(
        tfkl.Conv1D(64, 8, padding='SAME', strides=2))(encoder)
    encoder = tfkl.TimeDistributed(tfkl.Flatten())(encoder)
    encoder = tfkl.GRU(emb_size, return_sequences=True)(encoder)  #Global LSTM

    encoder_model = tf.keras.Model(wav_input, encoder)

    decoder_input = tf.keras.Input(shape=(N_WIN, emb_size), name='encoded_img')

    decoder = tfkl.GRU(256, return_sequences=True)(decoder_input)
    decoder = tfkl.TimeDistributed(tfkl.Reshape(target_shape=(4, 64)))(decoder)
    decoder = tfkl.TimeDistributed(tfkl.Conv1D(64, 8, padding='SAME'))(decoder)
    decoder = tfkl.TimeDistributed(tfkl.UpSampling1D(2))(decoder)
    decoder = tfkl.TimeDistributed(tfkl.Conv1D(64, 8, padding='SAME'))(decoder)
    decoder = tfkl.TimeDistributed(tfkl.UpSampling1D(4))(decoder)
    decoder = tfkl.TimeDistributed(tfkl.Conv1D(64, 8, padding='SAME'))(decoder)
    decoder = tfkl.TimeDistributed(tfkl.UpSampling1D(8))(decoder)
    decoder = tfkl.TimeDistributed(
        tfkl.Conv1D(1, 8, padding='SAME', activation='tanh',
                    dtype=tf.float32))(decoder)
    decoder = tfkl.Reshape(target_shape=(N_WIN, chunk_size))(decoder)
    decoder = tf.multiply(decoder, hann_win)
    decoder = tf.signal.overlap_and_add(decoder, chunk_hop)

    decoder_model = tf.keras.Model(decoder_input, decoder)

    ae_input = tf.keras.Input(shape=(wav_size, ))
    encoder_out = encoder_model(ae_input)
    decoder_out = decoder_model(encoder_out)

    ae_model = tf.keras.Model(inputs=ae_input, outputs=decoder_out)

    return ae_model, encoder_model, decoder_model
Beispiel #5
0
    def build(self, input_shape):
        assert len(input_shape) == 2
        assert len(input_shape[0]) == 3 and len(input_shape[1]) == 3
        # Assertion for high inputs
        assert input_shape[0][1] >= self.mor_kernel_size
        # Assertion for low inputs
        assert input_shape[0][1] // input_shape[1][1] == self.beta
        # channel last for Tensorflow
        assert K.image_data_format() == 'channels_last'

        self.conv_high_high = NCausalConv1D(
            filters=self.high_channels,
            kernel_size=self.mor_kernel_size,
            dilation_rate=self.mor_dilation,
            activation=tf.nn.leaky_relu,
        )
        self.conv_low_high = NCausalConv1D(
            filters=self.high_channels,
            kernel_size=self.rhy_kernel_size,
            dilation_rate=self.rhy_dilation,
            activation=tf.nn.leaky_relu,
        )
        self.conv_high_low = NCausalConv1D(
            filters=self.low_channels,
            kernel_size=self.mor_kernel_size,
            dilation_rate=self.mor_dilation,
            activation=tf.nn.leaky_relu,
        )
        self.conv_low_low = NCausalConv1D(
            filters=self.low_channels,
            kernel_size=self.rhy_kernel_size,
            dilation_rate=self.rhy_dilation,
            activation=tf.nn.leaky_relu,
        )
        # self.bi_gru = BiGRUCell(rnn_size=self.filters,
        #                         dropout=self.dropout)
        self.high_cross = layers.Conv1D(self.filters, 1, padding='same')
        self.low_cross = layers.Conv1D(self.filters, 1, padding='same')
        self.upsampling1d = layers.UpSampling1D(size=self.beta)
        self.averagepooling1d = layers.AveragePooling1D(pool_size=self.beta)

        self.batch_norm1 = layers.BatchNormalization()
        self.batch_norm2 = layers.BatchNormalization()
        self.batch_norm3 = layers.BatchNormalization()
        self.batch_norm4 = layers.BatchNormalization()

        self.dropout_high = layers.Dropout(
            self.dropout,
            [tf.constant(1),
             tf.constant(1),
             tf.constant(self.high_channels)])
        self.dropout_low = layers.Dropout(
            self.dropout,
            [tf.constant(1),
             tf.constant(1),
             tf.constant(self.low_channels)])

        super().build(input_shape)
Beispiel #6
0
def make_aa_model():
    model = tf.keras.Sequential()
    model.add(layers.Conv1D(100, 10, activation='relu', padding="same", input_shape=(22143, 1)))
    model.add(layers.MaxPooling1D(5, padding="same"))  # output = 100 x 4429
    model.add(layers.Conv1D(50, 10, activation='relu', padding="same"))  # output = 50 x 8857
    model.add(layers.MaxPooling1D(5, padding="same"))  # output = 50 x 886 (8857/ 5)

    assert model.output_shape == (None, 886, 50)
    model.add(layers.Conv1D(50, 10, activation='relu', padding="same"))  # output = 50 x 886
    model.add(layers.UpSampling1D(5))  # output = 50 x 4430
    model.add(layers.Conv1D(50, 10, activation='relu', padding="same"))  # output = 50 x 4430
    model.add(layers.UpSampling1D(5))  # output = 50 x 44300
    model.add(layers.Conv1D(1, 8, activation='relu', padding="valid"))  # output = 1 x 22150 (using VALID)

    assert model.output_shape == (None, 22143, 1)
    print("C")

    return model
Beispiel #7
0
    def create_vae(self):
        input_size = self.hparams['window_samples']
        scale = self.hparams['model_scale']
        n_layers = 4

        i = tfkl.Input(shape=(input_size, 1))
        o = i
        for n in range(1, n_layers + 1):
            o = tfkl.Conv1D(scale * (n + 1),
                            kernel_size=2,
                            strides=2,
                            padding='same')(o)
            o = tfkl.BatchNormalization(axis=1)(o)
            o = tfkl.ReLU()(o)

        o = tfkl.Flatten()(o)

        o = tfkl.Dense(tfpl.IndependentNormal.params_size(
            self.hparams['latent_size']),
                       activation=None)(o)
        o = tfpl.IndependentNormal(
            self.hparams['latent_size'],
            activity_regularizer=tfpl.KLDivergenceRegularizer(self.prior,
                                                              weight=2.0))(o)

        encoder = tfk.Model(inputs=i, outputs=o)

        i = tfkl.Input(shape=(self.hparams['latent_size'], ))
        s = scale * (n_layers + 1)
        o = tfkl.Dense(s, activation='relu')(i)
        o = tfkl.Reshape(target_shape=(1, s))(o)

        for n in range(1, n_layers + 1):
            o = tfkl.UpSampling1D(size=2)(o)
            o = tfkl.Conv1D(scale * (n_layers + 1 - n),
                            kernel_size=2,
                            strides=1,
                            padding='same')(o)
            o = tfkl.BatchNormalization(axis=1)(o)
            o = tfkl.ReLU()(o)

        o = tfkl.Cropping1D((0, o.shape[1] - input_size))(o)
        o = tfkl.Conv1D(1, kernel_size=1, strides=1, padding='same')(o)
        o = tfkl.Flatten()(o)

        o = tfkl.Dense(tfpl.IndependentNormal.params_size(
            (self.hparams['window_samples'], 1)),
                       activation='tanh')(o)
        o = tfpl.IndependentNormal((self.hparams['window_samples'], 1))(o)

        decoder = tfk.Model(inputs=i, outputs=o)

        vae = tfk.Model(inputs=encoder.inputs,
                        outputs=decoder(encoder.outputs[0]))

        return vae, encoder, decoder
def make_generator_model():
    # input
    noise = layers.Input(shape=(noise_dim, ))

    label = layers.Input(shape=(1, ))
    label_embedding = layers.Flatten()(layers.Embedding(num_classes, noise_dim)(label))

    model_input = layers.multiply([noise, label_embedding])

    # network
    x = layers.Dense(1790 * 256, use_bias=False, input_shape=(noise_dim, ))(model_input)
    x = layers.Reshape((1790, 256))(x)
    x = layers.BatchNormalization(epsilon=2e-5, momentum=9e-1)(x)
    x = layers.LeakyReLU()(x)

    x = layers.Conv1D(256,
                      7,
                      strides=1,
                      padding='same',
                      use_bias=False,
                      kernel_initializer='glorot_uniform')(x)
    x = layers.UpSampling1D(2)(x)
    x = layers.BatchNormalization(epsilon=2e-5, momentum=9e-1)(x)
    x = layers.LeakyReLU()(x)

    x = layers.Conv1D(128,
                      5,
                      strides=1,
                      padding='same',
                      use_bias=False,
                      kernel_initializer='glorot_uniform')(x)
    x = layers.UpSampling1D(2)(x)
    x = layers.BatchNormalization(epsilon=2e-5, momentum=9e-1)(x)
    x = layers.LeakyReLU()(x)

    x = layers.Conv1D(channels,
                      5,
                      strides=1,
                      padding='same',
                      activation='softmax')(x)

    return Model([noise, label], x)
Beispiel #9
0
 def __init__(self, scale: tuple, interp=NEAREST, scope='UPS'):
     super(Upscale, self).__init__(scope)
     dim = len(scale)
     if dim == 1:
         self.fn = layers.UpSampling1D(scale_factor=scale, mode=TF_INTERP[interp])
     elif dim == 2:
         self.fn = layers.UpSampling2D(scale_factor=scale, mode=TF_INTERP[interp])
     elif dim == 3:
         self.fn = layers.UpSampling3D(scale_factor=scale, mode=TF_INTERP[interp])
     else:
         raise Exception('NEBULAE ERROR ⨷ %d-d upscaling is not supported.' % dim)
Beispiel #10
0
    def __init__(self, channels, kernel_size=4, w_norm_clip=2,
                 use_initial_activation=True, normalization=None, **kwargs):
        super(UpResBlock, self).__init__(**kwargs)
        self.out_channels = channels
        self.use_initial_activation = use_initial_activation
        self.norm0 = None
        self.norm1 = None
        if normalization is "layer":
            self.norm0 = layers.LayerNormalization(axis=[2])
            self.norm1 = layers.LayerNormalization(axis=[2])
        elif normalization is "batch":
            self.norm0 = layers.BatchNormalization()
            self.norm1 = layers.BatchNormalization()
        self.initial_activation = layers.LeakyReLU(alpha=0.2)
        self.up = layers.UpSampling1D(size=2)
        self.conv0 = layers.Conv1D(filters=channels, kernel_size=kernel_size, padding="same", activation=layers.LeakyReLU(),
                                   )#kernel_constraint=tf.keras.constraints.MaxNorm(max_value=w_norm_clip, axis=[0,1]))
        self.conv1 = layers.Conv1D(filters=channels, kernel_size=kernel_size, padding="same",
                                   )#kernel_constraint=tf.keras.constraints.MaxNorm(max_value=w_norm_clip, axis=[0,1]))

        self.up_s = layers.UpSampling1D(size=2)
        self.upconv_s = layers.Conv1D(filters=channels, kernel_size=1, padding="same",
                                      )#kernel_constraint=tf.keras.constraints.MaxNorm(max_value=w_norm_clip, axis=[0,1]))
Beispiel #11
0
    def upsample(x, c):
        """Upsamples tensor c to have the same second dimension as x

    Args:
      x (tf.Tensor): Input tensor of desired size
      c (tf.Tensor): Conditional tensor to upsample

    Returns:
      tf.Tensor of c upsamples s.t. the 2nd dimension matches
        that of x
    """
        length = x.shape[1]
        encoding_length = c.shape[1]
        upsampling = layers.UpSampling1D(length / encoding_length)
        return upsampling(c)
Beispiel #12
0
def cnn_2(N):
    layer_in = tfkl.Input(shape=[N, 1])
    enc = tfkl.Conv1D(256, 512, strides=128)(layer_in)
    enc = tfkl.Conv1D(64, 3, activation='relu', padding='SAME')(enc)
    enc = tfkl.Conv1D(64, 3, activation='relu', padding='SAME')(enc)
    enc = tfkl.MaxPooling1D(2)
    enc = tfkl.Conv1D(64, 3, activation='relu', padding='SAME')(enc)
    enc = tfkl.Conv1D(64, 3, activation='relu', padding='SAME')(enc)
    dec = tfkl.UpSampling1D(2)
    dec = tfkl.Conv1D(64, 3, activation='relu', padding='SAME')(dec)
    dec = tfkl.Conv1D(64, 3, activation='relu', padding='SAME')(dec)
    dec = tfkl.Conv1D(256, 3, activation='relu', padding='SAME')(dec)
    dec = tfkl.Conv1D(512, 1, activation='relu', padding='SAME')(dec)
    out = overlap_and_add(dec, 128)
    model = tf.keras.Model(inputs=[layer_in], outputs=[out])
    return model
    def __init__(self, hparams, **kwargs):
        super(WaveGlow, self).__init__(dtype=hparams['ftype'], **kwargs)

        assert (hparams['n_group'] % 2 == 0)
        self.n_flows = hparams['n_flows']
        self.n_group = hparams['n_group']
        self.n_early_every = hparams['n_early_every']
        self.n_early_size = hparams['n_early_size']
        self.upsampling_size = hparams['upsampling_size']
        self.hidden_channels = hparams['hidden_channels']
        self.mel_channels = hparams['mel_channels']
        self.hparams = hparams
        self.normalisation = self.hparams['train_batch_size'] * self.hparams[
            'segment_length']

        self.waveNetAffineBlocks = []
        self.weightNormInv1x1ConvLayers = []

        self.upsampling = layers.UpSampling1D(size=self.upsampling_size,
                                              dtype=self.dtype)

        n_half = self.n_group // 2
        n_remaining_channels = self.n_group

        for index in range(self.n_flows):
            if ((index % self.n_early_every == 0) and (index > 0)):
                n_half -= self.n_early_size // 2
                n_remaining_channels -= self.n_early_size

            self.weightNormInv1x1ConvLayers.append(
                tfa.layers.wrappers.WeightNormalization(
                    Inv1x1Conv(filters=n_remaining_channels,
                               dtype=hparams['ftype'],
                               name="newInv1x1conv_{}".format(index)),
                    data_init=False,
                    dtype=hparams['ftype']))

            self.waveNetAffineBlocks.append(
                WaveNetAffineBlock(n_in_channels=n_half,
                                   n_channels=hparams['n_channels'],
                                   n_layers=hparams['n_layers'],
                                   kernel_size=hparams['kernel_size'],
                                   dtype=hparams['ftype'],
                                   name="waveNetAffineBlock_{}".format(index)))

        self.n_remaining_channels = n_remaining_channels
Beispiel #14
0
def decoder_smate(encoder, timesteps, data_dim, pool_step):
    input_ = encoder.output  # input: (batch_size, timesteps, latent_dim)

    out = ll.UpSampling1D(size=pool_step)(input_)

    # 1D-CNN for reconstructing the spatial information
    #cells = [rnn_cell(module_name) for _ in range(num_layers)]
    #out = ll.RNN(cells, return_sequences=True)(out)

    # temporal axis
    if (module_name == 'gru'):
        if (tf.test.is_gpu_available()):
            out_t = ll.CuDNNGRU(hidden_dim, return_sequences=True)(out)
            for i in range(num_layers - 1):
                out_t = ll.CuDNNGRU(hidden_dim, return_sequences=True)(
                    out_t)  # output: (batch_size, timesteps, hidden_dim)
        else:
            out_t = ll.GRU(hidden_dim, return_sequences=True)(out)
            for i in range(num_layers - 1):
                out_t = ll.GRU(hidden_dim, return_sequences=True)(
                    out_t)  # output: (batch_size, timesteps, hidden_dim)

    elif (module_name == 'lstm'):
        if (tf.test.is_gpu_available()):
            out_t = ll.CuDNNLSTM(hidden_dim, return_sequences=True)(out)
            for i in range(num_layers - 1):
                out_t = ll.CuDNNLSTM(hidden_dim, return_sequences=True)(
                    out_t)  # output: (batch_size, timesteps, hidden_dim)
        else:
            out_t = ll.LSTM(hidden_dim, return_sequences=True)(out)
            for i in range(num_layers - 1):
                out_t = ll.LSTM(hidden_dim, return_sequences=True)(
                    out_t)  # output: (batch_size, timesteps, hidden_dim)

    out = ll.Dense(data_dim, activation='sigmoid')(out_t)

    model = Model(encoder.input, out)
    return model
Beispiel #15
0
 def __init__(self, **kwargs):
     super(_Subdecoder, self).__init__(**kwargs)
     self.conv = create_conv1d()
     self.cat = layers.Concatenate()
     self.up_sample = layers.UpSampling1D(size=SAMPLING_RATE)
Beispiel #16
0
def build_model(nhr, nreg, conv_layers, l1, l2, lgdp1, lgdp2, gdp_out_name,
                dec_out_name):
    """
    Build the convolutional neural network.

    Our model is constructed using the Keras functional API
    (https://keras.io/getting-started/functional-api-guide/) which allows us to
    have multiple inputs. Note that all inputs must be specified with an Input
    layer.

    The main input to this model is a 4-dimensional array:
        [cases, weeks, hours, regions]
    where
        cases = number of quarters (open)
        weeks = number of weeks in a quarter (open)
        hours = 168, the number of hours in a week (fixed)
        regions = number of spatial regions (fixed)

    We need an additional inputs:
        timestep = value representing time since the start of the data

    """
    # Weekly timeseries input
    input_numeric = layers.Input(shape=(nhr, nreg), name='HourlyElectricity')

    # Time since start input (for dealing with energy efficiency changes)
    input_time = layers.Input(shape=(1, ), name='TimeSinceStart')

    # Previous quarter's GDP
    input_gdp_prev = layers.Input(shape=(1, ), name='GDPPrev')

    # Natural gas data at a weekly level
    input_gas = layers.Input(shape=(1, ), name='NaturalGas')

    # Petroleum data at a weekly level
    input_petrol = layers.Input(shape=(1, ), name='Petroleum')

    ## Add a way to specify specific encodings to investigate what the
    ## encoder is responding to.  The following scalar is either 1 or
    ## zero.  0 means normal operation; 1 means we use a specified
    ## input for the encoding.
    input_switch = layers.Input(shape=(1, ), name='EncoderSwitch')
    input_switch_complement = layers.Input(shape=(1, ),
                                           name='EncoderSwitchComplement')
    encoder_input = layers.Input(
        shape=(l2, ), name='EncoderInput')  # ignored if input_switch == 0.

    # The convolutional layers need input tensors with the shape (batch, steps, channels).
    # A convolutional layer is 1D in that it slides through the data length-wise,
    # moving along just one dimension.
    # Parameters for the convolutional layers are given as a comma-separated argument:
    #
    #     [kernel_size]-[filters]-[pool_size (optional)],[filters]-...
    #
    # These are fed directly into the Conv1D layer, which has parameters:
    #     Conv1D(filters, kernel_size, ...)
    conv_params = parse_conv_layers(conv_layers)

    i = 0
    for param_set in conv_params:
        if i == 0:
            convolutions = layers.Conv1D(
                param_set[1],
                param_set[0],
                padding='same',
                activation='relu',
                bias_initializer='glorot_uniform')(input_numeric)
        else:
            convolutions = layers.Conv1D(
                param_set[1],
                param_set[0],
                padding='same',
                activation='relu',
                bias_initializer='glorot_uniform')(convolutions)
        convolutions = layers.MaxPool1D(param_set[2])(convolutions)
        i += 1

    feature_layer = layers.Flatten()(convolutions)

    # Merge the inputs together and end our encoding with fully connected layers
    encoded = layers.Dense(l1,
                           bias_initializer='glorot_uniform')(feature_layer)
    encoded = layers.LeakyReLU()(encoded)
    encoded = layers.Dense(l2,
                           bias_initializer='glorot_uniform',
                           name='FinalEncoding')(encoded)
    encoded = layers.LeakyReLU()(encoded)

    ## Implement the input switch (see above).
    # def oneminus(tensor):
    #     one = keras.backend.ones(shape=(1,))
    #     return layers.subtract([one, tensor])
    # input_switch_complement = layers.Lambda(oneminus)(input_switch)
    ##input_switch_complement = layers.Subtract()([one, input_switch])

    encoded = layers.Multiply()([encoded, input_switch_complement])
    enc_in = layers.Multiply()([encoder_input, input_switch])
    encoded = layers.Add(name='SwitchedEncoding')([encoded, enc_in])

    # At this point, the representation is the most encoded and small; now let's build the decoder
    decoded = layers.Dense(l1, bias_initializer='glorot_uniform')(encoded)
    decoded = layers.LeakyReLU()(decoded)
    decoded = layers.Dense(convolutions.shape[1] * convolutions.shape[2],
                           bias_initializer='glorot_uniform')(decoded)
    decoded = layers.LeakyReLU()(decoded)

    decoded = layers.Reshape((convolutions.shape[1], convolutions.shape[2]),
                             name='UnFlatten')(decoded)

    for param_set in reversed(conv_params):
        i -= 1
        decoded = layers.UpSampling1D(param_set[2])(decoded)
        if i == 0:
            decoded = layers.Conv1D(nreg,
                                    param_set[0],
                                    padding='same',
                                    activation='linear',
                                    bias_initializer='glorot_uniform',
                                    name=dec_out_name)(decoded)
        else:
            decoded = layers.Conv1D(conv_params[i - 1][1],
                                    param_set[0],
                                    padding='same',
                                    activation='relu',
                                    bias_initializer='glorot_uniform')(decoded)

    # This is our actual output, the GDP prediction
    merged_layer = layers.Concatenate()(
        [encoded, input_time, input_gdp_prev, input_gas, input_petrol])

    gdp_hidden_layer = layers.Dense(lgdp1, name='GDP_Hidden')(merged_layer)
    gdp_hidden_layer = layers.LeakyReLU()(gdp_hidden_layer)
    if lgdp2 > 0:
        gdp_hidden_layer = layers.Dense(
            lgdp2,
            name='GDP_Hidden2',
            kernel_regularizer=keras.regularizers.l1(0))(gdp_hidden_layer)
        gdp_hidden_layer = layers.LeakyReLU()(gdp_hidden_layer)

    output = layers.Dense(1, activation='linear',
                          name=gdp_out_name)(gdp_hidden_layer)

    autoencoder = keras.models.Model(inputs=[
        input_numeric, input_time, input_gdp_prev, input_gas, input_petrol,
        input_switch, input_switch_complement, encoder_input
    ],
                                     outputs=[output, decoded])

    return autoencoder
Beispiel #17
0
    cnn = Conv1D(72, 2, padding="same")(inputs)
    cnn = layers.PReLU()(cnn)
    concat = layers.Concatenate()([inputs, cnn])
    
    return cnn
​
inputs = layers.Input((num_nits, 1))
​
inputs_2 = layers.MaxPool1D()(inputs)
inputs_3 = layers.MaxPool1D(4)(inputs)
​
net1 = fine(inputs=inputs)
net2 = midium(inputs_2)
net3 = coarse(inputs_3)
​
net2 = layers.UpSampling1D()(net2)
net3 = layers.UpSampling1D(4)(net3)
​​
concat = layers.Add()([net1, net2, net3])
​
concat = layers.Flatten()(concat)
dense = layers.Dense(128, activation="relu")(concat)
outputs = layers.Dense(s)(concat)
​
model = tf.keras.Model(inputs, outputs)
####################################################################################################################################

####################################################################################################################################
from tensorflow.keras import layers
from tensorflow.keras.layers import Conv1D
Beispiel #18
0
    def build_model(self):

        inputs = layers.Input(shape=(self.window_size, 1))

        conv_1 = layers.Conv1D(64, 3, activation='relu',
                               padding='same')(inputs)
        conv_1 = layers.Conv1D(64, 3, activation='relu',
                               padding='same')(conv_1)

        pool_1 = layers.MaxPooling1D(pool_size=2)(conv_1)

        conv_2 = layers.Conv1D(128, 3, activation='relu',
                               padding='same')(pool_1)
        conv_2 = layers.Conv1D(128, 3, activation='relu',
                               padding='same')(conv_2)

        pool_2 = layers.MaxPooling1D(pool_size=2)(conv_2)

        conv_3 = layers.Conv1D(256, 3, activation='relu',
                               padding='same')(pool_2)
        conv_3 = layers.Conv1D(256, 3, activation='relu',
                               padding='same')(conv_3)

        pool_3 = layers.MaxPooling1D(pool_size=2)(conv_3)

        conv_4 = layers.Conv1D(512, 3, activation='relu',
                               padding='same')(pool_3)
        conv_4 = layers.Conv1D(512, 3, activation='relu',
                               padding='same')(conv_4)

        pool_4 = layers.MaxPooling1D(pool_size=2)(conv_4)

        conv_5 = layers.Conv1D(1024, 3, activation='relu',
                               padding='same')(pool_4)
        conv_5 = layers.Conv1D(1024, 3, activation='relu',
                               padding='same')(conv_5)

        up_1 = layers.UpSampling1D(size=2)(conv_5)

        merge_1 = layers.concatenate([conv_4, up_1], axis=2)

        conv_6 = layers.Conv1D(512, 3, activation='relu',
                               padding='same')(merge_1)
        conv_6 = layers.Conv1D(512, 3, activation='relu',
                               padding='same')(conv_6)

        up_2 = layers.UpSampling1D(size=2)(conv_6)

        merge_2 = layers.concatenate([conv_3, up_2], axis=2)

        conv_7 = layers.Conv1D(256, 3, activation='relu',
                               padding='same')(merge_2)
        conv_7 = layers.Conv1D(256, 3, activation='relu',
                               padding='same')(conv_7)

        up_3 = layers.UpSampling1D(size=2)(conv_7)

        merge_3 = layers.concatenate([conv_2, up_3], axis=2)

        conv_8 = layers.Conv1D(128, 3, activation='relu',
                               padding='same')(merge_3)
        conv_8 = layers.Conv1D(128, 3, activation='relu',
                               padding='same')(conv_8)

        up_4 = layers.UpSampling1D(size=2)(conv_8)

        merge_4 = layers.concatenate([conv_1, up_4], axis=2)

        conv_9 = layers.Conv1D(64, 3, activation='relu',
                               padding='same')(merge_4)
        conv_9 = layers.Conv1D(64, 3, activation='relu',
                               padding='same')(conv_9)

        outputs = layers.Conv1D(self.num_states,
                                3,
                                activation="softmax",
                                padding="same")(conv_9)

        final_reshape = layers.Reshape(
            (self.window_size, self.num_states))(outputs)
        model = Model(inputs, final_reshape, name='UNet')

        return model
Beispiel #19
0
def coarse_fine(inputs):
  
    def Net1(inputs):
        cnn = Conv1D(18,6,padding="causal",activation="relu")(inputs)
        cnn = Conv1D(18,6,padding="causal",activation="relu")(cnn)
        cnn = Conv1D(36,6,padding="causal",activation="relu")(cnn)
        cnn = Conv1D(36,6,padding="causal",activation="relu")(cnn)
        cnn = Conv1D(74,6,padding="causal",activation="relu")(cnn)
        cnn = Conv1D(74, 6, padding="causal", activation="relu")(cnn)
        cnn = Conv1D(148, 6, padding="causal", activation="relu")(cnn)
        cnn = Conv1D(148, 6, padding="causal", activation="relu")(cnn)
        cnn = Conv1D(296, 6, padding="causal", activation="relu")(cnn)
        return cnn
      
    def Net2(inputs):
        pool = layers.MaxPool1D(2)(inputs)
        cnn = Conv1D(18,8,padding="causal",activation="relu")(pool)
        cnn = Conv1D(36,8,padding="causal",activation="relu")(cnn)    
        cnn = Conv1D(74, 8, padding="causal", activation="relu")(cnn)
        cnn = Conv1D(148, 8, padding="causal", activation="relu")(cnn)
        cnn = Conv1D(296, 6, padding="causal", activation="relu")(cnn)
        return cnn

    def Net3(inputs):
        pool = layers.MaxPool1D(4)(inputs)
        cnn = Conv1D(36,8,padding="causal",activation="relu")(pool)
        cnn = Conv1D(74, 8, padding="causal", activation="relu")(cnn)
        cnn = Conv1D(148, 8, padding="causal", activation="relu")(cnn)
        cnn = Conv1D(296, 8, padding="causal", activation="relu")(cnn)

        return cnn
    def Net4(inputs):
        pool = layers.MaxPool1D(8)(inputs)
        cnn = Conv1D(74, 6, padding="causal", activation="relu")(pool)
        cnn = Conv1D(148, 6, padding="causal", activation="relu")(cnn)
        cnn = Conv1D(296, 6, padding="causal", activation="relu")(cnn)

        return cnn

    def Net5(inputs):
        pool = layers.MaxPool1D(16)(inputs)
        cnn = Conv1D(148, 4, padding="causal", activation="relu")(pool)
        cnn = Conv1D(296, 4, padding="causal", activation="relu")(cnn)

        return cnn

    def Net6(inputs):
        pool = layers.MaxPool1D(32)(inputs)
        cnn = Conv1D(296, 2, padding="causal", activation="relu")(pool)

        return cnn

    net1 = Net1(inputs=inputs)
    net2 = Net2(inputs=inputs)
    net3 = Net3(inputs=inputs)
    net4 = Net4(inputs=inputs)
    net5 = Net5(inputs=inputs)
    net6 = Net6(inputs=inputs)

#     fine_net = layers.UpSampling1D()(fine_net)
    net2 = layers.UpSampling1D(2)(net2)
    net3 = layers.UpSampling1D(4)(net3)
    net4 = layers.UpSampling1D(8)(net4)
    net5 = layers.UpSampling1D(16)(net5)
    net6 = layers.UpSampling1D(32)(net6)

    concat = layers.Add()([net1,net2,net3,net5,net6])
    concat = layers.Flatten()(concat)
    outputs = layers.Dense(1)(concat)

    model = tf.keras.Model(inputs, outputs)
    print(model.summary())
    return model
Beispiel #20
0
            layers.Dense(1, name='{}_pred'.format(param))(pre_pred))

    encoder = keras.Model([encoder_inputs] + param_inputs,
                          [z_mean, z_log_var, z] + y_predictions,
                          name='encoder')

    latent_inputs = keras.Input(shape=(latent_dim, ), name='z_sampling')
    x = layers.Dense(np.prod(intermediate_shape),
                     activation="relu")(latent_inputs)
    x = layers.Reshape(intermediate_shape)(x)
    x = layers.Conv1DTranspose(fil2,
                               10,
                               activation="relu",
                               strides=1,
                               padding="same")(x)
    x = layers.UpSampling1D(size=2)(x)
    x = layers.Conv1DTranspose(fil1,
                               3,
                               activation="relu",
                               strides=1,
                               padding="same")(x)
    x = layers.UpSampling1D(size=2)(x)
    decoder_outputs = layers.Conv1DTranspose(2,
                                             3,
                                             activation="sigmoid",
                                             padding="same")(x)
    decoder = keras.Model(latent_inputs, decoder_outputs, name="decoder")

else:

    ## CONV 2D Model
Beispiel #21
0
    def __init__(self,
                 filters,
                 kernel_size,
                 octave=2,
                 ratio_out=0.5,
                 strides=1,
                 dilation_rate=1,
                 activation=None,
                 use_bias=True,
                 kernel_initializer='glorot_uniform',
                 bias_initializer='zeros',
                 kernel_regularizer=None,
                 bias_regularizer=None,
                 activity_regularizer=None,
                 kernel_constraint=None,
                 bias_constraint=None,
                 **kwargs):
        super(OctaveConv1D, self).__init__(**kwargs)
        self.filters = filters
        self.kernel_size = kernel_size
        self.octave = octave
        self.ratio_out = ratio_out
        self.strides = strides
        self.dilation_rate = dilation_rate
        self.activation = activations.get(activation)
        self.use_bias = use_bias
        self.kernel_initializer = initializers.get(kernel_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)
        self.kernel_constraint = constraints.get(kernel_constraint)
        self.bias_constraint = constraints.get(bias_constraint)

        self.filters_low = int(filters * self.ratio_out)
        self.filters_high = filters - self.filters_low

        self.conv_high_to_high, self.conv_low_to_high = None, None
        if self.filters_high > 0:
            self.conv_high_to_high = self._init_conv(
                self.filters_high, name='{}-Conv1D-HH'.format(self.name))
            self.conv_low_to_high = self._init_conv(self.filters_high,
                                                    name='{}-Conv1D-LH'.format(
                                                        self.name))
        self.conv_low_to_low, self.conv_high_to_low = None, None
        if self.filters_low > 0:
            self.conv_low_to_low = self._init_conv(self.filters_low,
                                                   name='{}-Conv1D-HL'.format(
                                                       self.name))
            self.conv_high_to_low = self._init_conv(self.filters_low,
                                                    name='{}-Conv1D-LL'.format(
                                                        self.name))
        self.pooling = layers.AveragePooling1D(
            pool_size=self.octave,
            padding='valid',
            name='{}-AveragePooling1D'.format(self.name),
        )
        self.up_sampling = layers.UpSampling1D(
            size=self.octave,
            name='{}-UpSampling1D'.format(self.name),
        )
Beispiel #22
0
def octave_conv_1d(inputs,
                   filters,
                   kernel_size,
                   octave=2,
                   ratio_out=0.5,
                   strides=1,
                   dilation_rate=1,
                   activation=None,
                   use_bias=True,
                   kernel_initializer='glorot_uniform',
                   bias_initializer='zeros',
                   kernel_regularizer=None,
                   bias_regularizer=None,
                   activity_regularizer=None,
                   kernel_constraint=None,
                   bias_constraint=None,
                   name=None,
                   **kwargs):
    if isinstance(inputs, (list, tuple)):
        inputs_high, inputs_low = inputs
    else:
        inputs_high, inputs_low = inputs, None

    filters_low = int(filters * ratio_out)
    filters_high = filters - filters_low

    def _init_conv(conv_filters, conv_name_suffix):
        if name is None:
            conv_name = None
        else:
            conv_name = name + '-' + conv_name_suffix
        return layers.Conv1D(filters=conv_filters,
                             kernel_size=kernel_size,
                             strides=strides,
                             padding='same',
                             dilation_rate=dilation_rate,
                             activation=activation,
                             use_bias=use_bias,
                             kernel_initializer=kernel_initializer,
                             bias_initializer=bias_initializer,
                             kernel_regularizer=kernel_regularizer,
                             bias_regularizer=bias_regularizer,
                             activity_regularizer=activity_regularizer,
                             kernel_constraint=kernel_constraint,
                             bias_constraint=bias_constraint,
                             name=conv_name,
                             **kwargs)

    outputs_high = None
    if filters_high > 0:
        outputs_high = _init_conv(filters_high, 'HH')(inputs_high)
        if inputs_low is not None:
            if name is None:
                up_sampling_name, add_name = None, None
            else:
                up_sampling_name, add_name = name + '-UpSample', name + '-Add-H'
            outputs_high = layers.Add(name=add_name)([
                outputs_high,
                layers.UpSampling1D(
                    size=octave,
                    name=up_sampling_name,
                )(_init_conv(filters_high, 'LH')(inputs_low))
            ])

    outputs_low = None
    if filters_low > 0:
        if name is None:
            pooling_name, add_name = None, None
        else:
            pooling_name, add_name = name + '-Pool', name + '-Add-L'
        outputs_low = _init_conv(filters_low, 'HL')(layers.AveragePooling1D(
            pool_size=octave,
            padding='valid',
            name=pooling_name,
        )(inputs_high))
        if inputs_low is not None:
            outputs_low = layers.Add(name=add_name)(
                [_init_conv(filters_low, 'LL')(inputs_low), outputs_low])

    if outputs_high is None:
        return outputs_low
    if outputs_low is None:
        return outputs_high
    return [outputs_high, outputs_low]