예제 #1
0
    def __init__(self, config):
        logging.info("Model initialization")
        # Flags for considering social interations
        self.stack_rnn_size = config.stack_rnn_size
        self.output_samples = 2 * config.output_var_dirs + 1
        self.output_var_dirs = config.output_var_dirs

        #########################################################################################
        # The components of our model are instantiated here
        # Encoder: Positions and context
        self.enc = TrajectoryAndContextEncoder(config)
        self.enc.summary()
        # Encoder to decoder initialization
        self.enctodec = TrajectoryDecoderInitializer(config)
        self.enctodec.summary()
        # Decoder
        self.dec = DecoderOf(config)
        self.dec.summary()
        #########################################################################################

        # Optimization scheduling
        # lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
        #        config.initial_lr,
        #        decay_steps=100000,
        #        decay_rate=0.96,
        #        staircase=True)
        # Instantiate an optimizer to train the models.
        #self.optimizer = tf.keras.optimizers.Adadelta(learning_rate=lr_schedule)
        self.optimizer = tf.keras.optimizers.Adam(learning_rate=5e-5)
        # Instantiate the loss operator
        self.loss_fn = losses.LogCosh()
        self.loss_fn_local = losses.LogCosh(losses.Reduction.NONE)
예제 #2
0
def SLFNN(vst_onlyTokens,
          dl_terms,
          dl_associations,
          vso,
          nbEpochs=100,
          batchSize=64):

    vstTerm, l_unknownToken = word2term.wordVST2TermVST(
        vst_onlyTokens, dl_terms)
    data, labels = getMatrix(dl_terms,
                             vstTerm,
                             dl_associations,
                             vso,
                             symbol="___")

    inputSize = data.shape[1]
    ontoSpaceSize = labels.shape[1]

    model = models.Sequential()
    model.add(
        layers.Dense(units=ontoSpaceSize,
                     use_bias=True,
                     kernel_initializer=initializers.GlorotUniform(),
                     input_shape=(inputSize, )))
    model.summary()

    model.compile(
        optimizer=optimizers.Nadam(),
        loss=losses.LogCosh(),
        metrics=[metrics.CosineSimilarity(),
                 metrics.MeanSquaredError()])
    model.fit(data, labels, epochs=nbEpochs, batch_size=batchSize)

    return model, vso, l_unknownToken
 def __init__(self, config):
     super(PredictorDetRNN, self).__init__()
     self.rnn_type = config.rnn_type
     # Layers
     self.embedding = layers.Dense(config.emb_size,
                                   activation=config.activation_func)
     if self.rnn_type == "lstm":
         logging.info("Using LSTM RNNs")
         self.rnn = layers.LSTM(config.enc_hidden_size,
                                return_sequences=False,
                                return_state=True,
                                activation='tanh',
                                dropout=config.dropout_rate)
     else:
         logging.info("Using GRU RNNs")
         self.rnn = layers.GRU(config.enc_hidden_size,
                               return_sequences=False,
                               return_state=True,
                               activation='tanh',
                               dropout=config.dropout_rate)
     self.dropout = layers.Dropout(config.dropout_rate,
                                   name="dropout_decode")
     self.decoder = layers.Dense(config.P, activation=tf.identity)
     # Loss function
     self.loss_fun = losses.LogCosh()
예제 #4
0
def SCNN(vst_onlyTokens,
         dl_terms,
         dl_associations,
         vso,
         nbEpochs=150,
         batchSize=64,
         l_numberOfFilters=[4000],
         l_filterSizes=[1],
         phraseMaxSize=15):

    data, labels, l_unkownTokens, l_uncompleteExpressions = prepare2D_data(
        vst_onlyTokens, dl_terms, dl_associations, vso, phraseMaxSize)

    embeddingSize = data.shape[2]
    ontoSpaceSize = labels.shape[2]

    inputLayer = Input(shape=(phraseMaxSize, embeddingSize))

    l_subLayers = list()
    for i, filterSize in enumerate(l_filterSizes):

        convLayer = (layers.Conv1D(
            l_numberOfFilters[i],
            filterSize,
            strides=1,
            kernel_initializer=initializers.GlorotUniform()))(inputLayer)

        outputSize = phraseMaxSize - filterSize + 1
        pool = (layers.MaxPool1D(pool_size=outputSize))(convLayer)

        activationLayer = (layers.LeakyReLU(alpha=0.3))(pool)

        l_subLayers.append(activationLayer)

    if len(l_filterSizes) > 1:
        concatenateLayer = (layers.Concatenate(axis=-1))(
            l_subLayers)  # axis=-1 // concatenating on the last dimension
    else:
        concatenateLayer = l_subLayers[0]

    convModel = Model(inputs=inputLayer, outputs=concatenateLayer)
    fullmodel = models.Sequential()
    fullmodel.add(convModel)

    fullmodel.add(
        layers.Dense(ontoSpaceSize,
                     kernel_initializer=initializers.GlorotUniform()))

    fullmodel.summary()
    fullmodel.compile(
        optimizer=optimizers.Nadam(),
        loss=losses.LogCosh(),
        metrics=[metrics.CosineSimilarity(),
                 metrics.MeanSquaredError()])
    fullmodel.fit(data, labels, epochs=nbEpochs, batch_size=batchSize)

    return fullmodel, vso, l_unkownTokens
예제 #5
0
def CNorm(vst_onlyTokens,
          dl_terms,
          dl_associations,
          vso,
          nbEpochs=30,
          batchSize=64,
          l_numberOfFilters=[4000],
          l_filterSizes=[1],
          phraseMaxSize=15):

    # Preparing data for SLFNN and S-CNN components:
    dataSCNN, labels, l_unkownTokens, l_uncompleteExpressions = prepare2D_data(
        vst_onlyTokens, dl_terms, dl_associations, vso, phraseMaxSize)
    dataSLFNN = numpy.zeros((dataSCNN.shape[0], dataSCNN.shape[2]))
    for i in range(dataSCNN.shape[0]):
        numberOfToken = 0
        for embedding in dataSCNN[i]:
            if not numpy.any(embedding):
                pass
            else:
                numberOfToken += 1
                dataSLFNN[i] += embedding

        if numberOfToken > 0:
            dataSLFNN[i] = dataSLFNN[i] / numberOfToken

    # Input layers:
    inputLP = Input(shape=dataSLFNN.shape[1])
    inputCNN = Input(shape=[dataSCNN.shape[1], dataSCNN.shape[2]])

    # SLFNN component:
    ontoSpaceSize = labels.shape[2]
    denseLP = layers.Dense(
        units=ontoSpaceSize,
        use_bias=True,
        kernel_initializer=initializers.GlorotUniform())(inputLP)
    modelLP = Model(inputs=inputLP, outputs=denseLP)

    # Shallow-CNN component:
    l_subLayers = list()
    for i, filterSize in enumerate(l_filterSizes):

        convLayer = (layers.Conv1D(
            l_numberOfFilters[i],
            filterSize,
            strides=1,
            kernel_initializer=initializers.GlorotUniform()))(inputCNN)

        outputSize = phraseMaxSize - filterSize + 1
        pool = (layers.MaxPool1D(pool_size=outputSize))(convLayer)

        activationLayer = (layers.LeakyReLU(alpha=0.3))(pool)

        l_subLayers.append(activationLayer)

    if len(l_filterSizes) > 1:
        concatenateLayer = (layers.Concatenate(axis=-1))(
            l_subLayers)  # axis=-1 // concatenating on the last dimension
    else:
        concatenateLayer = l_subLayers[0]

    denseLayer = layers.Dense(
        ontoSpaceSize,
        kernel_initializer=initializers.GlorotUniform())(concatenateLayer)
    modelCNN = Model(inputs=inputCNN, outputs=denseLayer)

    convModel = Model(inputs=inputCNN, outputs=concatenateLayer)
    fullmodel = models.Sequential()
    fullmodel.add(convModel)

    # Combination of the two components:
    combinedLayer = layers.average([modelLP.output, modelCNN.output])
    fullModel = Model(inputs=[inputLP, inputCNN], outputs=combinedLayer)
    fullModel.summary()

    # Compile and train:
    fullModel.compile(
        optimizer=optimizers.Nadam(),
        loss=losses.LogCosh(),
        metrics=[metrics.CosineSimilarity(),
                 metrics.MeanSquaredError()])
    fullModel.fit([dataSLFNN, dataSCNN],
                  labels,
                  epochs=nbEpochs,
                  batch_size=batchSize)

    return fullModel, vso, l_unkownTokens