def get_model(input_shape, intermediate_dim, latent_dim):
    # VAE model = encoder + decoder
    # build encoder model
    inputs = Input(shape=input_shape, name='encoder_input')
    x = Conv2D(64, (2, 2), activation=relu)(inputs)
    x = Conv2D(64, (3, 3), activation=relu)(x)
    x = Conv2D(64, (3, 3), activation=relu)(x)
    x = Flatten()(x)
    z_mean = Dense(latent_dim, name='z_mean')(x)
    z_log_var = Dense(latent_dim, name='z_log_var')(x)

    # use reparameterization trick to push the sampling out as input
    # note that "output_shape" isn't necessary with the TensorFlow backend
    z = Lambda(sampling, output_shape=(latent_dim, ),
               name='z')([z_mean, z_log_var])

    # instantiate encoder model
    encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')
    encoder.summary()
    # plot_model(encoder, to_file='vae_mlp_encoder.png', show_shapes=True)

    # build decoder model
    latent_inputs = Input(shape=(latent_dim, ), name='z_sampling')
    x = Dense(intermediate_dim, activation=relu)(latent_inputs)
    x = Dense(64 * 28 * 28, activation=relu)(x)
    x = Reshape((28, 28, 64))(x)
    x = Conv2DTranspose(64, (3, 3), activation=sigmoid, padding='same')(x)
    x = Conv2DTranspose(64, (3, 3), activation=sigmoid, padding='same')(x)
    outputs = Conv2D(image_channels, (2, 2), padding='same')(x)

    # instantiate decoder model
    decoder = Model(latent_inputs, outputs, name='decoder')
    decoder.summary()
    # plot_model(decoder, to_file='vae_mlp_decoder.png', show_shapes=True)

    # instantiate VAE model
    outputs = decoder(encoder(inputs)[2])
    vae = Model(inputs, outputs, name='vae_mlp')
    return vae, encoder, decoder, inputs, z_mean, z_log_var, latent_inputs, outputs
def inception_resnet_block(x, scale, block_type, block_idx, activation='relu'):
  """Adds a Inception-ResNet block.

  This function builds 3 types of Inception-ResNet blocks mentioned
  in the paper, controlled by the `block_type` argument (which is the
  block name used in the official TF-slim implementation):
      - Inception-ResNet-A: `block_type='block35'`
      - Inception-ResNet-B: `block_type='block17'`
      - Inception-ResNet-C: `block_type='block8'`

  Arguments:
      x: input tensor.
      scale: scaling factor to scale the residuals (i.e., the output of
          passing `x` through an inception module) before adding them
          to the shortcut branch. Let `r` be the output from the residual
            branch,
          the output of this block will be `x + scale * r`.
      block_type: `'block35'`, `'block17'` or `'block8'`, determines
          the network structure in the residual branch.
      block_idx: an `int` used for generating layer names. The Inception-ResNet
        blocks
          are repeated many times in this network. We use `block_idx` to
            identify
          each of the repetitions. For example, the first Inception-ResNet-A
            block
          will have `block_type='block35', block_idx=0`, ane the layer names
            will have
          a common prefix `'block35_0'`.
      activation: activation function to use at the end of the block.
          When `activation=None`, no activation is applied
          (i.e., "linear" activation: `a(x) = x`).

  Returns:
      Output tensor for the block.

  Raises:
      ValueError: if `block_type` is not one of `'block35'`,
          `'block17'` or `'block8'`.
  """
  if block_type == 'block35':
    branch_0 = conv2d_bn(x, 32, 1)
    branch_1 = conv2d_bn(x, 32, 1)
    branch_1 = conv2d_bn(branch_1, 32, 3)
    branch_2 = conv2d_bn(x, 32, 1)
    branch_2 = conv2d_bn(branch_2, 48, 3)
    branch_2 = conv2d_bn(branch_2, 64, 3)
    branches = [branch_0, branch_1, branch_2]
  elif block_type == 'block17':
    branch_0 = conv2d_bn(x, 192, 1)
    branch_1 = conv2d_bn(x, 128, 1)
    branch_1 = conv2d_bn(branch_1, 160, [1, 7])
    branch_1 = conv2d_bn(branch_1, 192, [7, 1])
    branches = [branch_0, branch_1]
  elif block_type == 'block8':
    branch_0 = conv2d_bn(x, 192, 1)
    branch_1 = conv2d_bn(x, 192, 1)
    branch_1 = conv2d_bn(branch_1, 224, [1, 3])
    branch_1 = conv2d_bn(branch_1, 256, [3, 1])
    branches = [branch_0, branch_1]
  else:
    raise ValueError('Unknown Inception-ResNet block type. '
                     'Expects "block35", "block17" or "block8", '
                     'but got: ' + str(block_type))

  block_name = block_type + '_' + str(block_idx)
  channel_axis = 1 if K.image_data_format() == 'channels_first' else 3
  mixed = Concatenate(axis=channel_axis, name=block_name + '_mixed')(branches)
  up = conv2d_bn(
      mixed,
      K.int_shape(x)[channel_axis],
      1,
      activation=None,
      use_bias=True,
      name=block_name + '_conv')

  x = Lambda(
      lambda inputs, scale: inputs[0] + inputs[1] * scale,
      output_shape=K.int_shape(x)[1:],
      arguments={'scale': scale},
      name=block_name)([x, up])
  if activation is not None:
    x = Activation(activation, name=block_name + '_ac')(x)
  return x
Ejemplo n.º 3
0
def get_item_embedding(item_embedding_layer, item_input_layer):
    return Lambda(
        lambda x: tf.squeeze(tf.gather(item_embedding_layer.embeddings, x),
                             axis=1))(item_input_layer)
    def get_model(self, numpy_matrix_embeddings: numpy.ndarray,
                  **kwargs) -> Model:
        # get default parameters
        dropout = kwargs.get('dropout', 0.9)
        assert isinstance(dropout, float)
        lstm_layer_size = kwargs.get('lstm_layer_size', 64)
        assert isinstance(lstm_layer_size, int)

        # we also need the full input length
        # nope, we don't need it :)
        # max_input_length = kwargs.get('max_input_length')
        # assert max_input_length

        # this is the standard placeholder tensor for the input sequences; a numpy array of word indices
        # input_sequence = Input(shape=(max_input_length,), dtype='int32', name='input_sequence')
        input_sequence = Input(shape=(None, ),
                               dtype='int32',
                               name='input_sequence')
        print("Embeddings shape:", numpy_matrix_embeddings.shape)

        # The embedding layer will transform the sequences of integers into vectors of size dimension of embeddings
        # We also fix the embeddings (not trainable)
        #
        # For this model, we have to disable masking as the Reshape layers do not support it
        embedded = Embedding(numpy_matrix_embeddings.shape[0],
                             numpy_matrix_embeddings.shape[1],
                             weights=[numpy_matrix_embeddings],
                             mask_zero=True,
                             name='embeddings',
                             trainable=False)(input_sequence)

        # bidirectional LSTM using the neat Keras wrapper
        lstm_output = Bidirectional(LSTM(lstm_layer_size,
                                         return_sequences=True),
                                    name="BiLSTM")(embedded)

        # matrix H
        # maybe we don't need to reshape? -- nope, we don't need to reshape :)
        # matrix_h = Reshape((max_input_length, lstm_layer_size * 2), name='H_matrix')(lstm_output)
        matrix_h = lstm_output

        # matrix H transposed; we don't need it, it can be transposed in matmul later on
        # matrix_h_t = Lambda(lambda x: tensorflow.transpose(x, perm=[0, 2, 1]), name='HT_matrix')(matrix_h)

        # 150 was default
        param_da = 300

        output_tanh_ws1_ht = Dense(units=param_da,
                                   activation='tanh',
                                   use_bias=False,
                                   name='tanh_Ws1_HT')(matrix_h)

        # 30 was ok
        param_r = 50

        annotation_matrix_a_linear = Dense(units=param_r,
                                           activation='linear',
                                           use_bias=False,
                                           name='A_matrix')(output_tanh_ws1_ht)

        # 0.1 was ok too
        # the longer the coefficient, the more only blocks at the beginning are shown
        # penalization_coefficient = 0.5
        # penalization_coefficient = 1
        # this kills the performance :(
        # so without penalization we get to almost 80 percent!! :)
        penalization_coefficient = 0

        # now do the softmax over rows, not the columns
        annotation_matrix_a = Lambda(
            lambda x: tensorflow.nn.softmax(x, dim=1),
            name='A_softmax',
            activity_regularizer=PRegularizer(penalization_coefficient))(
                annotation_matrix_a_linear)

        # multiplication: easier to use Lambda than Multiply
        matrix_m = Lambda(
            lambda x: tensorflow.matmul(x[0], x[1], transpose_a=True),
            name='M_matrix')([annotation_matrix_a, matrix_h])

        # and flatten
        dense_representation = Flatten()(matrix_m)

        # classification
        output = Dense(2, activation='softmax',
                       name="Output_dense")(dense_representation)
        model = Model(inputs=[input_sequence], outputs=output)

        print_summary(model)

        model.compile('adam', loss=categorical_crossentropy)
        print("Model compiled")

        debug_graph = False
        if debug_graph:
            from tensorflow.python.keras.utils import plot_model
            plot_model(model, to_file='/tmp/model.png', show_shapes=True)

        return model