Example #1
0
def ha(args):
    len_local, neighbor_size = args['len_local'], args['neighbor_size']

    main_input = []
    neighbor_slide_len = neighbor_size * 2 + 1

    stack_local_flow = \
        Input(shape=(len_local * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='stack_local_flow')
    main_input.append(stack_local_flow)

    # merge layer ask the number greater than 1
    if len_local == 1:
        output = main_input
        model = Model(inputs=main_input, outputs=output, name='HA')
        return model

    # average the stack

    def slice(inputs, channel):
        return inputs[:, channel:channel+1, :, :]

    outflow_list = []
    inflow_list = []

    for i in range(len_local):
        inflow_list.append(Lambda(slice, arguments={'channel':2*i})(stack_local_flow))
        outflow_list.append(Lambda(slice, arguments={'channel': 2*i+1})(stack_local_flow))

    inflow_ave = merge.Average()(inflow_list)
    outflow_ave = merge.Average()(outflow_list)

    output = merge.Concatenate(axis=1)([inflow_ave, outflow_ave])

    model = Model(inputs=main_input, outputs=output, name='HA')
    return model
Example #2
0
    def __init__(self, hparams, fl):
        """
        Initialises new linear SVM model based on input features_dim, labels_dim, hparams
        :param features_dim: Number of input feature nodes. Integer
        :param labels_dim: Number of output label nodes. Integer
        :param hparams: Dict containing hyperparameter information. Dict can be created using create_hparams() function.
        hparams includes: hidden_layers: List containing number of nodes in each hidden layer. [10, 20] means 10 then 20 nodes.
        """
        self.features_dim = fl.features_c_dim
        self.features_d_count = fl.features_d_count
        self.labels_dim = fl.n_classes
        self.hparams = hparams
        self.fp_length = [
            hparams['fp_length'] for _ in range(hparams['fp_number'])
        ]
        self.conv_width = [
            hparams['conv_width'] for _ in range(hparams['conv_number'])
        ]

        # Left side
        lc = Input(shape=(fl.features_c_dim, ))
        left_features_d = []
        left_conv_net = []
        for idx in range(fl.features_d_count):
            # Creating left input tensors for features_d.
            # For each molecule
            # Make one input tensor for atoms, bond, edge tensor
            left_features_d.append([
                Input(name='l_a_inputs_' + str(idx) + 'x',
                      shape=fl.features_d_a[idx][0].shape[1:]),
                Input(name='l_b_inputs_' + str(idx) + 'y',
                      shape=fl.features_d_a[idx][1].shape[1:]),
                Input(name='l_e_inputs_' + str(idx) + 'z',
                      shape=fl.features_d_a[idx][2].shape[1:],
                      dtype='int32')
            ])
            # Building the left_conv_net for that particular molecule
            left_conv_net.append(
                build_graph_conv_net_fp_only(left_features_d[idx],
                                             conv_layer_sizes=self.conv_width,
                                             fp_layer_size=self.fp_length,
                                             conv_activation='relu',
                                             fp_activation='softmax'))
        # Concat left side 4 inputs. Unlike SNN, there is no right side.
        left_combined = merge.Concatenate()([lc] + left_conv_net)

        # Input concat vector into SVM
        prediction = Dense(2, activation='linear')(left_combined)

        self.model = Model(input=[lc] + [
            left_tensor for molecule in left_features_d
            for left_tensor in molecule
        ],
                           output=prediction)

        # SVM uses categorical hinge loss
        self.model.compile(optimizer='Adam',
                           loss="categorical_hinge",
                           metrics=['accuracy'])
Example #3
0
def geo_lprnn_text_model(user_dim,
                         len,
                         place_dim=GRID_COUNT * GRID_COUNT,
                         time_dim=config.time_dim,
                         pl_d=config.pl_d,
                         time_k=config.time_k,
                         hidden_neurons=config.hidden_neurons,
                         learning_rate=config.learning_rate):
    # RNN model construction
    pl_input = Input(shape=(len - 1, ), dtype='int32', name='pl_input')
    time_input = Input(shape=(len - 1, ), dtype='int32', name='time_input')
    user_input = Input(shape=(len - 1, ), dtype='int32', name='user_input')
    text_input = Input(shape=(len - 1, pl_d),
                       dtype='float32',
                       name='text_input')

    pl_embedding = Embedding(input_dim=place_dim + 1,
                             output_dim=pl_d,
                             name='pl_embedding',
                             mask_zero=True)(pl_input)
    time_embedding = Embedding(input_dim=time_dim + 1,
                               output_dim=time_k,
                               name='time_embedding',
                               mask_zero=True)(time_input)
    user_embedding = Embedding(input_dim=user_dim + 1,
                               output_dim=place_dim + 1,
                               name='user_embedding',
                               mask_zero=True)(user_input)
    # text_embedding = Dense(pl_d)(text_input)

    attrs_latent = merge.Concatenate(
        [pl_embedding, time_embedding, text_input]
    )  # attrs_latent = merge([pl_embedding,time_embedding, text_input],mode='concat')
    # time_dist = TimeDistributed(Dense(50))
    lstm_out = LSTM(hidden_neurons, return_sequences=True,
                    name='lstm_layer')(attrs_latent)
    dense = Dense(place_dim + 1, name='dense')(lstm_out)
    out_vec = merge.Add(
        [dense,
         user_embedding])  # out_vec = merge([dense,user_embedding],mode='sum')
    pred = Activation('softmax')(out_vec)
    model = Model([pl_input, time_input, user_input, text_input], pred)

    # model.load_weights('./model/User_RNN_Seg_Epoch_0.3_rmsprop_300.h5')
    # Optimization
    sgd = optimizers.SGD(lr=learning_rate)
    rmsprop = optimizers.RMSprop(lr=learning_rate)
    model.compile(optimizer=rmsprop, loss='categorical_crossentropy')
    model.summary()
    return model
Example #4
0
def build_model(tokenizer):
    embedding_model = word2vec.Word2Vec.load(
        'D:/TSE/python/missplaceclass/embedding_model/new_model_1.bin')
    word_index = tokenizer.word_index
    nb_words = len(word_index)
    embedding_matrix = np.zeros((nb_words + 1, EMBEDDING_DIM))
    for word, i in word_index.items():
        if word == 'false':
            print(word)
        embedding_vector = embedding_model.wv[word]
        if embedding_vector is not None:
            embedding_matrix[i] = embedding_vector
    models = []
    for i in range(MODEL_NUMBER):
        embedding_layer = Embedding(nb_words + 1,
                                    EMBEDDING_DIM,
                                    input_length=MAX_SEQUENCE_LENGTH,
                                    weights=[embedding_matrix],
                                    trainable=False)
        model_left = Sequential()
        model_left.add(embedding_layer)
        model_left.add(Conv1D(128, 1, padding="same", activation='tanh'))
        model_left.add(Conv1D(128, 1, activation='tanh'))
        model_left.add(Conv1D(128, 1, activation='tanh'))
        model_left.add(Flatten())

        model_right = Sequential()
        model_right.add(
            Conv1D(128,
                   1,
                   input_shape=(8, 1),
                   padding="same",
                   activation='tanh'))
        model_right.add(Conv1D(128, 1, activation='tanh'))
        model_right.add(Conv1D(128, 1, activation='tanh'))
        model_right.add(Flatten())

        output = merge.Concatenate()([model_left.output, model_right.output])
        output = Dense(128, activation='tanh')(output)
        output = Dense(1, activation='sigmoid')(output)
        input_left = model_left.input
        input_right = model_right.input

        model = Model([input_left, input_right], output)
        model.compile(loss='binary_crossentropy',
                      optimizer='Adadelta',
                      metrics=['accuracy'])
        models.append(model)
    return models
    def _build_big_conv_nn(self, optimizer="adam"):
        """
        Defines and compiles same CNN as J. Saxe et al. - eXpose: A Character-Level Convolutional Neural Network with
        Embeddings For Detecting Malicious URLs, File Paths and Registry Keys.

        Parameters
        ----------
        optimizer:
            Optimizer algorithm
        """
        main_input = Input(shape=(self.max_length, ),
                           dtype='int32',
                           name='main_input')
        embedding = Embedding(input_dim=self.vocab_size,
                              output_dim=32,
                              input_length=self.max_length,
                              dropout=0)(main_input)

        conv1 = self._get_complete_conv_layer(2, 256)(embedding)
        conv2 = self._get_complete_conv_layer(3, 256)(embedding)
        conv3 = self._get_complete_conv_layer(4, 256)(embedding)
        conv4 = self._get_complete_conv_layer(5, 256)(embedding)

        merged = merge.Concatenate()([conv1, conv2, conv3, conv4])
        merged = BatchNormalization()(merged)

        middle = Dense(1024, activation='relu')(merged)
        middle = BatchNormalization()(middle)
        middle = Dropout(0.5)(middle)

        middle = Dense(1024, activation='relu')(middle)
        middle = BatchNormalization()(middle)
        middle = Dropout(0.5)(middle)

        middle = Dense(1024, activation='relu')(middle)
        middle = BatchNormalization()(middle)
        middle = Dropout(0.5)(middle)

        output = Dense(1, activation='sigmoid')(middle)

        self.model = Model(input=main_input, output=output)
        self.model.compile(loss='binary_crossentropy',
                           optimizer=optimizer,
                           metrics=['acc', self.f1])
        print(self.model.summary())
    def build_cell(self, modeltype):
        """
        build lstm or gru cell
        """
        if modeltype == 'lstm':
            # kernel_initializer='glorot_uniform', recurrent_initializer='orthogonal', bias_initializer='zeros',
            # not zeros
            # rnn_out_f = LSTM(self.hidden_layer_size, input_shape=(self.length ,self.n_features), use_bias=True, activation=self.activation, dropout=self.output_dropout, recurrent_dropout=self.input_dropout,                  )(self.feature)
            # rnn_out_b = LSTM(self.hidden_layer_size, input_shape=(self.length ,self.n_features), use_bias=True, activation=self.activation, dropout=self.output_dropout, recurrent_dropout=self.input_dropout, go_backwards=True)(self.feature)

            #zeros failing spontaneously.
            rnn_out_f = CuDNNLSTM(
                self.hidden_layer_size,
                input_shape=(self.length, self.n_features),
            )(self.feature)
            rnn_out_b = CuDNNLSTM(self.hidden_layer_size,
                                  input_shape=(self.length, self.n_features),
                                  go_backwards=True)(self.feature)

            #zeros
            # rnn_cell=tf.contrib.cudnn_rnn.CudnnCompatibleLSTMCell(self.hidden_layer_size)
            # rnn_cell_bw=tf.contrib.cudnn_rnn.CudnnCompatibleLSTMCell(self.hidden_layer_size)

            #zeros
            # rnn_cell=tf.contrib.cudnn_rnn.CudnnLSTM(1, self.hidden_layer_size, dropout=self.output_dropout, kernel_initializer=tf.glorot_uniform_initializer(), bias_initializer='zeros', direction='bidirectional')
            # rnn_cell_bw=tf.contrib.cudnn_rnn.CudnnLSTM(1, self.hidden_layer_size, dropout=self.output_dropout, kernel_initializer=tf.glorot_uniform_initializer(), bias_initializer='zeros', direction='bidirectional')

            # rnn_cell=tf.keras.layers.LSTMCell(self.hidden_layer_size, activation=self.activation)
        elif modeltype == 'gru':
            rnn_out_f = CuDNNGRU(
                self.hidden_layer_size,
                input_shape=(self.length, self.n_features),
            )(self.feature)
            rnn_out_b = CuDNNGRU(self.hidden_layer_size,
                                 input_shape=(self.length, self.n_features),
                                 go_backwards=True)(self.feature)
        else:
            raise ('Unknown modeltype for RNN')

        # self.rnn_cell=tf.nn.rnn_cell.DropoutWrapper(rnn_cell, input_keep_prob=1.0-self.input_dropout, output_keep_prob=1.0-self.output_dropout)
        # self.rnn_cell_bw=tf.nn.rnn_cell.DropoutWrapper(rnn_cell_bw, input_keep_prob=1.0-self.input_dropout, output_keep_prob=1.0-self.output_dropout)

        self.rnn_cell = merge.Concatenate(axis=-1)([rnn_out_f, rnn_out_b])
        self.rnn_cell = Dropout(self.output_dropout)(self.rnn_cell)
def Spatial_attention(x, name, kernel_size=7):
    assert kernel_size in (3, 7), 'kernel_size must be 3 or 7'
    # padding = 3 if kernel_size==7 else 1

    avg_out = Lambda(lambda y: K.mean(y, axis=3, keepdims=True),
                     name=f'{name}_avgpool')(x)  # (B, W, H, 1)
    max_out = Lambda(lambda y: K.max(x, axis=3, keepdims=True),
                     name=f'{name}_maxpool')(x)  # (B, W, H, 1)

    out = merge.Concatenate(name=f'{name}_concat')([avg_out,
                                                    max_out])  # (B, W, H, 2)

    out = Conv2D(1,
                 kernel_size,
                 padding='same',
                 name=f'{name}_conv',
                 activation='sigmoid')(out)

    x = Multiply(name=f'{name}_mul')([out, x])

    return x
def build_model():
    GLOVE_STORE = 'nli_precomputed_glove.weights'
    if USE_GLOVE:
        if not os.path.exists(GLOVE_STORE + '.npy'):
            print('Computing GloVe')
            embeddings_index = {}
            f = open(GLOVE_FILE_PATH)
            for line in f:
                values = line.split(' ')
                word = values[0]
                coefs = np.asarray(values[1:], dtype='float32')
                embeddings_index[word] = coefs
            f.close()
            # prepare embedding matrix
            embedding_matrix = np.zeros((VOCAB, EMBED_HIDDEN_SIZE))
            for word, i in tokenizer.word_index.items():
                embedding_vector = embeddings_index.get(word)
                if embedding_vector is not None:
                    # words not found in embedding index will be all-zeros.
                    embedding_matrix[i] = embedding_vector
                else:
                    print('Missing from GloVe: {}'.format(word))
            np.save(GLOVE_STORE, embedding_matrix)
        print('Loading GloVe')
        embedding_matrix = np.load(GLOVE_STORE + '.npy')
        print('Total number of null word embeddings:')
        print(np.sum(np.sum(embedding_matrix, axis=1) == 0))
        embed = Embedding(VOCAB,
                          EMBED_HIDDEN_SIZE,
                          weights=[embedding_matrix],
                          input_length=MAX_LEN,
                          trainable=TRAIN_EMBED)
    else:
        embed = Embedding(VOCAB, EMBED_HIDDEN_SIZE, input_length=MAX_LEN)
    rnn_kwargs = dict(output_dim=SENT_HIDDEN_SIZE, dropout_W=DP, dropout_U=DP)
    SumEmbeddings = keras.layers.core.Lambda(lambda x: K.sum(x, axis=1),
                                             output_shape=(SENT_HIDDEN_SIZE, ))
    translate = TimeDistributed(Dense(SENT_HIDDEN_SIZE, activation=ACTIVATION))

    premise = Input(shape=(MAX_LEN, ), dtype='int32')
    hypothesis = Input(shape=(MAX_LEN, ), dtype='int32')
    prem = embed(premise)
    hypo = embed(hypothesis)
    prem = translate(prem)
    hypo = translate(hypo)
    if RNN and LAYERS > 1:
        for l in range(LAYERS - 1):
            rnn = RNN(return_sequences=True, **rnn_kwargs)
            prem = BatchNormalization()(rnn(prem))
            hypo = BatchNormalization()(rnn(hypo))
    rnn = SumEmbeddings if not RNN else RNN(return_sequences=False,
                                            **rnn_kwargs)
    prem = rnn(prem)
    hypo = rnn(hypo)
    prem = BatchNormalization()(prem)
    hypo = BatchNormalization()(hypo)
    joint = merge.Concatenate()([prem, hypo])
    joint = Dropout(DP)(joint)
    for i in range(3):
        joint = Dense(2 * SENT_HIDDEN_SIZE,
                      activation=ACTIVATION,
                      W_regularizer=l2(L2) if L2 else None)(joint)
        joint = Dropout(DP)(joint)
        joint = BatchNormalization()(joint)

    pred = Dense(len(LABELS), activation='softmax')(joint)
    model = Model(input=[premise, hypothesis], output=pred)
    model.compile(optimizer=OPTIMIZER,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    return model
Example #9
0
    def __init__(self, SNN_hparams, fl):
        self.hparams = SNN_hparams
        self.features_c_dim = fl.features_c_dim
        self.features_d_count = fl.features_d_count
        self.fp_length = [
            SNN_hparams['fp_length'] for _ in range(SNN_hparams['fp_number'])
        ]
        self.conv_width = [
            SNN_hparams['conv_width']
            for _ in range(SNN_hparams['conv_number'])
        ]
        # Left side
        lc = Input(shape=(fl.features_c_dim, ))
        left_features_d = []
        left_conv_net = []
        for idx in range(fl.features_d_count):
            # Creating left input tensors for features_d.
            # For each molecule
            # Make one input tensor for atoms, bond, edge tensor
            left_features_d.append([
                Input(name='l_a_inputs_' + str(idx) + 'x',
                      shape=fl.features_d_a[idx][0].shape[1:]),
                Input(name='l_b_inputs_' + str(idx) + 'y',
                      shape=fl.features_d_a[idx][1].shape[1:]),
                Input(name='l_e_inputs_' + str(idx) + 'z',
                      shape=fl.features_d_a[idx][2].shape[1:],
                      dtype='int32')
            ])
            # Building the left_conv_net for that particular molecule
            left_conv_net.append(
                build_graph_conv_net_fp_only(left_features_d[idx],
                                             conv_layer_sizes=self.conv_width,
                                             fp_layer_size=self.fp_length,
                                             conv_activation='relu',
                                             fp_activation='softmax'))

        # Right side
        rc = Input(shape=(fl.features_c_dim, ))
        right_features_d = []
        right_conv_net = []
        for idx in range(fl.features_d_count):
            # Same as left side. Just change left to right.
            right_features_d.append([
                Input(name='r_a_inputs_' + str(idx) + 'x',
                      shape=fl.features_d_a[idx][0].shape[1:]),
                Input(name='r_b_inputs_' + str(idx) + 'y',
                      shape=fl.features_d_a[idx][1].shape[1:]),
                Input(name='r_e_inputs_' + str(idx) + 'z',
                      shape=fl.features_d_a[idx][2].shape[1:],
                      dtype='int32')
            ])
            right_conv_net.append(
                build_graph_conv_net_fp_only(right_features_d[idx],
                                             conv_layer_sizes=self.conv_width,
                                             fp_layer_size=self.fp_length,
                                             conv_activation='relu',
                                             fp_activation='softmax'))

        # Concat left side 4 inputs and right side 4 inputs
        left_combined = merge.Concatenate()([lc] + left_conv_net)
        right_combined = merge.Concatenate()([rc] + right_conv_net)

        encoded_l = self.singlenet()(left_combined)
        encoded_r = self.singlenet()(right_combined)

        # layer to merge two encoded inputs with the l1 distance between them
        L1_layer = Lambda(lambda tensors: K.abs(tensors[0] - tensors[1]))
        # call this layer on list of two input tensors.
        L1_distance = L1_layer([encoded_l, encoded_r])

        prediction = Dense(1, activation='sigmoid')(L1_distance)
        self.siamese_net = Model(input=[lc] + [
            left_tensor for molecule in left_features_d
            for left_tensor in molecule
        ] + [rc] + [
            right_tensor for molecule in right_features_d
            for right_tensor in molecule
        ],
                                 output=prediction)

        if self.hparams.get('learning_rate', None) is None:
            self.siamese_net.compile(loss='binary_crossentropy',
                                     optimizer=self.hparams['optimizer'])
        else:
            sgd = optimizers.Adam(lr=self.hparams['learning_rate'])
            self.siamese_net.compile(loss='binary_crossentropy', optimizer=sgd)
Example #10
0
    def __init__(self, SNN_hparams, fl):
        self.hparams = SNN_hparams
        self.features_c_dim = fl.features_c_dim
        self.features_d_count = fl.features_d_count
        self.fp_length = [
            SNN_hparams['fp_length'] for _ in range(SNN_hparams['fp_number'])
        ]
        self.conv_width = [
            SNN_hparams['conv_width']
            for _ in range(SNN_hparams['conv_number'])
        ]

        # First step: SMILES ==> fingerprint vector
        # Defining fingerprint(fp) model
        half_features_d = []
        half_conv_net = []
        for idx in range(fl.features_d_count):
            # Creating left input tensors for features_d.
            # For each molecule
            # Make one input tensor for atoms, bond, edge tensor
            half_features_d.append([
                Input(name='h_a_inputs_' + str(idx) + 'x',
                      shape=fl.features_d_a[idx][0].shape[1:]),
                Input(name='h_b_inputs_' + str(idx) + 'y',
                      shape=fl.features_d_a[idx][1].shape[1:]),
                Input(name='h_e_inputs_' + str(idx) + 'z',
                      shape=fl.features_d_a[idx][2].shape[1:],
                      dtype='int32')
            ])
            single_molecule = half_features_d[-1]
            # Building the half_conv_net for that particular molecule
            single_molecule_half_conv_net = build_graph_conv_net_fp_only(
                single_molecule,
                conv_layer_sizes=self.conv_width,
                fp_layer_size=self.fp_length,
                conv_activation=self.hparams['conv_activation'],
                conv_l1=self.hparams['conv_l1'],
                conv_l2=self.hparams['conv_l2'],
                fp_activation='softmax')
            single_molecule_half_conv_net_model = Model(
                name='h_fp_' + str(idx),
                inputs=single_molecule,
                outputs=single_molecule_half_conv_net)
            half_conv_net.append(single_molecule_half_conv_net_model)

        # Second step: Concat features_c and fingerprint vectors ==> form intermediate input vector (IIV)
        # IIV put into single SNN net
        # Defining half_model_model
        half_c = Input(shape=(fl.features_c_dim, ))
        half_fp = [
            Input(shape=(self.fp_length[0], ))
            for _ in range(fl.features_d_count)
        ]

        # Concat left side 4 inputs and right side 4 inputs
        half_combined = merge.Concatenate()([half_c] + half_fp)

        # singlenet for the SNN. Same weights and biases for top and bottom half of SNN
        singlenet = Sequential()
        hidden_layers = self.hparams['hidden_layers']
        numel = len(hidden_layers)
        generator_dropout = self.hparams.get('dropout', 0)
        singlenet.add(
            Dense(hidden_layers[0],
                  input_dim=self.features_c_dim +
                  self.features_d_count * self.hparams['fp_length'],
                  activation=self.hparams['activation'],
                  kernel_regularizer=regularizers.L1L2(
                      l1=self.hparams['singlenet_l1'],
                      l2=self.hparams['singlenet_l2'])))
        if generator_dropout != 0:
            singlenet.add(Dropout(generator_dropout))
        if numel > 1:
            if hidden_layers[
                    1] != 0:  # Even if hidden layers has 2 elements, 2nd element may be 0
                for i in range(numel - 1):
                    singlenet.add(
                        Dense(hidden_layers[i + 1],
                              activation=self.hparams['activation'],
                              kernel_regularizer=regularizers.L1L2(
                                  l1=self.hparams['singlenet_l1'],
                                  l2=self.hparams['singlenet_l2'])))
        singlenet.add(
            Dense(self.hparams.get('feature_vector_dim', 10),
                  activation='sigmoid'))

        # Output of half_model
        encoded_half = singlenet(half_combined)

        # Make half_model callable
        half_model = Model(name='half_model_encoded',
                           input=[half_c] + half_fp,
                           output=encoded_half)

        # All steps together by calling the above models one after another.
        # fp model ==> half_model ==> L1 distance and final node model
        lc = Input(name='left_c', shape=(fl.features_c_dim, ))
        left_features_d = []
        left_fp_model = []
        rc = Input(name='right_c', shape=(fl.features_c_dim, ))
        right_features_d = []
        right_fp_model = []
        for idx in range(fl.features_d_count):
            # a = atom tensor, b = bond tensor, e = edge tensor
            left_features_d.append([
                Input(name='l_a_inputs_' + str(idx) + 'x',
                      shape=fl.features_d_a[idx][0].shape[1:]),
                Input(name='l_b_inputs_' + str(idx) + 'y',
                      shape=fl.features_d_a[idx][1].shape[1:]),
                Input(name='l_e_inputs_' + str(idx) + 'z',
                      shape=fl.features_d_a[idx][2].shape[1:],
                      dtype='int32')
            ])
            # Call fp model for each set of left molecules
            left_fp_model.append(half_conv_net[idx](left_features_d[-1]))
            # Same as left side. Just change left to right.
            right_features_d.append([
                Input(name='r_a_inputs_' + str(idx) + 'x',
                      shape=fl.features_d_a[idx][0].shape[1:]),
                Input(name='r_b_inputs_' + str(idx) + 'y',
                      shape=fl.features_d_a[idx][1].shape[1:]),
                Input(name='r_e_inputs_' + str(idx) + 'z',
                      shape=fl.features_d_a[idx][2].shape[1:],
                      dtype='int32')
            ])
            # Call fp model for each set of right molecules
            right_fp_model.append(half_conv_net[idx](right_features_d[-1]))

        # Apply half_model to left and right side
        encoded_l = half_model([lc] + left_fp_model)
        encoded_r = half_model([rc] + right_fp_model)

        # layer to merge two encoded inputs with the l1 distance between them
        L1_layer = Lambda(lambda tensors: K.abs(tensors[0] - tensors[1]))
        # call this layer on list of two input tensors.
        L1_distance = L1_layer([encoded_l, encoded_r])

        prediction = Dense(1, activation='sigmoid')(L1_distance)
        self.siamese_net = Model(name='final_model',
                                 input=[lc] + [
                                     left_tensor
                                     for molecule in left_features_d
                                     for left_tensor in molecule
                                 ] + [rc] + [
                                     right_tensor
                                     for molecule in right_features_d
                                     for right_tensor in molecule
                                 ],
                                 output=prediction)

        if self.hparams.get('learning_rate', None) is None:
            self.siamese_net.compile(loss='binary_crossentropy',
                                     optimizer=self.hparams['optimizer'])
        else:
            sgd = optimizers.Adam(lr=self.hparams['learning_rate'])
            self.siamese_net.compile(loss='binary_crossentropy', optimizer=sgd)
Example #11
0
embedding_layer = Embedding(len(word_index) + 1,
                            EMBEDDING_DIM,
                            weights=[embedding_matrix],
                            input_length=MAX_SEQUENCE_LENGTH,
                            trainable=False)

sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)

for fsz in filter_sizes:
    l_conv = Conv1D(nb_filter=128, filter_length=fsz,
                    activation='relu')(embedded_sequences)
    l_pool = MaxPooling1D(5)(l_conv)
    convs.append(l_pool)

l_merge = merge.Concatenate(axis=1)(convs)
l_cov1 = Conv1D(128, 5, activation='relu')(l_merge)
l_pool1 = MaxPooling1D(5)(l_cov1)
l_cov2 = Conv1D(128, 5, activation='relu')(l_pool1)
l_pool2 = MaxPooling1D(30)(l_cov2)
l_flat = Flatten()(l_pool2)
l_dense = Dense(128, activation='relu')(l_flat)
preds = Dense(2, activation='softmax')(l_dense)

model = Model(sequence_input, preds)
checkpointer = ModelCheckpoint(filepath="weights.hdf5",
                               verbose=1,
                               save_best_only=True)
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=["accuracy", f1])
def train():#select optional model
    for kk in range(len(projects)):#len(projects)
        ss=time.time()

        print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
        print(projects[kk])
        print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")


        distances = []  # TrainSet
        labels = []  # 0/1
        texts = [] # ClassNameAndMethodName
        MAX_SEQUENCE_LENGTH = 15 
        EMBEDDING_DIM = 200 # Dimension of word vector

        embedding_model=word2vec.Word2Vec.load(basepath+"new_model.bin")

        with open(basepath+"data_compare/"+projects[kk]+"/train_Distances.txt",'r') as file_to_read:
        #with open("D:/data/7#Fold/train-weka"+"/train_distances.txt",'r') as file_to_read:
            for line in file_to_read.readlines():
                values = line.split()
                distance = values[:2]
                distances.append(distance)
                label =values[2:]
                labels.append(label)

            
        with open(basepath+"data_compare/"+projects[kk]+"/train_Names.txt",'r') as file_to_read:
        #with open("D:/data/7#Fold/train-weka"+"/train_names.txt",'r') as file_to_read:
            for line in file_to_read.readlines():
                texts.append(line)

        print('Found %s train_distances.' % len(distances))

        tokenizer = Tokenizer(num_words=None)
        tokenizer.fit_on_texts(texts)
        sequences = tokenizer.texts_to_sequences(texts)
        word_index = tokenizer.word_index
        data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)   
        distances = np.asarray(distances)
        labels = to_categorical(np.asarray(labels))

        print('Shape of train_data tensor:', data.shape)
        print('Shape of train_label tensor:', labels.shape)

        x_train = []
        x_train_names = data
        x_train_dis = distances 
        x_train_dis = np.expand_dims(x_train_dis, axis=2)

        x_train.append(x_train_names)
        x_train.append(np.array(x_train_dis))
        y_train = np.array(labels)
        
        for index in range(model_num):########################
            print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
            print(projects[kk],'---',index+1)
            print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
            print ("start time: "+time.strftime("%Y/%m/%d  %H:%M:%S"))

            x_train,y_train=getsubset(x_train,y_train)

            nb_words = len(word_index)
            embedding_matrix = np.zeros((nb_words+1, EMBEDDING_DIM))
            for word, i in word_index.items():
                embedding_vector = embedding_model.wv[word]
                if embedding_vector is not None:
                    embedding_matrix[i] = embedding_vector 

            embedding_layer = Embedding(nb_words + 1,
                                EMBEDDING_DIM,
                                input_length=MAX_SEQUENCE_LENGTH,
                                weights=[embedding_matrix],
                                trainable=False)

            print('Training model.')

            model_left = Sequential()
            model_left.add(embedding_layer)
            model_left.add(Conv1D(128, 1, padding = "same", activation='tanh'))
            model_left.add(Conv1D(128, 1, activation='tanh'))
            model_left.add(Conv1D(128, 1, activation='tanh'))
            model_left.add(Flatten())

            model_right = Sequential()
            model_right.add(Conv1D(128, 1, input_shape=(2,1), padding = "same", activation='tanh'))
            model_right.add(Conv1D(128, 1, activation='tanh'))
            model_right.add(Conv1D(128, 1, activation='tanh'))
            model_right.add(Flatten())

            output = merge.Concatenate()([model_left.output, model_right.output]) 

            output=Dense(128, activation='tanh')(output)
            output=Dense(2,activation='sigmoid')(output)

            input_left=model_left.input
            input_right=model_right.input

            model=Model([input_left,input_right],output)

            model.compile(loss='binary_crossentropy',optimizer='Adadelta',metrics=['accuracy'])

            model.fit(x_train, y_train,epochs=3,verbose=2)


            #json_string = model.to_json()
            #open(basepath+"models_compare/"+projects[kk]+"_"+str(index)+".json",'w').write(json_string)
            #model.save_weights(basepath+"models_compare/"+projects[kk]+"_"+str(index)+'.h5')
        print('########################',time.time()-ss)
Example #13
0
def stdn(args):

    att_lstm_num, att_lstm_seq_len, lstm_seq_len, neighbor_size, external_dim = \
        args['att_lstm_num'], args['att_lstm_seq_len'], args['lstm_seq_len'], \
        args['neighbor_size'], args['external_dim']

    slide_len = neighbor_size * 2 + 1

    flatten_att_nbhd_inputs = [
        Input(shape=(2, slide_len, slide_len),
              name="att_nbhd_volume_input_time_{0}_{1}".format(
                  att + 1, ts + 1)) for ts in range(att_lstm_seq_len)
        for att in range(att_lstm_num)
    ]

    flatten_att_flow_inputs = [
        Input(shape=(2, slide_len, slide_len),
              name="att_flow_volume_input_time_{0}_{1}".format(
                  att + 1, ts + 1)) for ts in range(att_lstm_seq_len)
        for att in range(att_lstm_num)
    ]

    # take out the corresponding local and flow data for attention
    att_nbhd_inputs = []
    att_flow_inputs = []
    for att in range(att_lstm_num):
        att_nbhd_inputs.append(
            flatten_att_nbhd_inputs[att * att_lstm_seq_len:(att + 1) *
                                    att_lstm_seq_len])
        att_flow_inputs.append(
            flatten_att_flow_inputs[att * att_lstm_seq_len:(att + 1) *
                                    att_lstm_seq_len])

    # external data for attention, holiday and weather
    att_lstm_inputs = [
        Input(shape=(att_lstm_seq_len, external_dim),
              name="att_lstm_input_{0}".format(att + 1))
        for att in range(att_lstm_num)
    ]

    # local, flow, external data for local cnn
    nbhd_inputs = [
        Input(shape=(2, slide_len, slide_len),
              name="nbhd_volume_input_time_{0}".format(ts + 1))
        for ts in range(lstm_seq_len)
    ]  # include many sequence
    flow_inputs = [
        Input(shape=(2, slide_len, slide_len),
              name="flow_volume_input_time_{0}".format(ts + 1))
        for ts in range(lstm_seq_len)
    ]
    lstm_inputs = Input(shape=(lstm_seq_len, external_dim), name="lstm_input")

    # short-term part, repeat three times
    # conv (local + flow) data --> Multiply fuse
    nbhd_convs = [
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="nbhd_convs_time0_{0}".format(ts + 1))(nbhd_inputs[ts])
        for ts in range(lstm_seq_len)
    ]  # Conv for different interval in current
    nbhd_convs = [
        Activation("relu",
                   name="nbhd_convs_activation_time0_{0}".format(ts + 1))(
                       nbhd_convs[ts]) for ts in range(lstm_seq_len)
    ]
    flow_convs = [
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="flow_convs_time0_{0}".format(ts + 1))(flow_inputs[ts])
        for ts in range(lstm_seq_len)
    ]  # Conv for past l(2) interval for flow gate
    flow_convs = [
        Activation("relu",
                   name="flow_convs_activation_time0_{0}".format(ts + 1))(
                       flow_convs[ts]) for ts in range(lstm_seq_len)
    ]
    flow_gates = [
        Activation("sigmoid",
                   name="flow_gate0_{0}".format(ts + 1))(flow_convs[ts])
        for ts in range(lstm_seq_len)
    ]  # use sigmoid to scale the range of flow conv into (0, 1)
    nbhd_convs = [
        keras.layers.Multiply()([nbhd_convs[ts], flow_gates[ts]])
        for ts in range(lstm_seq_len)
    ]  # Multiply local and flow

    # 2nd level gate
    nbhd_convs = [
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="nbhd_convs_time1_{0}".format(ts + 1))(nbhd_convs[ts])
        for ts in range(lstm_seq_len)
    ]  # ! here we use nbhd_convs(the result of first level)
    nbhd_convs = [
        Activation("relu",
                   name="nbhd_convs_activation_time1_{0}".format(ts + 1))(
                       nbhd_convs[ts]) for ts in range(lstm_seq_len)
    ]
    flow_convs = [
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="flow_convs_time1_{0}".format(ts + 1))(flow_inputs[ts])
        for ts in range(lstm_seq_len)
    ]  # flow input contai
    flow_convs = [
        Activation("relu",
                   name="flow_convs_activation_time1_{0}".format(ts + 1))(
                       flow_convs[ts]) for ts in range(lstm_seq_len)
    ]
    flow_gates = [
        Activation("sigmoid",
                   name="flow_gate1_{0}".format(ts + 1))(flow_convs[ts])
        for ts in range(lstm_seq_len)
    ]
    nbhd_convs = [
        keras.layers.Multiply()([nbhd_convs[ts], flow_gates[ts]])
        for ts in range(lstm_seq_len)
    ]

    # 3rd level gate
    nbhd_convs = [
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="nbhd_convs_time2_{0}".format(ts + 1))(nbhd_convs[ts])
        for ts in range(lstm_seq_len)
    ]
    nbhd_convs = [
        Activation("relu",
                   name="nbhd_convs_activation_time2_{0}".format(ts + 1))(
                       nbhd_convs[ts]) for ts in range(lstm_seq_len)
    ]
    flow_convs = [
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="flow_convs_time2_{0}".format(ts + 1))(flow_inputs[ts])
        for ts in range(lstm_seq_len)
    ]
    flow_convs = [
        Activation("relu",
                   name="flow_convs_activation_time2_{0}".format(ts + 1))(
                       flow_convs[ts]) for ts in range(lstm_seq_len)
    ]
    flow_gates = [
        Activation("sigmoid",
                   name="flow_gate2_{0}".format(ts + 1))(flow_convs[ts])
        for ts in range(lstm_seq_len)
    ]
    nbhd_convs = [
        keras.layers.Multiply()([nbhd_convs[ts], flow_gates[ts]])
        for ts in range(lstm_seq_len)
    ]

    # dense part
    nbhd_vecs = [
        Flatten(name="nbhd_flatten_time_{0}".format(ts + 1))(nbhd_convs[ts])
        for ts in range(lstm_seq_len)
    ]
    nbhd_vecs = [
        Dense(units=128,
              name="nbhd_dense_time_{0}".format(ts + 1))(nbhd_vecs[ts])
        for ts in range(lstm_seq_len)
    ]
    nbhd_vecs = [
        Activation("relu",
                   name="nbhd_dense_activation_time_{0}".format(ts + 1))(
                       nbhd_vecs[ts]) for ts in range(lstm_seq_len)
    ]

    # feature concatenate
    nbhd_vec = merge.Concatenate(axis=-1)(nbhd_vecs)
    nbhd_vec = Reshape(target_shape=(lstm_seq_len, 128))(nbhd_vec)
    lstm_input = merge.Concatenate(axis=-1)(
        [lstm_inputs,
         nbhd_vec])  # lstm_input: external data, nbhd_vec: cnn flatten result

    # lstm: the result of current interval
    lstm = LSTM(units=128,
                return_sequences=False,
                dropout=0.1,
                recurrent_dropout=0.1)(lstm_input)

    # attention part, repeat 3 times
    # # conv (local + flow) data --> Multiply fuse
    att_nbhd_convs = [[
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="att_nbhd_convs_time0_{0}_{1}".format(att + 1, ts + 1))(
                   att_nbhd_inputs[att][ts]) for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]  # att local conv
    att_nbhd_convs = [[
        Activation("relu",
                   name="att_nbhd_convs_activation_time0_{0}_{1}".format(
                       att + 1, ts + 1))(att_nbhd_convs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_flow_convs = [[
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="att_flow_convs_time0_{0}_{1}".format(att + 1, ts + 1))(
                   att_flow_inputs[att][ts]) for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]  # att flow
    att_flow_convs = [[
        Activation("relu",
                   name="att_flow_convs_activation_time0_{0}_{1}".format(
                       att + 1, ts + 1))(att_flow_convs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_flow_gates = [[
        Activation("sigmoid",
                   name="att_flow_gate0_{0}_{1}".format(att + 1, ts + 1))(
                       att_flow_convs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]  # att flow gate
    att_nbhd_convs = [[
        keras.layers.Multiply()(
            [att_nbhd_convs[att][ts], att_flow_gates[att][ts]])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]  # multiply to fuse local and flow gate

    att_nbhd_convs = [[
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="att_nbhd_convs_time1_{0}_{1}".format(att + 1, ts + 1))(
                   att_nbhd_convs[att][ts]) for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]  # ! note the input
    att_nbhd_convs = [[
        Activation("relu",
                   name="att_nbhd_convs_activation_time1_{0}_{1}".format(
                       att + 1, ts + 1))(att_nbhd_convs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_flow_convs = [[
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="att_flow_convs_time1_{0}_{1}".format(att + 1, ts + 1))(
                   att_flow_convs[att][ts]) for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_flow_convs = [[
        Activation("relu",
                   name="att_flow_convs_activation_time1_{0}_{1}".format(
                       att + 1, ts + 1))(att_flow_convs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_flow_gates = [[
        Activation("sigmoid",
                   name="att_flow_gate1_{0}_{1}".format(att + 1, ts + 1))(
                       att_flow_convs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_nbhd_convs = [[
        keras.layers.Multiply()(
            [att_nbhd_convs[att][ts], att_flow_gates[att][ts]])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]

    att_nbhd_convs = [[
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="att_nbhd_convs_time2_{0}_{1}".format(att + 1, ts + 1))(
                   att_nbhd_convs[att][ts]) for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_nbhd_convs = [[
        Activation("relu",
                   name="att_nbhd_convs_activation_time2_{0}_{1}".format(
                       att + 1, ts + 1))(att_nbhd_convs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_flow_convs = [[
        Conv2D(filters=64,
               kernel_size=(3, 3),
               padding="same",
               name="att_flow_convs_time2_{0}_{1}".format(att + 1, ts + 1))(
                   att_flow_convs[att][ts]) for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_flow_convs = [[
        Activation("relu",
                   name="att_flow_convs_activation_time2_{0}_{1}".format(
                       att + 1, ts + 1))(att_flow_convs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_flow_gates = [[
        Activation("sigmoid",
                   name="att_flow_gate2_{0}_{1}".format(att + 1, ts + 1))(
                       att_flow_convs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_nbhd_convs = [[
        keras.layers.Multiply()(
            [att_nbhd_convs[att][ts], att_flow_gates[att][ts]])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]

    # reshape cnn into (att_lstm_seq_len, 128),
    # then concatenate with lstm_input(external data)--> every day data
    att_nbhd_vecs = [[
        Flatten(name="att_nbhd_flatten_time_{0}_{1}".format(att + 1, ts + 1))(
            att_nbhd_convs[att][ts]) for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_nbhd_vecs = [[
        Dense(units=128,
              name="att_nbhd_dense_time_{0}_{1}".format(att + 1, ts + 1))(
                  att_nbhd_vecs[att][ts]) for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]
    att_nbhd_vecs = [[
        Activation("relu",
                   name="att_nbhd_dense_activation_time_{0}_{1}".format(
                       att + 1, ts + 1))(att_nbhd_vecs[att][ts])
        for ts in range(att_lstm_seq_len)
    ] for att in range(att_lstm_num)]

    att_nbhd_vec = [
        merge.Concatenate(axis=-1)(att_nbhd_vecs[att])
        for att in range(att_lstm_num)
    ]
    att_nbhd_vec = [
        Reshape(target_shape=(att_lstm_seq_len, 128))(att_nbhd_vec[att])
        for att in range(att_lstm_num)
    ]
    att_lstm_input = [
        merge.Concatenate(axis=-1)([att_lstm_inputs[att], att_nbhd_vec[att]])
        for att in range(att_lstm_num)
    ]

    # attention part : every day data --> lstm
    att_lstms = [
        LSTM(units=128,
             return_sequences=True,
             dropout=0.1,
             recurrent_dropout=0.1,
             name="att_lstm_{0}".format(att + 1))(att_lstm_input[att])
        for att in range(att_lstm_num)
    ]

    print('Before')
    # compare
    att_low_level = [
        Attention()([att_lstms[att], lstm]) for att in range(att_lstm_num)
    ]
    att_low_level = merge.Concatenate(axis=-1)(att_low_level)
    att_low_level = Reshape(target_shape=(att_lstm_num, 128))(att_low_level)

    print('After')

    att_high_level = LSTM(units=128,
                          return_sequences=False,
                          dropout=0.1,
                          recurrent_dropout=0.1)(att_low_level)

    lstm_all = merge.Concatenate(axis=-1)([att_high_level, lstm])
    lstm_all = Dense(units=2 * slide_len * slide_len)(lstm_all)
    lstm_all = Reshape((2, slide_len, slide_len))(lstm_all)
    pred_volume = Activation('tanh')(lstm_all)

    # every input is a list, only be this way
    inputs = \
        flatten_att_nbhd_inputs + flatten_att_flow_inputs + att_lstm_inputs + nbhd_inputs + flow_inputs + [lstm_inputs]
    # flatten_att_nbhd_inputs: (att_lstm_num * att_lstm_seq_len, 2, slide_len, slide_len)
    # flatten_att_flow_inputs: (att_lstm_num * att_lstm_seq_len, 2, slide_len, slide_len)
    # att_lstm_inputs: (att_lstm_num, att_lstm_seq_len, 18)
    # nbhd_inputs: (lstm_seq_len, 2, slide_len, slide_len)
    # flow_inputs: (lstm_seq_len, 2, slide_len, slide_len)
    # lstm_inputs: (lstm_seq_len, 18)

    print('flatten_att_nbhd_inputs:', att_lstm_num * att_lstm_seq_len, 2,
          slide_len, slide_len)
    print('flatten_att_flow_inputs:', att_lstm_num * att_lstm_seq_len, 2,
          slide_len, slide_len)
    print('att_lstm_inputs:', att_lstm_num, att_lstm_seq_len, 18)
    print('nbhd_inputs:', lstm_seq_len, 2, slide_len, slide_len)
    print('flow_inputs:', lstm_seq_len, 2, slide_len, slide_len)
    print('lstm_inputs:', lstm_seq_len, 18)

    model = Model(inputs=inputs, outputs=pred_volume)
    return model
Example #14
0
meta_input = Input(shape=data.x_train.shape[1:])

meta_hidden_layer = Dense(50)(meta_input)
meta_hidden_activation = LeakyReLU()(meta_hidden_layer)

meta = Dense(30)(meta_hidden_activation)
meta = LeakyReLU()(meta)

# ## Concatenate

# In[38]:

flatten1 = Flatten()(user_activity_stream)
flatten2 = Flatten()(sys_activity_stream)

concat_layer = merge.Concatenate(axis=-1)([flatten1, flatten2, meta])

# ## Merging MLP layer

# In[39]:

mlp_layer1 = Dense(380, input_shape=())(concat_layer)
mlp_activ1 = LeakyReLU()(mlp_layer1)

mlp_layer2 = Dense(450)(mlp_activ1)
mlp_activ2 = LeakyReLU()(mlp_layer2)

mlp_layer3 = Dense(260)(mlp_activ2)
mlp_activ3 = LeakyReLU()(mlp_layer3)

mlp_layer4 = Dense(200)(mlp_activ3)
Example #15
0
def tcn(args):
    len_global, len_local, neighbor_size, is_BN, nb_res_unit, num_cnn, num_tcn, nb_stacks, dataset, width, height = \
        args['len_global'], args['len_local'], args['neighbor_size'], args['is_BN'], \
        args['nb_res_unit'], args['num_cnn'], args['num_tcn'], args['nb_stacks'], \
        args['dataset'], args['width'], args['height']

    main_input = []
    neighbor_slide_len = neighbor_size * 2 + 1

    local_feature_list = []

    # ==============================
    # deal with the global local external data
    if dataset == 'bj_taxi':
        g_vacation = Input(shape=(len_global, ),
                           dtype='int32',
                           name='g_vacation')
        g_hour = Input(shape=(len_global, ), dtype='int32', name='g_hour')
        g_dayOfWeek = Input(shape=(len_global, ),
                            dtype='int32',
                            name='g_dayOfWeek')
        g_weather = Input(shape=(len_global, ),
                          dtype='int32',
                          name='g_weather')
        g_continuous_external = Input(shape=(len_global, 2),
                                      dtype='float32',
                                      name='g_continuous_external')
        main_input.append(g_vacation)
        main_input.append(g_hour)
        main_input.append(g_dayOfWeek)
        main_input.append(g_weather)
        main_input.append(g_continuous_external)

        embed_g_holiday = Embedding(output_dim=2,
                                    input_dim=2,
                                    input_length=len_global)(g_vacation)
        embed_g_hour = Embedding(output_dim=2,
                                 input_dim=25,
                                 input_length=len_global)(g_hour)
        embed_g_dayOfWeek = Embedding(output_dim=2,
                                      input_dim=7,
                                      input_length=len_global)(g_dayOfWeek)
        embed_g_weather = Embedding(output_dim=2,
                                    input_dim=17,
                                    input_length=len_global)(g_weather)

        g_external = merge.Concatenate(axis=-1)([
            embed_g_holiday, embed_g_hour, embed_g_dayOfWeek, embed_g_weather,
            g_continuous_external
        ])
    else:
        g_hour = Input(shape=(len_global, ), dtype='int32', name='g_hour')
        g_dayOfWeek = Input(shape=(len_global, ),
                            dtype='int32',
                            name='g_dayOfWeek')
        main_input.append(g_hour)
        main_input.append(g_dayOfWeek)

        embed_g_hour = Embedding(output_dim=2,
                                 input_dim=25,
                                 input_length=len_global)(g_hour)
        embed_g_dayOfWeek = Embedding(output_dim=2,
                                      input_dim=7,
                                      input_length=len_global)(g_dayOfWeek)

        g_external = merge.Concatenate(axis=-1)(
            [embed_g_hour, embed_g_dayOfWeek])

    g_out = Flatten()(g_external)
    g_out = Dense(units=50)(g_out)
    g_out = Dropout(rate=0.1)(g_out)
    g_out = Activation('relu')(g_out)
    g_out = Dense(units=2 * width * height)(g_out)
    g_out = Activation('relu')(g_out)
    feature_external_g = Reshape((2, width, height))(g_out)

    # ==============================
    # deal with the local external data
    if dataset == 'bj_taxi':
        t_vacation = Input(shape=(len_local, ),
                           dtype='int32',
                           name='t_vacation')
        t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour')
        t_dayOfWeek = Input(shape=(len_local, ),
                            dtype='int32',
                            name='t_dayOfWeek')
        t_weather = Input(shape=(len_local, ), dtype='int32', name='t_weather')
        t_continuous_external = Input(shape=(len_local, 2),
                                      dtype='float32',
                                      name='t_continuous_external')
        main_input.append(t_vacation)
        main_input.append(t_hour)
        main_input.append(t_dayOfWeek)
        main_input.append(t_weather)
        main_input.append(t_continuous_external)

        embed_t_holiday = Embedding(output_dim=2,
                                    input_dim=2,
                                    input_length=len_local)(t_vacation)
        embed_t_hour = Embedding(output_dim=2,
                                 input_dim=25,
                                 input_length=len_local)(t_hour)
        embed_t_dayOfWeek = Embedding(output_dim=2,
                                      input_dim=7,
                                      input_length=len_local)(t_dayOfWeek)
        embed_t_weather = Embedding(output_dim=2,
                                    input_dim=17,
                                    input_length=len_local)(t_weather)

        t_external = merge.Concatenate(axis=-1)([
            embed_t_holiday, embed_t_hour, embed_t_dayOfWeek, embed_t_weather,
            t_continuous_external
        ])
    else:
        t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour')
        t_dayOfWeek = Input(shape=(len_local, ),
                            dtype='int32',
                            name='t_dayOfWeek')
        main_input.append(t_hour)
        main_input.append(t_dayOfWeek)

        embed_t_hour = Embedding(output_dim=2,
                                 input_dim=25,
                                 input_length=len_local)(t_hour)
        embed_t_dayOfWeek = Embedding(output_dim=2,
                                      input_dim=7,
                                      input_length=len_local)(t_dayOfWeek)

        t_external = merge.Concatenate(axis=-1)(
            [embed_t_hour, embed_t_dayOfWeek])

    t_out = Flatten()(t_external)
    t_out = Dense(units=50)(t_out)
    t_out = Dropout(rate=0.1)(t_out)
    t_out = Activation('relu')(t_out)
    t_out = Dense(units=2 * neighbor_slide_len * neighbor_slide_len)(t_out)
    t_out = Activation('relu')(t_out)
    feature_external_t = Reshape(
        (2, neighbor_slide_len, neighbor_slide_len))(t_out)

    # ==============================
    # deal with the current local flow data
    current_local_flow = \
        Input(shape=(2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='current_local_flow')
    main_input.append(current_local_flow)

    cnn_out = cnn_unit(is_BN, num_cnn)(current_local_flow)
    activation = Activation('relu')(cnn_out)
    feature_current_local_flow = Conv2D(2, (3, 3),
                                        strides=(1, 1),
                                        padding='same')(activation)

    local_feature_list.append(feature_current_local_flow)

    # ==============================
    # deal with the local stacked flow data
    stack_local_flow = \
        Input(shape=(len_local * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='stack_local_flow')
    main_input.append(stack_local_flow)
    tcn_out = Reshape(
        (len_local,
         2 * neighbor_slide_len * neighbor_slide_len))(stack_local_flow)

    for i in range(num_tcn):
        if i == num_tcn - 1 or (i == 0 and num_tcn == 1):
            tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=False)(tcn_out)
        else:
            tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=True)(tcn_out)

    o = Dense(units=(2 * neighbor_slide_len * neighbor_slide_len))(tcn_out)
    feature_stacked_local_flow = Reshape(
        (2, neighbor_slide_len, neighbor_slide_len))(o)

    local_feature_list.append(feature_stacked_local_flow)

    # ==============================
    # fuse local stacked flow feature and current local flow feature, then fuse local external feature
    local_output = []
    for feature in local_feature_list:
        local_output.append(ilayer()(feature))
    feature_local_flow = merge.Add()(local_output)

    # todo consider the fuse method
    feature_local = merge.Add()([feature_external_t, feature_local_flow])

    # ==============================
    # deal with the global flow data, then fuse global external feature
    global_flow = \
        Input(shape=(len_global * 2, width, height), dtype='float32', name='global_flow')
    main_input.append(global_flow)

    # todo consider the model here
    conv1 = Conv2D(64, (3, 3), strides=(1, 1), padding='same')(global_flow)
    activation = Activation('relu')(conv1)
    res_out = rest_unit(is_BN, nb_res_unit)(activation)
    activation = Activation('relu')(res_out)
    feature_global_flow = Conv2D(2, (3, 3), strides=(1, 1),
                                 padding='same')(activation)

    # todo consider the fuse method
    feature_global = merge.Add()([feature_external_g, feature_global_flow])

    # reshape the global feaure
    feature_global = Flatten()(feature_global)
    feature_global = Dense(units=200)(feature_global)
    feature_global = Dropout(rate=0.1)(feature_global)
    feature_global = Dense(units=2 * neighbor_slide_len *
                           neighbor_slide_len)(feature_global)
    feature_global = Activation('relu')(feature_global)
    feature_global = Reshape(
        (2, neighbor_slide_len, neighbor_slide_len))(feature_global)

    # ==============================
    # fuse local and global feature to predict
    # index_cut = Input(shape=(2,), dtype='int32', name='index_cut')
    # main_input.append(index_cut)
    # row, column = index_cut[0].eval(), index_cut[1].eval(session=K.get_session())

    # fuse the global and local feature
    output = []
    for feature in [feature_global, feature_local]:
        output.append(ilayer()(feature))
    main_output = merge.Add()(output)

    main_output = Activation('tanh')(main_output)

    model = Model(inputs=main_input, outputs=main_output, name='GLST-Net')
    return model
Example #16
0
def tcn_nog_rdw_att(args):
    len_recent, len_daily, len_week, neighbor_size, num_tcn, nb_stacks, is_BN, num_cnn, dataset  = \
        args['len_recent'], args['len_daily'], args['len_week'], args['neighbor_size'], \
        args['num_tcn'], args['nb_stacks'], args['BN'], args['num_cnn'], args['dataset']

    main_input = []
    neighbor_slide_len = neighbor_size * 2 + 1

    local_feature_list = []

    # ==============================
    # deal with the local external data
    if dataset == 'bj_taxi':
        # current
        current_vacation = Input(shape=(1, ),
                                 dtype='int32',
                                 name='current_vacation')
        current_hour = Input(shape=(1, ), dtype='int32', name='current_hour')
        current_dayOfWeek = Input(shape=(1, ),
                                  dtype='int32',
                                  name='current_dayOfWeek')
        current_weather = Input(shape=(1, ),
                                dtype='int32',
                                name='current_weather')
        current_continuous_external = Input(shape=(1, 2),
                                            dtype='float32',
                                            name='current_continuous_external')

        main_input.append(current_vacation)
        main_input.append(current_hour)
        main_input.append(current_dayOfWeek)
        main_input.append(current_weather)
        main_input.append(current_continuous_external)

        embed_current_vacation = Embedding(output_dim=2,
                                           input_dim=2,
                                           input_length=1)(current_vacation)
        embed_current_hour = Embedding(output_dim=2,
                                       input_dim=25,
                                       input_length=1)(current_hour)
        embed_current_dayOfWeek = Embedding(output_dim=2,
                                            input_dim=7,
                                            input_length=1)(current_dayOfWeek)
        embed_current_weather = Embedding(output_dim=2,
                                          input_dim=17,
                                          input_length=1)(current_weather)
        current_external = merge.Concatenate(axis=-1)([
            embed_current_vacation, embed_current_hour,
            embed_current_dayOfWeek, embed_current_weather,
            current_continuous_external
        ])

        # recent external
        recent_vacation = Input(shape=(len_recent, ),
                                dtype='int32',
                                name='recent_vacation')
        recent_hour = Input(shape=(len_recent, ),
                            dtype='int32',
                            name='recent_hour')
        recent_dayOfWeek = Input(shape=(len_recent, ),
                                 dtype='int32',
                                 name='recent_dayOfWeek')
        recent_weather = Input(shape=(len_recent, ),
                               dtype='int32',
                               name='recent_weather')
        recent_continuous_external = Input(shape=(len_recent, 2),
                                           dtype='float32',
                                           name='recent_continuous_external')

        main_input.append(recent_vacation)
        main_input.append(recent_hour)
        main_input.append(recent_dayOfWeek)
        main_input.append(recent_weather)
        main_input.append(recent_continuous_external)

        embed_recent_vacation = Embedding(
            output_dim=2, input_dim=2,
            input_length=len_recent)(recent_vacation)
        embed_recent_hour = Embedding(output_dim=2,
                                      input_dim=25,
                                      input_length=len_recent)(recent_hour)
        embed_recent_dayOfWeek = Embedding(
            output_dim=2, input_dim=7,
            input_length=len_recent)(recent_dayOfWeek)
        embed_recent_weather = Embedding(
            output_dim=2, input_dim=17,
            input_length=len_recent)(recent_weather)
        recent_external = merge.Concatenate(axis=-1)([
            embed_recent_vacation, embed_recent_hour, embed_recent_dayOfWeek,
            embed_recent_weather, recent_continuous_external
        ])

        # daily external
        daily_vacation = Input(shape=(len_daily, ),
                               dtype='int32',
                               name='daily_vacation')
        daily_hour = Input(shape=(len_daily, ),
                           dtype='int32',
                           name='daily_hour')
        daily_dayOfWeek = Input(shape=(len_daily, ),
                                dtype='int32',
                                name='daily_dayOfWeek')
        daily_weather = Input(shape=(len_daily, ),
                              dtype='int32',
                              name='daily_weather')
        daily_continuous_external = Input(shape=(len_daily, 2),
                                          dtype='float32',
                                          name='daily_continuous_external')

        main_input.append(daily_vacation)
        main_input.append(daily_hour)
        main_input.append(daily_dayOfWeek)
        main_input.append(daily_weather)
        main_input.append(daily_continuous_external)

        embed_daily_vacation = Embedding(
            output_dim=2, input_dim=2, input_length=len_daily)(daily_vacation)
        embed_daily_hour = Embedding(output_dim=2,
                                     input_dim=25,
                                     input_length=len_daily)(daily_hour)
        embed_daily_dayOfWeek = Embedding(
            output_dim=2, input_dim=7, input_length=len_daily)(daily_dayOfWeek)
        embed_daily_weather = Embedding(output_dim=2,
                                        input_dim=17,
                                        input_length=len_daily)(daily_weather)
        daily_external = merge.Concatenate(axis=-1)([
            embed_daily_vacation, embed_daily_hour, embed_daily_dayOfWeek,
            embed_daily_weather, daily_continuous_external
        ])

        # weekly external
        week_vacation = Input(shape=(len_week, ),
                              dtype='int32',
                              name='week_vacation')
        week_hour = Input(shape=(len_week, ), dtype='int32', name='week_hour')
        week_dayOfWeek = Input(shape=(len_week, ),
                               dtype='int32',
                               name='week_dayOfWeek')
        week_weather = Input(shape=(len_week, ),
                             dtype='int32',
                             name='week_weather')
        week_continuous_external = Input(shape=(len_week, 2),
                                         dtype='float32',
                                         name='week_continuous_external')

        main_input.append(week_vacation)
        main_input.append(week_hour)
        main_input.append(week_dayOfWeek)
        main_input.append(week_weather)
        main_input.append(week_continuous_external)

        embed_week_vacation = Embedding(output_dim=2,
                                        input_dim=2,
                                        input_length=len_week)(week_vacation)
        embed_week_hour = Embedding(output_dim=2,
                                    input_dim=25,
                                    input_length=len_week)(week_hour)
        embed_week_dayOfWeek = Embedding(output_dim=2,
                                         input_dim=7,
                                         input_length=len_week)(week_dayOfWeek)
        embed_week_weather = Embedding(output_dim=2,
                                       input_dim=17,
                                       input_length=len_week)(week_weather)
        week_external = merge.Concatenate(axis=-1)([
            embed_week_vacation, embed_week_hour, embed_week_dayOfWeek,
            embed_week_weather, week_continuous_external
        ])
    else:
        # current
        current_hour = Input(shape=(1, ), dtype='int32', name='current_hour')
        current_dayOfWeek = Input(shape=(1, ),
                                  dtype='int32',
                                  name='current_dayOfWeek')

        main_input.append(current_hour)
        main_input.append(current_dayOfWeek)

        embed_current_hour = Embedding(output_dim=2,
                                       input_dim=25,
                                       input_length=1)(current_hour)
        embed_current_dayOfWeek = Embedding(output_dim=2,
                                            input_dim=7,
                                            input_length=1)(current_dayOfWeek)
        current_external = merge.Concatenate(axis=-1)(
            [embed_current_hour, embed_current_dayOfWeek])

        # recent external
        recent_hour = Input(shape=(len_recent, ),
                            dtype='int32',
                            name='recent_hour')
        recent_dayOfWeek = Input(shape=(len_recent, ),
                                 dtype='int32',
                                 name='recent_dayOfWeek')

        main_input.append(recent_hour)
        main_input.append(recent_dayOfWeek)

        embed_recent_hour = Embedding(output_dim=2,
                                      input_dim=25,
                                      input_length=len_recent)(recent_hour)
        embed_recent_dayOfWeek = Embedding(
            output_dim=2, input_dim=7,
            input_length=len_recent)(recent_dayOfWeek)

        recent_external = merge.Concatenate(axis=-1)(
            [embed_recent_hour, embed_recent_dayOfWeek])

        # daily external
        daily_hour = Input(shape=(len_daily, ),
                           dtype='int32',
                           name='daily_hour')
        daily_dayOfWeek = Input(shape=(len_daily, ),
                                dtype='int32',
                                name='daily_dayOfWeek')

        main_input.append(daily_hour)
        main_input.append(daily_dayOfWeek)

        embed_daily_hour = Embedding(output_dim=2,
                                     input_dim=25,
                                     input_length=len_daily)(daily_hour)
        embed_daily_dayOfWeek = Embedding(
            output_dim=2, input_dim=7, input_length=len_daily)(daily_dayOfWeek)
        daily_external = merge.Concatenate(axis=-1)(
            [embed_daily_hour, embed_daily_dayOfWeek])

        # weekly external
        week_hour = Input(shape=(len_week, ), dtype='int32', name='week_hour')
        week_dayOfWeek = Input(shape=(len_week, ),
                               dtype='int32',
                               name='week_dayOfWeek')

        main_input.append(week_hour)
        main_input.append(week_dayOfWeek)

        embed_week_hour = Embedding(output_dim=2,
                                    input_dim=25,
                                    input_length=len_week)(week_hour)
        embed_week_dayOfWeek = Embedding(output_dim=2,
                                         input_dim=7,
                                         input_length=len_week)(week_dayOfWeek)

        week_external = merge.Concatenate(axis=-1)(
            [embed_week_hour, embed_week_dayOfWeek])

    current_out = Flatten()(current_external)
    current_out = Dense(units=20)(current_out)
    current_out = Dropout(rate=0.1)(current_out)
    current_out = Activation('relu')(current_out)
    current_out = Dense(units=10)(current_out)
    feature_external_current = Activation('relu')(current_out)

    recent_out = Dense(units=20)(recent_external)
    recent_out = Dropout(rate=0.1)(recent_out)
    recent_out = Activation('relu')(recent_out)
    recent_out = Dense(units=10)(recent_out)
    feature_external_recent = Activation('relu')(recent_out)

    daily_out = Dense(units=20)(daily_external)
    daily_out = Dropout(rate=0.1)(daily_out)
    daily_out = Activation('relu')(daily_out)
    daily_out = Dense(units=10)(daily_out)
    feature_external_daily = Activation('relu')(daily_out)

    week_out = Dense(units=20)(week_external)
    week_out = Dropout(rate=0.1)(week_out)
    week_out = Activation('relu')(week_out)
    week_out = Dense(units=10)(week_out)
    feature_external_week = Activation('relu')(week_out)

    # ==============================
    # deal with the current local flow data
    current_local_flow = \
        Input(shape=(2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='current_local_flow')
    main_input.append(current_local_flow)

    current_local_flow = cnn_unit(is_BN, num_cnn)(current_local_flow)
    current_local_flow = Activation('relu')(current_local_flow)
    current_local_flow = Conv2D(2, (3, 3), strides=(1, 1),
                                padding='same')(current_local_flow)

    current_local_flow = Flatten()(current_local_flow)
    current_local_flow = merge.Concatenate(axis=-1)(
        [feature_external_current, current_local_flow])
    current_local_flow = Dense(units=64, activation='relu')(current_local_flow)

    local_feature_list.append(current_local_flow)

    # ==============================
    # deal with the recent stacked flow data
    recent_local_flow = \
        Input(shape=(len_recent * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='recent_local_flow')
    main_input.append(recent_local_flow)
    recent_local_flow = Reshape(
        (len_recent,
         2 * neighbor_slide_len * neighbor_slide_len))(recent_local_flow)
    recent_local_flow = merge.Concatenate(axis=-1)(
        [feature_external_recent, recent_local_flow])
    recent_local_flow = Dense(units=64, activation='relu')(recent_local_flow)

    # deal with the daily stacked flow data
    daily_local_flow = \
        Input(shape=(len_daily * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32',
              name='daily_local_flow')
    main_input.append(daily_local_flow)
    daily_local_flow = Reshape(
        (len_daily,
         2 * neighbor_slide_len * neighbor_slide_len))(daily_local_flow)
    daily_local_flow = merge.Concatenate(axis=-1)(
        [feature_external_daily, daily_local_flow])
    daily_local_flow = Dense(units=64, activation='relu')(daily_local_flow)

    # deal with the week stacked flow data
    week_local_flow = \
        Input(shape=(len_week * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32',
              name='week_local_flow')
    main_input.append(week_local_flow)
    week_local_flow = Reshape(
        (len_week,
         2 * neighbor_slide_len * neighbor_slide_len))(week_local_flow)
    week_local_flow = merge.Concatenate(axis=-1)(
        [feature_external_week, week_local_flow])
    week_local_flow = Dense(units=64, activation='relu')(week_local_flow)

    for i in range(num_tcn):
        recent_local_flow = TCN(nb_stacks=nb_stacks,
                                return_sequences=True)(recent_local_flow)
        daily_local_flow = TCN(nb_stacks=nb_stacks,
                               return_sequences=True)(daily_local_flow)
        week_local_flow = TCN(nb_stacks=nb_stacks,
                              return_sequences=True)(daily_local_flow)

    attention_hidden_recent = Lambda(lambda x: x[:, :])(recent_local_flow)
    attention_hidden_daily = Lambda(lambda x: x[:, :])(daily_local_flow)
    attention_hidden_week = Lambda(lambda x: x[:, :])(week_local_flow)

    att_recent = Attention()([attention_hidden_recent, current_local_flow])
    att_daily = Attention()([attention_hidden_daily, current_local_flow])
    att_week = Attention()([attention_hidden_week, current_local_flow])

    # fuse attention
    output = []
    for att in [att_recent, att_daily, att_week]:
        output.append(ilayer()(att))
    att_final = merge.Add()(output)

    att_current = merge.Concatenate(axis=-1)([att_final, current_local_flow])

    att_current = Dense(units=2 * neighbor_slide_len *
                        neighbor_slide_len)(att_current)
    att_current = Reshape(
        (2, neighbor_slide_len, neighbor_slide_len))(att_current)
    main_output = Activation('tanh')(att_current)

    model = Model(inputs=main_input,
                  outputs=main_output,
                  name='tcn_nog_rdw_att')
    return model
Example #17
0
def tcn_no_global(args):
    len_local, neighbor_size, num_tcn, nb_stacks, is_BN, num_cnn, dataset  = \
        args['len_local'], args['neighbor_size'], args['num_tcn'], args['nb_stacks'], \
        args['is_BN'], args['num_cnn'], args['dataset']

    main_input = []
    neighbor_slide_len = neighbor_size * 2 + 1

    local_feature_list = []

    # ==============================
    # deal with the local external data
    if dataset == 'bj_taxi':
        t_vacation = Input(shape=(len_local, ),
                           dtype='int32',
                           name='t_vacation')
        t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour')
        t_dayOfWeek = Input(shape=(len_local, ),
                            dtype='int32',
                            name='t_dayOfWeek')
        t_weather = Input(shape=(len_local, ), dtype='int32', name='t_weather')
        t_continuous_external = Input(shape=(len_local, 2),
                                      dtype='float32',
                                      name='t_continuous_external')
        main_input.append(t_vacation)
        main_input.append(t_hour)
        main_input.append(t_dayOfWeek)
        main_input.append(t_weather)
        main_input.append(t_continuous_external)

        embed_t_holiday = Embedding(output_dim=2,
                                    input_dim=2,
                                    input_length=len_local)(t_vacation)
        embed_t_hour = Embedding(output_dim=2,
                                 input_dim=25,
                                 input_length=len_local)(t_hour)
        embed_t_dayOfWeek = Embedding(output_dim=2,
                                      input_dim=7,
                                      input_length=len_local)(t_dayOfWeek)
        embed_t_weather = Embedding(output_dim=2,
                                    input_dim=17,
                                    input_length=len_local)(t_weather)
        t_external = merge.Concatenate(axis=-1)([
            embed_t_holiday, embed_t_hour, embed_t_dayOfWeek, embed_t_weather,
            t_continuous_external
        ])
    else:
        t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour')
        t_dayOfWeek = Input(shape=(len_local, ),
                            dtype='int32',
                            name='t_dayOfWeek')
        main_input.append(t_hour)
        main_input.append(t_dayOfWeek)

        embed_t_hour = Embedding(output_dim=2,
                                 input_dim=25,
                                 input_length=len_local)(t_hour)
        embed_t_dayOfWeek = Embedding(output_dim=2,
                                      input_dim=7,
                                      input_length=len_local)(t_dayOfWeek)
        t_external = merge.Concatenate(axis=-1)(
            [embed_t_hour, embed_t_dayOfWeek])

    t_out = Flatten()(t_external)
    t_out = Dense(units=50)(t_out)
    t_out = Dropout(rate=0.1)(t_out)
    t_out = Activation('relu')(t_out)
    t_out = Dense(units=2 * neighbor_slide_len * neighbor_slide_len)(t_out)
    t_out = Activation('relu')(t_out)
    feature_external_t = Reshape(
        (2, neighbor_slide_len, neighbor_slide_len))(t_out)

    # ==============================
    # deal with the current local flow data
    current_local_flow = \
        Input(shape=(2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='current_local_flow')
    main_input.append(current_local_flow)

    cnn_out = cnn_unit(is_BN, num_cnn)(current_local_flow)
    activation = Activation('relu')(cnn_out)
    feature_current_local_flow = Conv2D(2, (3, 3),
                                        strides=(1, 1),
                                        padding='same')(activation)

    local_feature_list.append(feature_current_local_flow)

    # ==============================
    # deal with the local stacked flow data
    stack_local_flow = \
        Input(shape=(len_local * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='stack_local_flow')
    main_input.append(stack_local_flow)
    tcn_out = Reshape(
        (len_local,
         2 * neighbor_slide_len * neighbor_slide_len))(stack_local_flow)

    for i in range(num_tcn):
        if i == num_tcn - 1 or (i == 0 and num_tcn == 1):
            tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=False)(tcn_out)
        else:
            tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=True)(tcn_out)

    o = Dense(units=(2 * neighbor_slide_len * neighbor_slide_len))(tcn_out)
    feature_stacked_local_flow = Reshape(
        (2, neighbor_slide_len, neighbor_slide_len))(o)

    local_feature_list.append(feature_stacked_local_flow)

    # ==============================
    # fuse local stacked flow feature and current local flow feature, then fuse local external feature
    local_output = []
    for feature in local_feature_list:
        local_output.append(ilayer()(feature))
    feature_local_flow = merge.Add()(local_output)

    feature_local = merge.Add()([feature_external_t, feature_local_flow])

    main_output = Activation('tanh')(feature_local)

    model = Model(inputs=main_input, outputs=main_output, name='tcn_nog')
    return model
Example #18
0
def simple_tcn(args):
    len_local, neighbor_size, num_tcn, nb_stacks, dataset = \
        args['len_local'], args['neighbor_size'], args['num_tcn'], args['nb_stacks'], args['dataset']
    main_input = []
    neighbor_slide_len = neighbor_size * 2 + 1

    # ==============================
    # deal with the local external data
    if dataset == 'bj_taxi':
        t_vacation = Input(shape=(len_local, ),
                           dtype='int32',
                           name='t_vacation')
        t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour')
        t_dayOfWeek = Input(shape=(len_local, ),
                            dtype='int32',
                            name='t_dayOfWeek')
        t_weather = Input(shape=(len_local, ), dtype='int32', name='t_weather')
        t_continuous_external = Input(shape=(len_local, 2),
                                      dtype='float32',
                                      name='t_continuous_external')
        main_input.append(t_vacation)
        main_input.append(t_hour)
        main_input.append(t_dayOfWeek)
        main_input.append(t_weather)
        main_input.append(t_continuous_external)

        embed_t_holiday = Embedding(output_dim=2,
                                    input_dim=2,
                                    input_length=len_local)(t_vacation)
        embed_t_hour = Embedding(output_dim=2,
                                 input_dim=25,
                                 input_length=len_local)(t_hour)
        embed_t_dayOfWeek = Embedding(output_dim=2,
                                      input_dim=7,
                                      input_length=len_local)(t_dayOfWeek)
        embed_t_weather = Embedding(output_dim=2,
                                    input_dim=17,
                                    input_length=len_local)(t_weather)

        t_external = merge.Concatenate(axis=-1)([
            embed_t_holiday, embed_t_hour, embed_t_dayOfWeek, embed_t_weather,
            t_continuous_external
        ])
    else:
        t_hour = Input(shape=(len_local, ), dtype='int32', name='t_hour')
        t_dayOfWeek = Input(shape=(len_local, ),
                            dtype='int32',
                            name='t_dayOfWeek')
        main_input.append(t_hour)
        main_input.append(t_dayOfWeek)

        embed_t_hour = Embedding(output_dim=2,
                                 input_dim=25,
                                 input_length=len_local)(t_hour)
        embed_t_dayOfWeek = Embedding(output_dim=2,
                                      input_dim=7,
                                      input_length=len_local)(t_dayOfWeek)

        t_external = merge.Concatenate(axis=-1)(
            [embed_t_hour, embed_t_dayOfWeek])

    t_out = Flatten()(t_external)
    t_out = Dense(units=50)(t_out)
    t_out = Dropout(rate=0.1)(t_out)
    t_out = Activation('relu')(t_out)
    t_out = Dense(units=2 * neighbor_slide_len * neighbor_slide_len)(t_out)
    t_out = Activation('relu')(t_out)
    feature_external_t = Reshape(
        (2, neighbor_slide_len, neighbor_slide_len))(t_out)

    # ==============================
    # deal with the local stacked flow data
    stack_local_flow = \
        Input(shape=(len_local * 2, neighbor_slide_len, neighbor_slide_len), dtype='float32', name='stack_local_flow')
    main_input.append(stack_local_flow)
    tcn_out = Reshape(
        (len_local,
         2 * neighbor_slide_len * neighbor_slide_len))(stack_local_flow)

    for i in range(num_tcn):
        if i == num_tcn - 1 or (i == 0 and num_tcn == 1):
            tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=False)(tcn_out)
        else:
            tcn_out = TCN(nb_stacks=nb_stacks, return_sequences=True)(tcn_out)

    o = Dense(units=(2 * neighbor_slide_len * neighbor_slide_len))(tcn_out)
    feature_stacked_local_flow = Reshape(
        (2, neighbor_slide_len, neighbor_slide_len))(o)

    feature_local = merge.Add()(
        [feature_external_t, feature_stacked_local_flow])

    main_output = Activation('tanh')(feature_local)

    model = Model(inputs=main_input, outputs=main_output, name='simple-tcn')
    return model