Python keras.layers.recurrent.LSTM() Examples

The following are code examples for showing how to use keras.layers.recurrent.LSTM(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: GroundedTranslation   Author: elliottd   File: models.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, embed_size, hidden_size, vocab_size, dropin, optimiser,
                 l2reg, hsn_size=512, weights=None, gru=False,
                 clipnorm=-1, batch_size=None, t=None, lr=0.001):

        self.max_t = t  # Expected timesteps. Needed to build the Theano graph

        # Model hyperparameters
        self.vocab_size = vocab_size  # size of word vocabulary
        self.embed_size = embed_size  # number of units in a word embedding
        self.hsn_size = hsn_size  # size of the source hidden vector
        self.hidden_size = hidden_size  # number of units in first LSTM
        self.gru = gru  # gru recurrent layer? (false = lstm)
        self.dropin = dropin  # prob. of dropping input units
        self.l2reg = l2reg  # weight regularisation penalty

        # Optimiser hyperparameters
        self.optimiser = optimiser  # optimisation method
        self.lr = lr
        self.beta1 = 0.9
        self.beta2 = 0.999
        self.epsilon = 1e-8
        self.clipnorm = clipnorm

        self.weights = weights  # initialise with checkpointed weights? 
Example 2
Project: GroundedTranslation   Author: elliottd   File: models.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, embed_size, hidden_size, vocab_size, dropin, optimiser,
                 l2reg, hsn_size=512, weights=None, gru=False,
                 clipnorm=-1, batch_size=None, t=None, lr=0.001):

        self.max_t = t  # Expected timesteps. Needed to build the Theano graph

        # Model hyperparameters
        self.vocab_size = vocab_size  # size of word vocabulary
        self.embed_size = embed_size  # number of units in a word embedding
        self.hsn_size = hsn_size  # size of the source hidden vector
        self.hidden_size = hidden_size  # number of units in first LSTM
        self.gru = gru  # gru recurrent layer? (false = lstm)
        self.dropin = dropin  # prob. of dropping input units
        self.l2reg = l2reg  # weight regularisation penalty

        # Optimiser hyperparameters
        self.optimiser = optimiser  # optimisation method
        self.lr = lr
        self.beta1 = 0.9
        self.beta2 = 0.999
        self.epsilon = 1e-8
        self.clipnorm = clipnorm

        self.weights = weights  # initialise with checkpointed weights? 
Example 3
Project: PIEPredict   Author: aras62   File: pie_intent.py    Apache License 2.0 6 votes vote down vote up
def create_lstm_model(self,
                          name='convlstm_encdec',
                          r_state=True,
                          r_sequence=False):
        return LSTM(units=self._num_hidden_units,
                    dropout=self._lstm_dropout,
                    recurrent_dropout=self._lstm_recurrent_dropout,
                    return_state=r_state,
                    return_sequences=r_sequence,
                    stateful=False,
                    bias_initializer='zeros',
                    kernel_regularizer=self._kernel_regularizer,
                    recurrent_regularizer=self._recurrent_regularizer,
                    bias_regularizer=self._bias_regularizer,
                    activation=self._activation,
                    name=name) 
Example 4
Project: PIEPredict   Author: aras62   File: pie_predict.py    Apache License 2.0 6 votes vote down vote up
def create_lstm_model(self, name='lstm', r_state=True, r_sequence=True):
        """
        A Helper function that generates an instance of LSTM
        :param name: Name of the layer
        :param r_state: Whether to return states
        :param r_sequence: Whether to return sequences
        :return: An LSTM instance
        """

        return LSTM(units=self._num_hidden_units,
                    return_state=r_state,
                    return_sequences=r_sequence,
                    stateful=False,
                    kernel_regularizer=self._regularizer,
                    recurrent_regularizer=self._regularizer,
                    bias_regularizer=self._regularizer,
                    activity_regularizer=None,
                    activation=self._activation,
                    name=name)

    # Custom layers 
Example 5
Project: StockRecommendSystem   Author: doncat99   File: Stock_Prediction_Model_Stateless_LSTM.py    MIT License 6 votes vote down vote up
def LSTM(self, argsDict):
        self.paras.batch_size             = argsDict["batch_size"]
        self.paras.model['dropout']       = argsDict['dropout']
        self.paras.model['activation']    = argsDict["activation"]
        self.paras.model['optimizer']     = argsDict["optimizer"]
        self.paras.model['learning_rate'] = argsDict["learning_rate"]

        print(self.paras.batch_size, self.paras.model['dropout'], self.paras.model['activation'], self.paras.model['optimizer'], self.paras.model['learning_rate'])

        model = self.lstm_model()
        model.fit(self.train_x, self.train_y,
              batch_size=self.paras.batch_size,
              epochs=self.paras.epoch,
              verbose=0,
              callbacks=[EarlyStopping(monitor='loss', patience=5)]
              )

        score, mse = model.evaluate(self.test_x, self.test_y, verbose=0)
        y_pred=model.predict(self.test_x)
        reca=Recall_s(self.test_y,y_pred)
        return -reca 
Example 6
Project: StockRecommendSystem   Author: doncat99   File: Stock_Prediction_Model_Stateless_LSTM.py    MIT License 6 votes vote down vote up
def plot_training_curve(self, history):
        #         %matplotlib inline
        #         %pylab inline
        #         pylab.rcParams['figure.figsize'] = (15, 9)   # Change the size of plots

        # LSTM training
        f, ax = plt.subplots()
        ax.plot(history.history['loss'])
        #ax.plot(history.history['val_loss'])
        ax.set_title('loss function')
        ax.set_ylabel('mse')
        ax.set_xlabel('epoch')
        #ax.legend(['loss', 'val_loss'], loc='upper right')
        ax.legend(['loss'], loc='upper right')
        plt.show()
        if self.paras.save == True:
            w = csv.writer(open(self.paras.save_folder + 'training_curve_model.txt', 'w'))
            for key, val in history.history.items():
                w.writerow([key, val])
            for key, val in history.params.items():
                w.writerow([key, val])

# Classification 
Example 7
Project: dlopt   Author: acamero   File: rnn.py    GNU General Public License v3.0 6 votes vote down vote up
def _build_model(self, layers, dense_activation):
        self.hidden_layers = len(layers) - 2
        self.layers = layers
        self.input_dim = layers[0]
        self.output_dim = layers[-1]
        model = Sequential()
        for i in range(len(layers) - 2):
            model.add(
                    LSTM(
                    #SimpleRNN(
                    input_dim=layers[i],
                    output_dim=layers[i+1],
                    kernel_initializer='zeros', 
                    recurrent_initializer='zeros',
                    bias_initializer='zeros',
                    # Uncomment to use last batch state to init next training step.
                    # Specify shuffle=False when calling fit() 
                    #batch_size=batch_size, stateful=True,
                    return_sequences= True if i < len(layers) - 3 else False )
                    )
        model.add(Dense(layers[-1], activation=dense_activation, kernel_initializer='zeros', bias_initializer='zeros'))        
        return model 
Example 8
Project: dlopt   Author: acamero   File: rnn.py    GNU General Public License v3.0 6 votes vote down vote up
def _build_model(self, layers, dense_activation):
        self.hidden_layers = len(layers) - 2
        self.layers = layers
        self.input_dim = layers[0]
        self.output_dim = layers[-1]
        model = Sequential()
        for i in range(len(layers) - 2):
            model.add(
                    LSTM(
                    #SimpleRNN(
                    input_dim=layers[i],
                    output_dim=layers[i+1],
                    kernel_initializer='zeros', 
                    recurrent_initializer='zeros',
                    bias_initializer='zeros',
                    # Uncomment to use last batch state to init next training step.
                    # Specify shuffle=False when calling fit() 
                    #batch_size=batch_size, stateful=True,
                    return_sequences= True if i < len(layers) - 3 else False )
                    )
        model.add(Dense(layers[-1], activation=dense_activation, kernel_initializer='zeros', bias_initializer='zeros'))        
        return model 
Example 9
Project: copper_price_forecast   Author: liyinwei   File: lstm.py    GNU General Public License v3.0 6 votes vote down vote up
def build_model(layers):
    """
    模型定义
    """
    model = Sequential()

    model.add(LSTM(units=layers[1], input_shape=(layers[1], layers[0]), return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(layers[2], return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(units=layers[3]))
    model.add(Activation("tanh"))

    start = time.time()
    model.compile(loss="mse", optimizer="rmsprop")
    print("> Compilation Time : ", time.time() - start)
    return model 
Example 10
Project: copper_price_forecast   Author: liyinwei   File: co_lstm_predict_day.py    GNU General Public License v3.0 6 votes vote down vote up
def build_model():
    """
    定义模型
    """
    model = Sequential()

    model.add(LSTM(units=Conf.LAYERS[1], input_shape=(Conf.LAYERS[1], Conf.LAYERS[0]), return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(Conf.LAYERS[2], return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(units=Conf.LAYERS[3]))
    # model.add(BatchNormalization(weights=None, epsilon=1e-06, momentum=0.9))
    model.add(Activation("tanh"))
    # act = PReLU(alpha_initializer='zeros', weights=None)
    # act = LeakyReLU(alpha=0.3)
    # model.add(act)

    start = time.time()
    model.compile(loss="mse", optimizer="rmsprop")
    print("> Compilation Time : ", time.time() - start)
    return model 
Example 11
Project: copper_price_forecast   Author: liyinwei   File: co_lstm_predict_sequence.py    GNU General Public License v3.0 6 votes vote down vote up
def build_model():
    """
    定义模型
    """
    model = Sequential()

    model.add(LSTM(units=Conf.LAYERS[1], input_shape=(Conf.LAYERS[1], Conf.LAYERS[0]), return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(Conf.LAYERS[2], return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(units=Conf.LAYERS[3]))
    # model.add(BatchNormalization(weights=None, epsilon=1e-06, momentum=0.9))
    model.add(Activation("tanh"))
    # act = PReLU(alpha_initializer='zeros', weights=None)
    # act = LeakyReLU(alpha=0.3)
    # model.add(act)

    start = time.time()
    model.compile(loss="mse", optimizer="rmsprop")
    print("> Compilation Time : ", time.time() - start)
    return model 
Example 12
Project: keras-fake-news-generator-and-detector   Author: chen0040   File: doc2vec.py    MIT License 6 votes vote down vote up
def __init__(self, config, target_seq_length=None):
        if target_seq_length is None:
            target_seq_length = GLOVE_EMBEDDING_SIZE
        self.num_input_tokens = config['num_input_tokens']
        self.word2idx = config['word2idx']
        self.idx2word = config['idx2word']
        self.max_input_seq_length = config['max_input_seq_length']
        self.target_seq_length = target_seq_length
        self.config = config

        model = Sequential()
        model.add(Bidirectional(LSTM(LATENT_SIZE), input_shape=(self.max_input_seq_length, GLOVE_EMBEDDING_SIZE)))
        model.add(RepeatVector(self.max_input_seq_length))
        model.add(Bidirectional(LSTM(self.target_seq_length, return_sequences=True), merge_mode="sum"))
        model.compile(optimizer="sgd", loss="mse")
        self.model = model

        self.embedding = np.zeros((len(self.word2idx), GLOVE_EMBEDDING_SIZE)) 
Example 13
Project: keras-fake-news-generator-and-detector   Author: chen0040   File: recurrent_networks.py    MIT License 6 votes vote down vote up
def __init__(self, config):
        self.num_input_tokens = config['num_input_tokens']
        self.max_input_seq_length = config['max_input_seq_length']
        self.num_target_tokens = config['num_target_tokens']
        self.word2idx = config['word2idx']
        self.idx2word = config['idx2word']
        self.config = config

        model = Sequential()
        model.add(Embedding(input_dim=self.num_input_tokens, output_dim=EMBEDDING_SIZE,
                            input_length=self.max_input_seq_length))
        model.add(SpatialDropout1D(0.2))
        model.add(LSTM(units=64, dropout=0.2, recurrent_dropout=0.2))
        model.add(Dense(self.num_target_tokens, activation='softmax'))
        model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
        self.model = model 
Example 14
Project: QUANTAXIS   Author: QUANTAXIS   File: RNN-example_using_keras.py    MIT License 6 votes vote down vote up
def build_model(layers):
    model = Sequential()

    model.add(LSTM(
        input_dim=layers[0],
        output_dim=layers[1],
        return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(
        layers[2],
        return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(
        output_dim=layers[2]))
    model.add(Activation("linear"))

    start = time.time()
    model.compile(loss="mse", optimizer="rmsprop", metrics=['accuracy'])
    print("Compilation Time : ", time.time() - start)
    return model 
Example 15
Project: QUANTAXIS   Author: QUANTAXIS   File: RNN-example_using_keras.py    MIT License 6 votes vote down vote up
def build_model2(layers):
    d = 0.2
    model = Sequential()
    model.add(LSTM(128, input_shape=(
        layers[1], layers[0]), return_sequences=True))
    model.add(Dropout(d))
    model.add(LSTM(64, input_shape=(
        layers[1], layers[0]), return_sequences=False))
    model.add(Dropout(d))
    model.add(Dense(16, init='uniform', activation='relu'))
    model.add(Dense(1, init='uniform', activation='relu'))
    model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
    return model


# In[10]: 
Example 16
Project: PClub-Project   Author: rharish101   File: sentiment_vocab.py    MIT License 6 votes vote down vote up
def create_nn(vocab_len=None, max_tweet_len=None):
    if vocab_len == None:
        print "Error: Vocabulary not initialized"
        return
    if max_tweet_len == None:
        print "Error: Please specify max tweet length"
        return

    nn_model = Sequential()
    nn_model.add(Embedding(input_dim=(vocab_len + 1), output_dim=32,
                           mask_zero=True))
    nn_model.add(LSTM(128))
    nn_model.add(Dense(32, activation='sigmoid', kernel_regularizer=l2(0.05)))
    nn_model.add(Dropout(0.3))
    nn_model.add(Dense(1, activation='sigmoid'))

    nn_model.compile(loss='binary_crossentropy', optimizer='nadam', metrics=[
                     'accuracy'])

    print "Created neural network model"
    return nn_model 
Example 17
Project: timecop   Author: BBVA   File: helpers.py    Apache License 2.0 6 votes vote down vote up
def fit_model_new(train_X, train_Y, window_size = 1):
    model2 = Sequential()
    model2.add(LSTM(input_shape = (window_size, 1),
               units = window_size,
               return_sequences = True))
    model2.add(Dropout(0.5))
    model2.add(LSTM(256))
    model2.add(Dropout(0.5))
    model2.add(Dense(1))
    model2.add(Activation("linear"))
    model2.compile(loss = "mse",
              optimizer = "adam")
    model2.summary()

    # Fit the first model.
    model2.fit(train_X, train_Y, epochs = 80,
              batch_size = 1,
              verbose = 2)
    return(model2) 
Example 18
Project: VizDoom-Keras-RL   Author: flyyufelix   File: networks.py    MIT License 6 votes vote down vote up
def drqn(input_shape, action_size, learning_rate):

        model = Sequential()
        model.add(TimeDistributed(Convolution2D(32, 8, 8, subsample=(4,4), activation='relu'), input_shape=(input_shape)))
        model.add(TimeDistributed(Convolution2D(64, 4, 4, subsample=(2,2), activation='relu')))
        model.add(TimeDistributed(Convolution2D(64, 3, 3, activation='relu')))
        model.add(TimeDistributed(Flatten()))

        # Use all traces for training
        #model.add(LSTM(512, return_sequences=True,  activation='tanh'))
        #model.add(TimeDistributed(Dense(output_dim=action_size, activation='linear')))

        # Use last trace for training
        model.add(LSTM(512,  activation='tanh'))
        model.add(Dense(output_dim=action_size, activation='linear'))

        adam = Adam(lr=learning_rate)
        model.compile(loss='mse',optimizer=adam)

        return model 
Example 19
Project: VizDoom-Keras-RL   Author: flyyufelix   File: networks.py    MIT License 6 votes vote down vote up
def a2c_lstm(input_shape, action_size, value_size, learning_rate):
        """Actor and Critic Network share convolution layers with LSTM
        """

        state_input = Input(shape=(input_shape)) # 4x64x64x3
        x = TimeDistributed(Convolution2D(32, 8, 8, subsample=(4,4), activation='relu'))(state_input)
        x = TimeDistributed(Convolution2D(64, 4, 4, subsample=(2,2), activation='relu'))(x)
        x = TimeDistributed(Convolution2D(64, 3, 3, activation='relu'))(x)
        x = TimeDistributed(Flatten())(x)

        x = LSTM(512, activation='tanh')(x)

        # Actor Stream
        actor = Dense(action_size, activation='softmax')(x)

        # Critic Stream
        critic = Dense(value_size, activation='linear')(x)

        model = Model(input=state_input, output=[actor, critic])

        adam = Adam(lr=learning_rate, clipnorm=1.0)
        model.compile(loss=['categorical_crossentropy', 'mse'], optimizer=adam, loss_weights=[1., 1.])

        return model 
Example 20
Project: applications   Author: geomstats   File: recurrent_test.py    MIT License 6 votes vote down vote up
def test_specify_initial_state_keras_tensor(layer_class):
    num_states = 2 if layer_class is recurrent.LSTM else 1

    # Test with Keras tensor
    inputs = Input((timesteps, embedding_dim))
    initial_state = [Input((units,)) for _ in range(num_states)]
    layer = layer_class(units)
    if len(initial_state) == 1:
        output = layer(inputs, initial_state=initial_state[0])
    else:
        output = layer(inputs, initial_state=initial_state)
    assert initial_state[0] in layer._inbound_nodes[0].input_tensors

    model = Model([inputs] + initial_state, output)
    model.compile(loss='categorical_crossentropy', optimizer='adam')

    inputs = np.random.random((num_samples, timesteps, embedding_dim))
    initial_state = [np.random.random((num_samples, units))
                     for _ in range(num_states)]
    targets = np.random.random((num_samples, units))
    model.fit([inputs] + initial_state, targets) 
Example 21
Project: applications   Author: geomstats   File: recurrent_test.py    MIT License 6 votes vote down vote up
def test_specify_initial_state_non_keras_tensor(layer_class):
    num_states = 2 if layer_class is recurrent.LSTM else 1

    # Test with non-Keras tensor
    inputs = Input((timesteps, embedding_dim))
    initial_state = [K.random_normal_variable((num_samples, units), 0, 1)
                     for _ in range(num_states)]
    layer = layer_class(units)
    output = layer(inputs, initial_state=initial_state)

    model = Model(inputs, output)
    model.compile(loss='categorical_crossentropy', optimizer='adam')

    inputs = np.random.random((num_samples, timesteps, embedding_dim))
    targets = np.random.random((num_samples, units))
    model.fit(inputs, targets) 
Example 22
Project: applications   Author: geomstats   File: recurrent_test.py    MIT License 6 votes vote down vote up
def test_reset_states_with_values(layer_class):
    num_states = 2 if layer_class is recurrent.LSTM else 1

    layer = layer_class(units, stateful=True)
    layer.build((num_samples, timesteps, embedding_dim))
    layer.reset_states()
    assert len(layer.states) == num_states
    assert layer.states[0] is not None
    np.testing.assert_allclose(K.eval(layer.states[0]),
                               np.zeros(K.int_shape(layer.states[0])),
                               atol=1e-4)
    state_shapes = [K.int_shape(state) for state in layer.states]
    values = [np.ones(shape) for shape in state_shapes]
    if len(values) == 1:
        values = values[0]
    layer.reset_states(values)
    np.testing.assert_allclose(K.eval(layer.states[0]),
                               np.ones(K.int_shape(layer.states[0])),
                               atol=1e-4)

    # Test fit with invalid data
    with pytest.raises(ValueError):
        layer.reset_states([1] * (len(layer.states) + 1)) 
Example 23
Project: applications   Author: geomstats   File: recurrent_test.py    MIT License 6 votes vote down vote up
def test_initial_states_as_other_inputs(layer_class):
    num_states = 2 if layer_class is recurrent.LSTM else 1

    # Test with Keras tensor
    main_inputs = Input((timesteps, embedding_dim))
    initial_state = [Input((units,)) for _ in range(num_states)]
    inputs = [main_inputs] + initial_state

    layer = layer_class(units)
    output = layer(inputs)
    assert initial_state[0] in layer._inbound_nodes[0].input_tensors

    model = Model(inputs, output)
    model.compile(loss='categorical_crossentropy', optimizer='adam')

    main_inputs = np.random.random((num_samples, timesteps, embedding_dim))
    initial_state = [np.random.random((num_samples, units))
                     for _ in range(num_states)]
    targets = np.random.random((num_samples, units))
    model.train_on_batch([main_inputs] + initial_state, targets) 
Example 24
Project: kpi2017   Author: deepmipt   File: model.py    Apache License 2.0 6 votes vote down vote up
def lstm_word_model(self):
        embed_input = Input(shape=(self.opt['max_sequence_length'], self.opt['embedding_dim'],))

        output = Bidirectional(LSTM(self.opt['units_lstm'], activation='tanh',
                                      kernel_regularizer=l2(self.opt['regul_coef_lstm']),
                                      dropout=self.opt['dropout_rate']))(embed_input)

        output = Dropout(rate=self.opt['dropout_rate'])(output)
        output = Dense(self.opt['dense_dim'], activation=None,
                       kernel_regularizer=l2(self.opt['regul_coef_dense']))(output)
        output = BatchNormalization()(output)
        output = Activation('relu')(output)
        output = Dropout(rate=self.opt['dropout_rate'])(output)
        output = Dense(1, activation=None,
                       kernel_regularizer=l2(self.opt['regul_coef_dense']))(output)
        output = BatchNormalization()(output)
        act_output = Activation('sigmoid')(output)
        model = Model(inputs=embed_input, outputs=act_output)
        return model 
Example 25
Project: LSTM_generator   Author: MCSH   File: model.py    MIT License 6 votes vote down vote up
def get_model(Config, maxlen, chars):
    model_name = Config['model_name']
    has_model = os.path.isfile(model_name)

    if has_model:
        print('Load model...')
        return load_model(model_name)
    else:
        print('Build model...')
        LSTM_size = Config['LSTM_size']
        LSTM_count = Config['LSTM_count']
        LSTM_STATEFUL = Config['stateful']
        model = Sequential()
        model.add(LSTM(LSTM_size, return_sequences=True, input_shape=(maxlen, len(chars)), batch_size=Config['batch_size'], stateful=LSTM_STATEFUL))
        model.add(Dropout(0.2))
        for i in range(LSTM_count - 2):
            model.add(LSTM(LSTM_size, return_sequences=True, stateful=LSTM_STATEFUL))
            model.add(Dropout(0.2))
        model.add(LSTM(LSTM_size, return_sequences=False, stateful=LSTM_STATEFUL))
        model.add(Dropout(0.2))
        model.add(Dense(len(chars)))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
        return model 
Example 26
Project: shell-complete   Author: src-d   File: test_model2correct.py    Apache License 2.0 6 votes vote down vote up
def setUp(self):
        self.log = logging.getLogger("test")
        self.log.setLevel(logging.INFO)
        self.path_to_vocab = "shcomplete/tests/data/vocab_0.01.txt"
        self.chars = get_chars(self.path_to_vocab)
        self.path_to_corpus = "shcomplete/tests/data/corpus.txt"
        self.models_directory = "shcomplete/tests/data"
        self.args = argparse.Namespace(vocabulary=self.path_to_vocab, corpus=self.path_to_corpus,
                                       models_directory=self.models_directory, max_cmd_len=40,
                                       input_layers=1, hidden_layers=4, output_layers=1,
                                       dropout=0.2, batch_size=32, level_noise=0.4,
                                       nb_predictions=2, nb_epochs=1, steps_per_epoch=64,
                                       from_model=None, checkpoint=2, optimizer="adam",
                                       cell_type=recurrent.LSTM)
        self.model = generate_model(self.args, nb_features=len(self.chars)+1,
                                    input_length=self.args.max_cmd_len,
                                    nb_repeats=self.args.max_cmd_len) 
Example 27
Project: DeepLearning   Author: STHSF   File: lstm.py    MIT License 6 votes vote down vote up
def build_model(layers):
    # 建立keras序贯模型
    model = Sequential()

    model.add(LSTM(units=layers[2], input_dim=layers[0], input_length=layers[1], return_sequences=True))
    # model.add(LSTM(output_dim=layers[2], input_dim=layers[0], input_length=layers[1], return_sequences=True))
    model.add(Dropout(0.2))
    # 添加隐藏层
    model.add(LSTM(units=layers[2], return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(output_dim=layers[3]))
    model.add(Activation("linear"))

    start = time.time()
    model.compile(loss="mse", optimizer="rmsprop")
    print("> Compilation Time : ", time.time() - start)
    return model 
Example 28
Project: Kutils   Author: ishank26   File: skopt.py    MIT License 5 votes vote down vote up
def my_model(dropout):
    ############ model params ################
    line_length = 248  # seq size
    train_char = 58
    hidden_neurons = 512  # hidden neurons
    batch = 64  # batch_size
    no_epochs = 5
    ################### Model ################
    model = Sequential()
    # layer 1
    model.add(LSTM(hidden_neurons, return_sequences=True,
                   input_shape=(line_length, train_char)))
    model.add(Dropout(dropout))
    # layer 2
    model.add(LSTM(hidden_neurons, return_sequences=True))
    model.add(Dropout(dropout))
    # layer 3
    model.add(LSTM(hidden_neurons, return_sequences=True))
    model.add(Dropout(dropout))
    model.add(Reshape((248, 512)))
    # fc layer
    model.add(TimeDistributed(Dense(58, activation='softmax')))
    # model.load_weights("weights/model_maha1_noep50_batch64_seq_248.hdf5")
    # model.layers.pop()
    # model.layers.pop()
    # model.add(Dropout(dropout))
    #model.add(TimeDistributed(Dense(train_char, activation='softmax')))
    initlr = 0.00114
    adagrad = Adagrad(lr=initlr, epsilon=1e-08)
    model.compile(optimizer=adagrad,
                  loss='categorical_crossentropy', metrics=['accuracy'])
    ###load weights####
    return model 
Example 29
Project: Kutils   Author: ishank26   File: hypopt.py    MIT License 5 votes vote down vote up
def my_model(X_train, y_train, X_test, y_test):
    ############ model params ################
    line_length = 248  # seq size
    train_char = 58
    hidden_neurons = 512  # hidden neurons
    batch = 64  # batch_size
    no_epochs = 3
    ################### Model ################

    ######### begin model ########
    model = Sequential()
    # layer 1
    model.add(LSTM(hidden_neurons, return_sequences=True,
                   input_shape=(line_length, train_char)))
    model.add(Dropout({{choice([0.4, 0.5, 0.6, 0.7, 0.8])}}))
    # layer 2
    model.add(LSTM(hidden_neurons, return_sequences=True))
    model.add(Dropout({{choice([0.4, 0.5, 0.6, 0.7, 0.8])}}))
    # layer 3
    model.add(LSTM(hidden_neurons, return_sequences=True))
    model.add(Dropout({{choice([0.4, 0.5, 0.6, 0.7, 0.8])}}))
    # fc layer
    model.add(TimeDistributed(Dense(train_char, activation='softmax')))
    model.load_weights("weights/model_maha1_noep50_batch64_seq_248.hdf5")
    ########################################################################
    checkpoint = ModelCheckpoint("weights/hypmodel2_maha1_noep{0}_batch{1}_seq_{2}.hdf5".format(
        no_epochs, batch, line_length), monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='min')

    initlr = 0.00114
    adagrad = Adagrad(lr=initlr, epsilon=1e-08,
                      clipvalue={{choice([0, 1, 2, 3, 4, 5, 6, 7])}})
    model.compile(optimizer=adagrad,
                  loss='categorical_crossentropy', metrics=['accuracy'])
    history = History()
    # fit model
    model.fit(X_train, y_train, batch_size=batch, nb_epoch=no_epochs,
              validation_split=0.2, callbacks=[history, checkpoint])

    score, acc = model.evaluate(X_test, y_test, verbose=0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model} 
Example 30
Project: BOP2017   Author: crh19970307   File: output.py    MIT License 5 votes vote down vote up
def train():
	data=loadfromjson()
	taglist=[]
	for index,item in enumerate(data['datalist']):
		if item[0]=='0':
			taglist.append(0)
		else:
			if item[0]=='1':
				taglist.append(1)
			else:
				print('ERROR\n')
				print(index)
		#print(len(data['vectorlist'][index]))
	#xa=np.array(data['vectorlist'])
	xa=np.zeros((len(data['vectorlist']),50,60),dtype='float64')
	for index1,items in enumerate(data['vectorlist']):
		for index2,item2 in enumerate(items):
			if index2==50:
				break
			xa[index1][index2]=item2

	#xa=np.random.rand(len(data['vectorlist']),50,60)
	ya=np.array(taglist)
	#print(np.size(xa))
	#print(np.size(ya))
	print('Build model...')  
	model = Sequential()  
	#model.add(Embedding(60,32))  
	#model.add(LSTM(128)) # try using a GRU instead, for fun  
	#model.add(LSTM(32,input_shape=(10,60)))
	#model.add(LSTM(32,input_length=50,input_dim=60))
	model.add(LSTM(32,input_dim=60))
	print('LSTM added')
	model.add(Dropout(0.5))  	
	model.add(Dense( 1))  
	model.add(Activation('sigmoid'))  
	model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")  
	model.fit(xa, ya, batch_size=16, nb_epoch=100) #训练时间为若干个小时  
	model.save('my_model.h5') 
Example 31
Project: BOP2017   Author: crh19970307   File: output.py    MIT License 5 votes vote down vote up
def train():
	data=loadfromjson()
	taglist=[]
	for index,item in enumerate(data['datalist']):
		if item[0]=='0':
			taglist.append(0)
		else:
			if item[0]=='1':
				taglist.append(1)
			else:
				print('ERROR\n')
				print(index)
		#print(len(data['vectorlist'][index]))
	#xa=np.array(data['vectorlist'])
	xa=np.zeros((len(data['vectorlist']),50,60),dtype='float64')
	for index1,items in enumerate(data['vectorlist']):
		for index2,item2 in enumerate(items):
			if index2==50:
				break
			xa[index1][index2]=item2

	#xa=np.random.rand(len(data['vectorlist']),50,60)
	ya=np.array(taglist)
	#print(np.size(xa))
	#print(np.size(ya))
	print('Build model...')  
	model = Sequential()  
	#model.add(Embedding(60,32))  
	#model.add(LSTM(128)) # try using a GRU instead, for fun  
	#model.add(LSTM(32,input_shape=(10,60)))
	#model.add(LSTM(32,input_length=50,input_dim=60))
	model.add(LSTM(32,input_dim=60))
	print('LSTM added')
	model.add(Dropout(0.5))  	
	model.add(Dense( 1))  
	model.add(Activation('sigmoid'))  
	model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")  
	model.fit(xa, ya, batch_size=16, nb_epoch=100)
	model.save('my_model.h5') 
Example 32
Project: BOP2017   Author: crh19970307   File: train.py    MIT License 5 votes vote down vote up
def train():
	data=loadfromjson()
	taglist=[]
	for index,item in enumerate(data['datalist']):
		if item[0]=='0':
			taglist.append(0)
		else:
			if item[0]=='1':
				taglist.append(1)
			else:
				print('EiRROR\n')
				print(index)
				taglist.append(0)
		#print(len(data['vectorlist'][index]))
	#xa=np.array(data['vectorlist'])
	xa=np.zeros((len(data['vectorlist']),30,60),dtype='float64')
	for index1,items in enumerate(data['vectorlist']):
		for index2,item2 in enumerate(items):
			if index2==30:
				break
			xa[index1][index2]=item2

	#xa=np.random.rand(len(data['vectorlist']),50,60)
	ya=np.array(taglist)
	#print(np.size(xa))
	#print(np.size(ya))
	print('Build model...')  
	model = Sequential()  
	#model.add(Embedding(60,32))  
	#model.add(LSTM(128)) # try using a GRU instead, for fun  
	#model.add(LSTM(32,input_shape=(10,60)))
	#model.add(LSTM(32,input_length=50,input_dim=60))
	model.add(LSTM(128,input_length=30,input_dim=60))
	print('LSTM added')
	model.add(Dropout(0.5))  	
	model.add(Dense( 1))  
	model.add(Activation('sigmoid'))  
	model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")  
	model.fit(xa, ya, batch_size=128, nb_epoch=100) #训练时间为若干个小时  
	model.save('sentencelen30_230000_lstm128_epoch200_model.h5') 
Example 33
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def main():
    window_length = 10
    select_only_last_state = False
    model_file = 'model.h5'
    hidden_dim = 16

    # no outliers.
    signal_train, _ = get_signal(100000, outliers_size=0)
    x_train, _ = prepare_sequences(signal_train, window_length, [])

    # 1 percent are outliers.
    signal_test, random_indices = get_signal(100000, outliers_size=0.01)
    x_test, contain_outliers = prepare_sequences(signal_test, window_length, random_indices)
    outlier_indices = np.where(contain_outliers)[0]

    if os.path.isfile(model_file):
        m = load_model(model_file)
    else:
        m = Sequential()
        if select_only_last_state:
            m.add(LSTM(hidden_dim, input_shape=(window_length, 1), return_sequences=False))
            m.add(RepeatVector(window_length))
        else:
            m.add(LSTM(hidden_dim, input_shape=(window_length, 1), return_sequences=True))
        m.add(Dropout(p=0.1))
        m.add(LSTM(1, return_sequences=True, activation='linear'))
        m.compile(loss='mse', optimizer='adam')
        m.fit(x_train, x_train, batch_size=64, nb_epoch=5, validation_data=(x_test, x_test))
        m.save(model_file)

    pred_x_test = m.predict(x_test)
    mae_of_predictions = np.squeeze(np.max(np.square(pred_x_test - x_test), axis=1))
    mae_threshold = np.mean(mae_of_predictions) + np.std(mae_of_predictions)  # can use a running mean instead.
    actual = np.where(mae_of_predictions > mae_threshold)[0]

    tp, fn, fp, tn = tp_fn_fp_tn(set(range(len(pred_x_test))), outlier_indices, actual)
    precision = float(tp) / (tp + fp)
    hit_rate = float(tp) / (tp + fn)
    accuracy = float(tp + tn) / (tp + tn + fp + fn)

    print('precision = {}, hit_rate = {}, accuracy = {}'.format(precision, hit_rate, accuracy)) 
Example 34
Project: CAPTCHA-breaking   Author: lllcho   File: check_autoencoder.py    MIT License 5 votes vote down vote up
def build_lstm_autoencoder(autoencoder, X_train, X_test):
    X_train = X_train[:, np.newaxis, :]
    X_test = X_test[:, np.newaxis, :]
    print("Modified X_train: ", X_train.shape)
    print("Modified X_test: ", X_test.shape)

    # The TimeDistributedDense isn't really necessary, however you need a lot of GPU memory to do 784x394-394x784
    autoencoder.add(TimeDistributedDense(input_dim, 16))
    autoencoder.add(AutoEncoder(encoder=LSTM(16, 8, activation=activation, return_sequences=True),
                                decoder=LSTM(8, input_dim, activation=activation, return_sequences=True),
                                output_reconstruction=False))
    return autoencoder, X_train, X_test 
Example 35
Project: CAPTCHA-breaking   Author: lllcho   File: test_recurrent.py    MIT License 5 votes vote down vote up
def test_lstm(self):
        _runner(recurrent.LSTM) 
Example 36
Project: stock-price-prediction   Author: chinuy   File: classifier.py    MIT License 5 votes vote down vote up
def performRNNlass(X_train, y_train):
    X_train = numpy.reshape(numpy.array(X_train), (X_train.shape[0], 1, X_train.shape[1]))
    model = Sequential()

    model.add(LSTM(
        128,
        input_shape=(None, X_train.shape[2]),
        return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(
        100,
        return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(
        units=1))
    model.add(Activation('sigmoid'))

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

    model.fit(
        X_train,
        y_train,
        batch_size=64,
        epochs=64,
        validation_split=0.1)
    return model 
Example 37
Project: StockRecommendSystem   Author: doncat99   File: Stock_Prediction_Model_Stateless_LSTM.py    MIT License 5 votes vote down vote up
def best_model(self, X_train, y_train, X_test, y_test):
        self.train_x = X_train
        self.train_y = y_train
        self.test_x  = X_test
        self.test_y  = y_test

        algo = partial(tpe.suggest, n_startup_jobs=1)
        best = fmin(self.LSTM, space=self.paras.hyper_opt, algo=algo, max_evals=20)
        print("best", best)
        return best 
Example 38
Project: StockRecommendSystem   Author: doncat99   File: Stock_Prediction_Model_Stateless_LSTM.py    MIT License 5 votes vote down vote up
def build_model(self, window, X_train, y_train, X_test, y_test):
        if self.paras.load == True:
            model = self.load_training_model(window)
            if model != None:
                return model

        best = {}
        file_name = "hyper_parameter_lstm_" + str(window) + ".pkl"

        if self.paras.run_hyperopt == True:
            print('find hyper parameters...')
            best = self.best_model(X_train, y_train, X_test, y_test)
            pickle.dump(best, open(file_name, "wb"))
        else:
            if os.path.exists(file_name):
                best = pickle.load(open(file_name, "rb"))

        if len(best) != 0:
            self.paras.batch_size             = self.paras.hyper_opt['batch_size_opt'][best["batch_size"]]
            self.paras.model['dropout']       = best['dropout']
            self.paras.model['activation']    = self.paras.hyper_opt['activation_opt'][best["activation"]]
            self.paras.model['optimizer']     = self.paras.hyper_opt['optimizer_opt'][best["optimizer"]]
            self.paras.model['learning_rate'] = best["learning_rate"]

        print('build LSTM model...')
        return self.lstm_model() 
Example 39
Project: StockRecommendSystem   Author: doncat99   File: Stock_Prediction_Model_Stateless_LSTM.py    MIT License 5 votes vote down vote up
def save_training_model(self, model, window_len):
        if self.paras.save == True:
            print('save LSTM model...')
            # https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model
            model.save(self.paras.model_folder + self.get_model_name(window_len) + '.h5')  # creates a HDF5 file 'my_model.h5' 
Example 40
Project: StockRecommendSystem   Author: doncat99   File: Stock_Prediction_Model_Stateless_LSTM.py    MIT License 5 votes vote down vote up
def run(self, train, predict):
        if self.check_parameters() == False:
            raise IndexError('Parameters for LSTM is wrong, check out_class_type')

        ################################################################################
        self.paras.save_folder = self.get_save_directory()
        print(' Log  Directory: ', self.paras.save_folder)
        self.paras.model_folder = self.get_model_directory()
        print('Model Directory: ', self.paras.model_folder)
        ################################################################################

        for window in self.paras.window_len:
            self.do_run(train, predict, window) 
Example 41
Project: PhilosophyLSTM   Author: guilherme-pombo   File: lstm_model.py    MIT License 5 votes vote down vote up
def create_model(word_coding):
    """
    Create the LSTM model
    :param word_coding:
    :return:
    """
    model = Graph()
    model.add_input(name='input', input_shape=(sd_len, input_dim))
    model.add_node(TimeDistributedDense(input_dim=input_dim, output_dim=lstm_hdim, input_length=sd_len),
                   name=layerNames[0], input='input')
    model.add_node(BatchNormalization(), name=layerNames[1], input=layerNames[0])

    model.add_node(LSTM(input_dim=lstm_hdim, output_dim=lstm_hdim, return_sequences=True), name=layerNames[2] + 'left',
                   input=layerNames[1])
    model.add_node(BatchNormalization(), name=layerNames[3] + 'left', input=layerNames[2] + 'left')

    model.add_node(LSTM(input_dim=lstm_hdim, output_dim=lstm_hdim, return_sequences=True, go_backwards=True),
                   name=layerNames[2] + 'right', input=layerNames[1])
    model.add_node(BatchNormalization(), name=layerNames[3] + 'right', input=layerNames[2] + 'right')

    model.add_node(LSTM(input_dim=lstm_hdim, output_dim=lstm_hdim, return_sequences=False), name=layerNames[6] + 'left',
                   input=layerNames[3] + 'left')

    model.add_node(LSTM(input_dim=lstm_hdim, output_dim=lstm_hdim, return_sequences=False, go_backwards=True),
                   name=layerNames[6] + 'right', input=layerNames[3] + 'right')

    model.add_node(BatchNormalization(), name=layerNames[7], inputs=[layerNames[6] + 'left', layerNames[6] + 'right'])
    model.add_node(Dropout(0.2), name=layerNames[8], input=layerNames[7])

    model.add_node(Dense(input_dim=bridge_dim, output_dim=dense_dim), name=layerNames[9], input=layerNames[8])
    model.add_node(ELU(), name=layerNames[10], input=layerNames[9])
    model.add_node(Dropout(0.2), name=layerNames[11], input=layerNames[10])

    model.add_node(Dense(input_dim=dense_dim, output_dim=len(word_coding)), name=layerNames[12], input=layerNames[11])
    model.add_node(Activation('softmax'), name=layerNames[13], input=layerNames[12])
    model.add_output(name='output1', input=layerNames[13])

    model.compile(optimizer='rmsprop', loss={'output1': 'categorical_crossentropy'})

    return model 
Example 42
Project: dlopt   Author: acamero   File: rnn.py    GNU General Public License v3.0 5 votes vote down vote up
def _build_lstm_model(self, layers, drop_out, dense_activation):
        model = Sequential()
        for i in range(len(layers) - 2):
            model.add(LSTM(
                    input_dim=layers[i],
                    output_dim=layers[i+1], 
                    #stateful=True,
                    return_sequences= True if i < len(layers) - 3 else False ))
            model.add(Dropout(drop_out))
    
        model.add(Dense(layers[-1], activation=dense_activation))
        model.summary()
        return model 
Example 43
Project: dlopt   Author: acamero   File: rnn.py    GNU General Public License v3.0 5 votes vote down vote up
def _build_lstm_model(self, layers, drop_out, dense_activation):
        model = Sequential()
        for i in range(len(layers) - 2):
            model.add(LSTM(
                    input_dim=layers[i],
                    output_dim=layers[i+1], 
                    #stateful=True,
                    return_sequences= True if i < len(layers) - 3 else False ))
            model.add(Dropout(drop_out))
    
        model.add(Dense(layers[-1], activation=dense_activation))
        model.summary()
        return model 
Example 44
Project: SPaT_Prediction   Author: priscillaboyd   File: RNN_LSTM.py    Apache License 2.0 5 votes vote down vote up
def build_model():
    """
    Build the learning RNN model using Keras (Sequential) module.

    :return: RNN model
    :rtype: History object
    """
    model = Sequential()

    # declare the sizes of the layers (1d input and output)
    layers = [1, 50, 100, 1]

    # first hidden layer, using linear activation (not specified)
    model.add(LSTM(layers[1], input_shape=(None, layers[0]), return_sequences=True))
    model.add(Dropout(0.2))

    # second hidden layer
    model.add(LSTM(layers[2], return_sequences=False))
    model.add(Dropout(0.2))

    # third hidden layer
    model.add(Dense(layers[3]))
    model.add(Activation("linear"))

    # compile using MSE as loss function for regression, RMSPROP as optimiser
    model.compile(loss="mse", optimizer="RMSProp", metrics=['accuracy'])

    # return the model
    return model 
Example 45
Project: SPaT_Prediction   Author: priscillaboyd   File: RNN_LSTM.py    Apache License 2.0 5 votes vote down vote up
def run_rnn(file):
    # define model params
    """
    Run the process to train/test a recurrent neural network using LSTM using a given dataset file.

    :param string file: Location of CSV-formatted dataset file
    :return: Model with expected (test) targets and associated scores
    :rtype: object, dataframe, object
    """
    num_epochs = 2
    sequence_length = 20

    # grab train and test data from CSV
    X_train, y_train, X_test, y_test = split_test_training(file, sequence_length)

    print(X_train)

    # build model
    model = build_model()
    model.fit(X_train, y_train, epochs=num_epochs, batch_size=64, validation_split=0.2)

    # predict
    predict = model.predict(X_test)
    predict = np.reshape(predict, predict.size)

    # evaluate
    score = model.evaluate(X_test, y_test, verbose=0)
    print("Accuracy: ", score[1]*100, "%")

    # save model to h5 file (same folder as data)
    model_location_folder = get_latest_dataset_folder()
    model.save(model_location_folder + '/RNN_' + current_dt + '.h5')

    return model, y_test, predict 
Example 46
Project: PClub-Project   Author: rharish101   File: sentiment_wv.py    MIT License 5 votes vote down vote up
def create_nn(max_tweet_len=None):
    if max_tweet_len == None:
        print "Error: Please specify max tweet length"

    nn_model = Sequential()
    nn_model.add(LSTM(128, input_shape=(max_tweet_len, 32)))
    nn_model.add(Dense(1, activation='sigmoid'))

    nn_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=[
                     'accuracy'])

    print "Created neural network model"
    return nn_model 
Example 47
Project: timecop   Author: BBVA   File: lstm.py    Apache License 2.0 5 votes vote down vote up
def add_hlayer(model, num_nodes, return_sequences=False):
    model.add(LSTM(num_nodes, return_sequences=return_sequences)) 
Example 48
Project: timecop   Author: BBVA   File: lstm.py    Apache License 2.0 5 votes vote down vote up
def define_model(n_nodes, n_hlayers, dropout, input_data, output_shape):
    model = Sequential()
    if n_hlayers == 1:
        model.add(LSTM(units =int(n_nodes), activation='relu', input_shape =(input_data.shape[1], input_data.shape[2]),return_sequences=False))
    else:
        #model.add(LSTM(output_dim =int(n_nodes), activation='relu', input_shape =(input_data.shape[1], input_data.shape[2]),return_sequences=True))
        model.add(LSTM(activation='relu', input_shape =(input_data.shape[1], input_data.shape[2]),return_sequences=True,units =int(n_nodes) ))
    model.add(Dropout(dropout))
    #print(n_hlayers)

    for i in range(n_hlayers-1):
        #print(i)
        if i == n_hlayers-2:
            #add_hlayer(model, n_nodes, return_sequences=False)
            model.add(LSTM(n_nodes, return_sequences=False))
            model.add(Dropout(dropout))
            model.add(BatchNormalization())
        else:
            #add_hlayer(model, n_nodes, return_sequences=True)
            model.add(LSTM(n_nodes, return_sequences=True))
            model.add(Dropout(dropout))
            model.add(BatchNormalization())

    model.add(Dense(int(n_nodes/2), activation='relu'))
    model.add(Dropout(dropout))

    #model.add(Dense(output_dim=int(output_shape)))
    model.add(Dense(units=int(output_shape)))

    model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
    return model 
Example 49
Project: neural-fuzzer   Author: CIFASIS   File: neural-fuzzer.py    GNU General Public License v3.0 5 votes vote down vote up
def define_model(input_dim, output_dim):

    model = Sequential()
    model.add(LSTM(64, return_sequences=True, input_shape=input_dim))
    model.add(Dropout(0.2))
    model.add(LSTM(64, return_sequences=True, input_shape=input_dim))
    model.add(Dropout(0.2))
    model.add(LSTM(64, return_sequences=False))
    model.add(Dropout(0.2))
    model.add(Dense(output_dim))
    model.add(Activation('softmax'))
    return model 
Example 50
Project: cervantes   Author: textclf   File: models.py    MIT License 5 votes vote down vote up
def _generate_model(self, lembedding, num_classes=2, unit='gru', rnn_size=128, train_vectors=True):

        model = Sequential()
        if lembedding.vector_box.W is None:
            emb = Embedding(lembedding.vector_box.size,
                            lembedding.vector_box.vector_dim,
                            W_constraint=None)
        else:
            emb = Embedding(lembedding.vector_box.size,
                            lembedding.vector_box.vector_dim,
                            weights=[lembedding.vector_box.W], W_constraint=None)
        emb.trainable = train_vectors
        model.add(emb)
        if unit == 'gru':
            model.add(GRU(rnn_size))
        else:
            model.add(LSTM(rnn_size))
        model.add(Dropout(0.2))
        if num_classes == 2:
            model.add(Dense(1, activation='sigmoid'))
            if self.optimizer is None:
                self.optimizer = 'rmsprop'
            model.compile(loss='binary_crossentropy', optimizer=self.optimizer, metrics=["accuracy"])
        else:
            if self.optimizer is None:
                self.optimizer = 'adam'
            model.add(Dense(num_classes, activation='softmax'))
            model.compile(loss='categorical_crossentropy', optimizer=self.optimizer, metrics=["accuracy"])

        return model 
Example 51
Project: cervantes   Author: textclf   File: models.py    MIT License 5 votes vote down vote up
def _generate_model(self, lembedding, num_classes=2, unit='gru', rnn_size=128, train_vectors=True):

        input = Input(shape=(lembedding.size,), dtype='int32')
        if lembedding.vector_box.W is None:
            emb = Embedding(lembedding.vector_box.size,
                            lembedding.vector_box.vector_dim,
                            W_constraint=None)(input)
        else:
            emb = Embedding(lembedding.vector_box.size,
                            lembedding.vector_box.vector_dim,
                            weights=[lembedding.vector_box.W], W_constraint=None, )(input)
        emb.trainable = train_vectors
        if unit == 'gru':
            forward = GRU(rnn_size)(emb)
            backward = GRU(rnn_size, go_backwards=True)(emb)
        else:
            forward = LSTM(rnn_size)(emb)
            backward = LSTM(rnn_size, go_backwards=True)(emb)

        merged_rnn = merge([forward, backward], mode='concat')
        dropped = Dropout(0.5)(merged_rnn)
        if num_classes == 2:
            out = Dense(1, activation='sigmoid')(dropped)
            model = Model(input=input, output=out)
            if self.optimizer is None:
                self.optimizer = 'rmsprop'
            model.compile(loss='binary_crossentropy', optimizer=self.optimizer, metrics=["accuracy"])
        else:
            out = Dense(num_classes, activation='softmax')(dropped)
            model = Model(input=input, output=out)
            if self.optimizer is None:
                self.optimizer = 'adam'
            model.compile(loss='categorical_crossentropy', optimizer=self.optimizer, metrics=["accuracy"])

        return model 
Example 52
Project: code-of-csdn   Author: ZhiqiangHo   File: lstm.py    MIT License 5 votes vote down vote up
def normalise_windows(window_data):
    normalised_data = []
    for window in window_data:
        normalised_window = [((float(p) / float(window[0])) - 1) for p in window]
        normalised_data.append(normalised_window)
    return normalised_data
# def build_model(layers):
#     model = Sequential()
#
#     model.add(LSTM(
#         input_dim=layers[0],
#         output_dim=layers[1],
#         return_sequences=True))
#     model.add(Dropout(0.2))
#
#     model.add(LSTM(
#         layers[2],
#         return_sequences=False))
#     model.add(Dropout(0.2))
#
#     model.add(Dense(
#         output_dim=layers[3]))
#     model.add(Activation("linear"))
#
#     start = time.time()
#     model.compile(loss="mse", optimizer="rmsprop")
#     print("Compilation Time : ", time.time() - start)
#     return model 
Example 53
Project: applications   Author: geomstats   File: recurrent_test.py    MIT License 5 votes vote down vote up
def test_return_state(layer_class):
    num_states = 2 if layer_class is recurrent.LSTM else 1

    inputs = Input(batch_shape=(num_samples, timesteps, embedding_dim))
    layer = layer_class(units, return_state=True, stateful=True)
    outputs = layer(inputs)
    output, state = outputs[0], outputs[1:]
    assert len(state) == num_states
    model = Model(inputs, state[0])

    inputs = np.random.random((num_samples, timesteps, embedding_dim))
    state = model.predict(inputs)
    np.testing.assert_allclose(K.eval(layer.states[0]), state, atol=1e-4) 
Example 54
Project: StarTrader   Author: jiewwantan   File: compare.py    MIT License 5 votes vote down vote up
def generate_signals(self, predicted_tomorrow_close):
        """
        Generate trade signla from the prediction of the LSTM model
        :param predicted_tomorrow_close:
        :return:
        """

        predicted_tomorrow_close.columns = self.stock_price.columns
        predicted_next_day_returns = (predicted_tomorrow_close / predicted_tomorrow_close.shift(1) - 1).dropna()
        next_day_returns = (self.stock_price / self.stock_price.shift(1) - 1).dropna()
        signals = pd.DataFrame(index=predicted_tomorrow_close.index, columns=self.stock_price.columns)

        for s in self.stock_price.columns:
            for d in next_day_returns.index:
                if predicted_tomorrow_close[s].loc[d] > self.stock_price[s].loc[d] and next_day_returns[s].loc[
                    d] > 0 and predicted_next_day_returns[s].loc[d] > 0:
                    signals[s].loc[d] = 2
                elif predicted_tomorrow_close[s].loc[d] < self.stock_price[s].loc[d] and next_day_returns[s].loc[
                    d] < 0 and predicted_next_day_returns[s].loc[d] < 0:
                    signals[s].loc[d] = -2
                elif predicted_tomorrow_close[s].loc[d] > self.stock_price[s].loc[d]:
                    signals[s].loc[d] = 2
                elif next_day_returns[s].loc[d] > 0:
                    signals[s].loc[d] = 1
                elif next_day_returns[s].loc[d] < 0:
                    signals[s].loc[d] = -1
                elif predicted_next_day_returns[s].loc[d] > 0:
                    signals[s].loc[d] = 2
                elif predicted_next_day_returns[s].loc[d] < 0:
                    signals[s].loc[d] = -1
                else:
                    signals[s].loc[d] = 0
        signals.loc[self.stock_price.index[0]] = [0, 0, 0, 0, 0]
        self.signals = signals 
Example 55
Project: DeepSequenceClassification   Author: napsternxg   File: model.py    GNU General Public License v2.0 5 votes vote down vote up
def gen_model(vocab_size=100, embedding_size=128, maxlen=100, output_size=6, hidden_layer_size=100, num_hidden_layers = 1, RNN_LAYER_TYPE="LSTM"):
    RNN_CLASS = LSTM
    if RNN_LAYER_TYPE == "GRU":
        RNN_CLASS = GRU
    logger.info("Parameters: vocab_size = %s, embedding_size = %s, maxlen = %s, output_size = %s, hidden_layer_size = %s, " %\
            (vocab_size, embedding_size, maxlen, output_size, hidden_layer_size))
    logger.info("Building Model")
    model = Sequential()
    logger.info("Init Model with vocab_size = %s, embedding_size = %s, maxlen = %s" % (vocab_size, embedding_size, maxlen))
    model.add(Embedding(vocab_size, embedding_size, input_length=maxlen))
    logger.info("Added Embedding Layer")
    model.add(Dropout(0.5))
    logger.info("Added Dropout Layer")
    for i in xrange(num_hidden_layers):
        model.add(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True))
        logger.info("Added %s Layer" % RNN_LAYER_TYPE)
        model.add(Dropout(0.5))
        logger.info("Added Dropout Layer")
    model.add(RNN_CLASS(output_dim=output_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True))
    logger.info("Added %s Layer" % RNN_LAYER_TYPE)
    model.add(Dropout(0.5))
    logger.info("Added Dropout Layer")
    model.add(TimeDistributedDense(output_size, activation="softmax"))
    logger.info("Added Dropout Layer")
    logger.info("Created model with following config:\n%s" % json.dumps(model.get_config(), indent=4))
    logger.info("Compiling model with optimizer %s" % optimizer)
    start_time = time.time()
    model.compile(loss='categorical_crossentropy', optimizer=optimizer)
    total_time = time.time() - start_time
    logger.info("Model compiled in %.4f seconds." % total_time)
    return model 
Example 56
Project: DeepSequenceClassification   Author: napsternxg   File: model.py    GNU General Public License v2.0 5 votes vote down vote up
def gen_model_brnn(vocab_size=100, embedding_size=128, maxlen=100, output_size=6, hidden_layer_size=100, num_hidden_layers = 1, RNN_LAYER_TYPE="LSTM"):
    RNN_CLASS = LSTM
    if RNN_LAYER_TYPE == "GRU":
        RNN_CLASS = GRU
    logger.info("Parameters: vocab_size = %s, embedding_size = %s, maxlen = %s, output_size = %s, hidden_layer_size = %s, " %\
            (vocab_size, embedding_size, maxlen, output_size, hidden_layer_size))
    logger.info("Building Graph model for Bidirectional RNN")
    model = Graph()
    model.add_input(name='input', input_shape=(maxlen,), dtype=int)
    logger.info("Added Input node")
    logger.info("Init Model with vocab_size = %s, embedding_size = %s, maxlen = %s" % (vocab_size, embedding_size, maxlen))
    model.add_node(Embedding(vocab_size, embedding_size, input_length=maxlen), name='embedding', input='input')
    logger.info("Added Embedding node")
    model.add_node(Dropout(0.5), name="dropout_0", input="embedding")
    logger.info("Added Dropout Node")
    for i in xrange(num_hidden_layers):
        last_dropout_name = "dropout_%s" % i
        forward_name, backward_name, dropout_name = ["%s_%s" % (k, i + 1) for k in ["forward", "backward", "dropout"]]
        model.add_node(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True), name=forward_name, input=last_dropout_name)
        logger.info("Added %s forward node[%s]" % (RNN_LAYER_TYPE, i+1))
        model.add_node(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True, go_backwards=True), name=backward_name, input=last_dropout_name)
        logger.info("Added %s backward node[%s]" % (RNN_LAYER_TYPE, i+1))
        model.add_node(Dropout(0.5), name=dropout_name, inputs=[forward_name, backward_name])
        logger.info("Added Dropout node[%s]" % (i+1))
    model.add_node(TimeDistributedDense(output_size, activation="softmax"), name="tdd", input=dropout_name)
    logger.info("Added TimeDistributedDense node")
    model.add_output(name="output", input="tdd")
    logger.info("Added Output node")
    logger.info("Created model with following config:\n%s" % model.get_config())
    logger.info("Compiling model with optimizer %s" % optimizer)
    start_time = time.time()
    model.compile(optimizer, {"output": 'categorical_crossentropy'})
    total_time = time.time() - start_time
    logger.info("Model compiled in %.4f seconds." % total_time)
    return model 
Example 57
Project: DeepSequenceClassification   Author: napsternxg   File: model.py    GNU General Public License v2.0 5 votes vote down vote up
def gen_model_brnn_multitask(vocab_size=100, embedding_size=128, maxlen=100, output_size=[6, 96], hidden_layer_size=100, num_hidden_layers = 1, RNN_LAYER_TYPE="LSTM"):
    RNN_CLASS = LSTM
    if RNN_LAYER_TYPE == "GRU":
        RNN_CLASS = GRU
    logger.info("Parameters: vocab_size = %s, embedding_size = %s, maxlen = %s, output_size = %s, hidden_layer_size = %s, " %\
            (vocab_size, embedding_size, maxlen, output_size, hidden_layer_size))
    logger.info("Building Graph model for Bidirectional RNN")
    model = Graph()
    model.add_input(name='input', input_shape=(maxlen,), dtype=int)
    logger.info("Added Input node")
    logger.info("Init Model with vocab_size = %s, embedding_size = %s, maxlen = %s" % (vocab_size, embedding_size, maxlen))
    model.add_node(Embedding(vocab_size, embedding_size, input_length=maxlen, mask_zero=True), name='embedding', input='input')
    logger.info("Added Embedding node")
    model.add_node(Dropout(0.5), name="dropout_0", input="embedding")
    logger.info("Added Dropout Node")
    for i in xrange(num_hidden_layers):
        last_dropout_name = "dropout_%s" % i
        forward_name, backward_name, dropout_name = ["%s_%s" % (k, i + 1) for k in ["forward", "backward", "dropout"]]
        model.add_node(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True), name=forward_name, input=last_dropout_name)
        logger.info("Added %s forward node[%s]" % (RNN_LAYER_TYPE, i+1))
        model.add_node(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True, go_backwards=True), name=backward_name, input=last_dropout_name)
        logger.info("Added %s backward node[%s]" % (RNN_LAYER_TYPE, i+1))
        model.add_node(Dropout(0.5), name=dropout_name, inputs=[forward_name, backward_name])
        logger.info("Added Dropout node[%s]" % (i+1))
    output_names = []
    for i, output_task_size in enumerate(output_size):
        tdd_name, output_name = "tdd_%s" % i, "output_%s" % i
        model.add_node(TimeDistributedDense(output_task_size, activation="softmax"), name=tdd_name, input=dropout_name)
        logger.info("Added TimeDistributedDense node %s with output_size %s" % (i, output_task_size))
        model.add_output(name=output_name, input=tdd_name)
        output_names.append(output_name)
    logger.info("Added Output node")
    logger.info("Created model with following config:\n%s" % model.get_config())
    logger.info("Compiling model with optimizer %s" % optimizer)
    start_time = time.time()
    model.compile(optimizer, {k: 'categorical_crossentropy' for k in output_names})
    total_time = time.time() - start_time
    logger.info("Model compiled in %.4f seconds." % total_time)
    return model, output_names 
Example 58
Project: time-series-forecasting-rnn-tensorflow   Author: jiegzhan   File: build_model.py    Apache License 2.0 5 votes vote down vote up
def rnn_lstm(layers, params):
	"""Build RNN (LSTM) model on top of Keras and Tensorflow"""

	model = Sequential()
	model.add(LSTM(input_shape=(layers[1], layers[0]), output_dim=layers[1], return_sequences=True))
	model.add(Dropout(params['dropout_keep_prob']))
	model.add(LSTM(layers[2], return_sequences=False))
	model.add(Dropout(params['dropout_keep_prob']))
	model.add(Dense(output_dim=layers[3]))
	model.add(Activation("tanh"))

	model.compile(loss="mean_squared_error", optimizer="rmsprop")
	return model 
Example 59
Project: stock-price-predict   Author: kaka-lin   File: lstm_cnn.py    MIT License 5 votes vote down vote up
def base_model(feature_len=1, after_day=1, input_shape=(20, 1)):
    model = Sequential()

    model.add(Conv1D(10, kernel_size=5, input_shape=input_shape,  activation='relu', padding='valid', strides=1))
    model.add(LSTM(100, return_sequences=False, input_shape=input_shape))
    model.add(Dropout(0.25))

    # one to many
    model.add(RepeatVector(after_day))
    model.add(LSTM(200, return_sequences=True))
    model.add(Dropout(0.25))
    model.add(TimeDistributed(Dense(100, activation='relu', kernel_initializer='uniform')))
    model.add(TimeDistributed(Dense(feature_len, activation='linear', kernel_initializer='uniform')))

    return model 
Example 60
Project: stock-price-predict   Author: kaka-lin   File: lstm_mtm.py    MIT License 5 votes vote down vote up
def base_model(feature_len=1, after_day=1, input_shape=(20, 1)):
    model = Sequential()

    model.add(LSTM(units=100, return_sequences=False, input_shape=input_shape))
    #model.add(LSTM(units=100, return_sequences=False, input_shape=input_shape))

    # one to many
    model.add(RepeatVector(after_day))
    model.add(LSTM(200, return_sequences=True))
    #model.add(LSTM(50, return_sequences=True))

    model.add(TimeDistributed(Dense(units=feature_len, activation='linear')))

    return model 
Example 61
Project: shell-complete   Author: src-d   File: test_model2predict.py    Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.log = logging.getLogger("test")
        self.log.setLevel(logging.INFO)
        self.path_to_vocab = "shcomplete/tests/data/vocab_0.01.txt"
        self.vc = Vocabulary(self.path_to_vocab)
        self.path_to_corpus = "shcomplete/tests/data/corpus.txt"
        self.models_directory = "shcomplete/tests/data"
        self.args = argparse.Namespace(vocabulary=self.path_to_vocab, corpus=self.path_to_corpus,
                                       models_directory=self.models_directory, seq_len=10,
                                       input_layers=1, hidden_layers=2, output_layers=1,
                                       dropout=0.2, batch_size=32, nb_epochs=2, steps_per_epoch=64,
                                       from_model=None, checkpoint=2, optimizer="adam",
                                       cell_type=recurrent.LSTM)
        self.model = generate_model(self.args, nb_features=self.vc.size,
                                    input_length=self.args.seq_len) 
Example 62
Project: ntm_keras   Author: flomlo   File: ntm.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def build(self, input_shape):
        bs, input_length, input_dim = input_shape

        self.controller_input_dim, self.controller_output_dim = controller_input_output_shape(
                input_dim, self.units, self.m_depth, self.n_slots, self.shift_range, self.read_heads,
                self.write_heads)
            
        # Now that we've calculated the shape of the controller, we have add it to the layer/model.
        if self.controller is None:
            self.controller = Dense(
                name = "controller",
                activation = 'linear',
                bias_initializer = 'zeros',
                units = self.controller_output_dim,
                input_shape = (bs, input_length, self.controller_input_dim))
            self.controller.build(input_shape=(self.batch_size, input_length, self.controller_input_dim))
            self.controller_with_state = False


        # This is a fixed shift matrix
        self.C = _circulant(self.n_slots, self.shift_range)

        self.trainable_weights = self.controller.trainable_weights 

        # We need to declare the number of states we want to carry around.
        # In our case the dimension seems to be 6 (LSTM) or 5 (GRU) or 4 (FF),
        # see self.get_initial_states, those respond to:
        # [old_ntm_output] + [init_M, init_wr, init_ww] +  [init_h] (LSMT and GRU) + [(init_c] (LSTM only))
        # old_ntm_output does not make sense in our world, but is required by the definition of the step function we
        # intend to use.
        # WARNING: What self.state_spec does is only poorly understood,
        # I only copied it from keras/recurrent.py.
        self.states = [None, None, None, None]
        self.state_spec = [InputSpec(shape=(None, self.output_dim)),                            # old_ntm_output
                            InputSpec(shape=(None, self.n_slots, self.m_depth)),                # Memory
                            InputSpec(shape=(None, self.read_heads, self.n_slots)),   # weights_read
                            InputSpec(shape=(None, self.write_heads, self.n_slots))]  # weights_write

        super(NeuralTuringMachine, self).build(input_shape) 
Example 63
Project: allen-ai-science-qa   Author: arranger1044   File: utils.py    GNU General Public License v3.0 5 votes vote down vote up
def get_recurrent_layer(model_name, input_size, output_size, return_sequences=False):
    layer = None
    if model_name == 'rnn':
        layer = SimpleRNN(input_dim=input_size, output_dim=output_size, return_sequences=return_sequences)
    elif model_name == 'lstm':
        layer = LSTM(input_dim=input_size, output_dim=output_size, return_sequences=return_sequences)
    elif model_name == 'gru':
        layer = GRU(input_dim=input_size, output_dim=output_size, return_sequences=return_sequences)
    if layer is None:
        raise ValueError('Unknown recurrent layer: %s' % model_name)
    return layer 
Example 64
Project: stocks-lstm-keras   Author: soarbear   File: stocks-lstm-keras.py    GNU General Public License v3.0 5 votes vote down vote up
def create_model(self) :
    Model = Sequential()
    Model.add(LSTM(self.hidden_neurons, batch_input_shape=(None, self.length_of_sequences, self.in_out_neurons), return_sequences=False))
    Model.add(Dense(self.in_out_neurons))
    Model.add(Activation("linear"))
    Model.compile(loss="mape", optimizer="adam")
    return Model
 
  # do learning 
Example 65
Project: DRNN-Keras   Author: jmsalash   File: drnn.py    MIT License 5 votes vote down vote up
def create_lstm_network1(num_frequency_dimensions, num_hidden_dimensions, \
                         num_recurrent_units=3, activation_function='relu', fl2=0.0, bn=False, hps=False, lrate=0.001):
    print('Creating DRNN for experiment 1')
    model = Sequential()
	#This layer converts frequency space to hidden space
    model.add(TimeDistributed(Dense(num_hidden_dimensions),input_shape=num_frequency_dimensions))
    model.add(TimeDistributed(Dense(num_hidden_dimensions)))
    model.add(TimeDistributed(Dense(num_hidden_dimensions)))
    

    for cur_unit in range(num_recurrent_units):
        print('Creating LSTM with %s neurons' %(num_hidden_dimensions))
        model.add(LSTM(num_hidden_dimensions, return_sequences=True, recurrent_regularizer=l2(fl2)))
#        model.add(LSTM(num_hidden_dimensions, return_sequences=True))
        if bn:
            model.add(BatchNormalization())
#        model.add(LSTM(num_hidden_dimensions, return_sequences=True))

	#This layer converts hidden space back to frequency space
#    model.add(TimeDistributed(Dense(num_frequency_dimensions[1],activation=activation_function)))
    if hps:
        myoutput = int(num_frequency_dimensions[1]/2)
    else:
        myoutput = num_frequency_dimensions[1]
    print('Samples per step: ', myoutput)
    model.add(TimeDistributed(Dense(myoutput,activation=activation_function)))
    rms = RMSprop(lr=lrate)
    model.compile(loss='mean_squared_error', optimizer=rms)

    return model

###############################################################################
# EXPERIMENT 2: 
# - 1 time distributed layer (no activation) 
# - n LSTM layers
# - 2 time distributed layer (no activation)
# - 1 time distributed layer with activation
# - RMSProp optimiser with default values (gamma=0.9, default learning rate=0.001)
# Optional: batch normalisation between LSTM layers, L2 recurrent regularisation
############################################################################### 
Example 66
Project: DRNN-Keras   Author: jmsalash   File: drnn.py    MIT License 5 votes vote down vote up
def create_lstm_network2(num_frequency_dimensions, num_hidden_dimensions, \
                         num_recurrent_units=1, activation_function='relu', fl2=0.0, bn=False, hps=False, lrate=0.001):
    print('Creating DRNN for experiment 2')
    model = Sequential()
	#This layer converts frequency space to hidden space
    print('Initialising dense time distributed layer with dimensions ', num_frequency_dimensions)
    model.add(TimeDistributed(Dense(num_hidden_dimensions),input_shape=num_frequency_dimensions))
    

    for cur_unit in range(num_recurrent_units):
        print('Creating LSTM with %s neurons' %(num_hidden_dimensions))
        model.add(LSTM(num_hidden_dimensions, return_sequences=True, recurrent_regularizer=l2(fl2)))
        if bn:
            model.add(BatchNormalization())
        
    model.add(TimeDistributed(Dense(num_hidden_dimensions)))
    model.add(TimeDistributed(Dense(num_hidden_dimensions)))
	#This layer converts hidden space back to frequency space
    model.add(TimeDistributed(Dense(num_frequency_dimensions[1],activation=activation_function)))
    rms = RMSprop(lr=lrate)
    model.compile(loss='mean_squared_error', optimizer=rms)

    return model

###############################################################################
# EXPERIMENT 3: 
# - n LSTM layers
# - 3 time distributed layer (no activation)
# - 1 time distributed layer with activation
# - RMSProp optimiser with default values (gamma=0.9, default learning rate=0.001)
# Optional: batch normalisation between LSTM layers, L2 recurrent regularisation
############################################################################### 
Example 67
Project: BOP2017   Author: crh19970307   File: train.py    MIT License 4 votes vote down vote up
def train():
	data=loadfromjson()
	quelist,answerlist,datalist=getdata2()
	wordlist=spiltword(datalist)
	taglist=[]
	for index,item in enumerate(data['datalist']):
		if item[0]=='0':
			taglist.append(0)
		else:
			if item[0]=='1':
				taglist.append(1)
			else:
				print('EiRROR\n')
				print(index)
				taglist.append(0)
		#print(len(data['vectorlist'][index]))
	#xa=np.array(data['vectorlist'])
	w=[]
	for items in data['wordlist']:
		w.extend(items)
	for items in wordlist:
		w.extend(items)
	dict = pd.DataFrame(pd.Series(w).value_counts())
	dict['id']=list(range(1,len(dict)+1))  
	get_sent = lambda x: list(dict['id'][x])  
	xa=[]
	for items in data['wordlist']:
		xa.append(get_sent(items))
	#get_sent(data['wordlist'])
	tmp=list(sequence.pad_sequences(xa, maxlen=20,padding='post',truncating='post'))
	xa=np.array(tmp)
	#xa=list(sequence.pad_sequences(xa, maxlen=20)) 
	ya=np.array(taglist)
	#print(np.size(xa))
	#print(np.size(ya))
	print('Build model...')  
	model = Sequential()  
	model.add(Embedding(len(dict)+1,256))  
	#model.add(LSTM(128)) # try using a GRU instead, for fun  
	#model.add(LSTM(32,input_shape=(10,60)))
	#model.add(LSTM(32,input_length=50,input_dim=60))
	model.add(LSTM(128))
	print('LSTM added')
	model.add(Dropout(0.5))  	
	model.add(Dense( 1))  
	model.add(Activation('sigmoid'))  
	model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")  
	model.fit(xa, ya, batch_size=128, nb_epoch=20) #训练时间为若干个小时  
	model.save('my_model.h5') 
Example 68
Project: BOP2017   Author: crh19970307   File: train_mix.py    MIT License 4 votes vote down vote up
def train():

	import os

	gpu_id = '0'

	os.environ['CUDA_VISIBLE_DEVICES']=str(gpu_id)

	os.system('echo $CUDA_VISIBLE_DEVICES')

	data=loadfromjson()

	taglist=[]

	for index,item in enumerate(data['datalist']):

		if item[0]=='0':

			taglist.append(0)

		else:

			if item[0]=='1':

				taglist.append(1)

			else:

				print('EiRROR\n')

				print(index)

				taglist.append(0)

		#print(len(data['vectorlist'][index]))

	#xa=np.array(data['vectorlist'])

	xa=np.zeros((len(data['vectorlist']),30,60),dtype='float64')

	for index1,items in enumerate(data['vectorlist']):

		for index2,item2 in enumerate(items):

			if index2==30:

				break

			xa[index1][index2]=item2



	#xa=np.random.rand(len(data['vectorlist']),50,60)

	ya=np.array(taglist)

	#print(np.size(xa))

	#print(np.size(ya))

	print('Build model...')  

	model = Sequential()  

	#model.add(Embedding(60,32))  

	#model.add(LSTM(128)) # try using a GRU instead, for fun  

	#model.add(LSTM(32,input_shape=(10,60)))

	#model.add(LSTM(32,input_length=50,input_dim=60))

	model.add(LSTM(128,input_length=30,input_dim=60))

	print('LSTM added')

	model.add(Dropout(0.5))  	

	model.add(Dense( 1))  

	model.add(Activation('sigmoid'))  

	model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")  

	model.fit(xa, ya, batch_size=128, nb_epoch=5) #训练时间为若干个小时  

	model.save('sentencelen30_230000_lstm128_epoch5_model.h5') 
Example 69
Project: BOP2017   Author: crh19970307   File: train.py    MIT License 4 votes vote down vote up
def train():
	import os
	gpu_id = '0'
	os.environ['CUDA_VISIBLE_DEVICES']=str(gpu_id)
	os.system('echo $CUDA_VISIBLE_DEVICES')
	data=loadfromjson()
	taglist=[]
	for index,item in enumerate(data['datalist']):
		if item[0]=='0':
			taglist.append(0)
		else:
			if item[0]=='1':
				taglist.append(1)
			else:
				print('EiRROR\n')
				print(index)
				taglist.append(0)
		#print(len(data['vectorlist'][index]))
	#xa=np.array(data['vectorlist'])
	xa=np.zeros((len(data['vectorlist1']),45,60),dtype='float64')
	for index1,items in enumerate(data['vectorlist1']):
		for index2,item2 in enumerate(items):
			if index2==15:
				break
			xa[index1][index2]=item2
	for index1,items in enumerate(data['vectorlist2']):
		for index2,item2 in enumerate(items):
			if index2==30:
				break
			xa[index1][index2+15]=item2		

	#xa=np.random.rand(len(data['vectorlist']),50,60)
	ya=np.array(taglist)
	#print(np.size(xa))
	#print(np.size(ya))
	print('Build model...')  
	model = Sequential()  
	#model.add(Embedding(60,32))  
	#model.add(LSTM(128)) # try using a GRU instead, for fun  
	#model.add(LSTM(32,input_shape=(10,60)))
	#model.add(LSTM(32,input_length=50,input_dim=60))
	model.add(LSTM(128,input_length=45,input_dim=60))
	print('LSTM added')
	model.add(Dropout(0.5))  	
	model.add(Dense( 1))  
	model.add(Activation('sigmoid'))  
	model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")  
	model.fit(xa, ya, batch_size=128, nb_epoch=100) #训练时间为若干个小时  
	model.save('sentencelen15+30_230000_lstm128_epoch100_model.h5') 
Example 70
Project: PIEPredict   Author: aras62   File: pie_intent.py    Apache License 2.0 4 votes vote down vote up
def pie_convlstm_encdec(self):
        '''
        Create an LSTM Encoder-Decoder model for intention estimation
        '''
        #Generate input data. the shapes is (sequence_lenght,length of flattened features)
        encoder_input=input_data=Input(shape=(self._sequence_length,) + self.context_model.output_shape[1:],
                                       name = "encoder_input")
        interm_input = encoder_input

        # Generate Encoder LSTM Unit
        encoder_model = ConvLSTM2D(filters=self._convlstm_num_filters,
                                   kernel_size=self._convlstm_kernel_size,
                                   kernel_regularizer=self._kernel_regularizer,
                                   recurrent_regularizer=self._recurrent_regularizer,
                                   bias_regularizer=self._bias_regularizer,
                                   dropout=self._lstm_dropout,
                                   recurrent_dropout=self._lstm_recurrent_dropout,
                                   return_sequences=False)(interm_input)
        encoder_output = Flatten(name='encoder_flatten')(encoder_model)

        # Generate Decoder LSTM unit
        decoder_input = Input(shape=(self._decoder_seq_length,
                                     self._decoder_input_size),
                              name='decoder_input')
        encoder_vec = RepeatVector(self._decoder_seq_length)(encoder_output)
        decoder_concat_inputs = Concatenate(axis=2)([encoder_vec, decoder_input])

        decoder_model = self.create_lstm_model(name='decoder_network',
                                               r_state = False,
                                               r_sequence=False)(decoder_concat_inputs)

        decoder_dense_output = Dense(self._decoder_dense_output_size,
                                     activation='sigmoid',
                                     name='decoder_dense')(decoder_model)

        decoder_output = decoder_dense_output

        self.train_model = Model(inputs=[encoder_input, decoder_input],
                                 outputs=decoder_output)

        self.train_model.summary()
        return self.train_model 
Example 71
Project: PIEPredict   Author: aras62   File: pie_predict.py    Apache License 2.0 4 votes vote down vote up
def pie_encdec(self):
        """
        Generates the encoder decoder method
        :return: An instance of the network model
        """

        # Generate input data. the shapes is (sequence_lenght,length of flattened features)
        _encoder_input = Input(shape=(self._observe_length, self._encoder_feature_size),
                               name='encoder_input')

        # Temporal attention module
        _attention_net = self.attention_temporal(_encoder_input, self._observe_length)

        # Generate Encoder LSTM Unit
        encoder_model = self.create_lstm_model(name='encoder_network')
        _encoder_outputs_states = encoder_model(_attention_net)
        _encoder_states = _encoder_outputs_states[1:]

        # Generate Decoder LSTM unit
        decoder_model = self.create_lstm_model(name='decoder_network', r_state=False)
        _hidden_input = RepeatVector(self._predict_length)(_encoder_states[0])
        _decoder_input = Input(shape=(self._predict_length, self._decoder_feature_size),
                               name='pred_decoder_input')

        # Embedding unit on the output of Encoder
        _embedded_hidden_input = Dense(self._embed_size, activation='relu')(_hidden_input)
        _embedded_hidden_input = Dropout(self._embed_dropout,
                                         name='dropout_dec_input')(_embedded_hidden_input)

        decoder_concat_inputs = Concatenate(axis=2)([_embedded_hidden_input, _decoder_input])

        # Self attention unit
        att_input_dim = self._embed_size + self._decoder_feature_size
        decoder_concat_inputs = self.attention_element(decoder_concat_inputs, att_input_dim)

        # Initialize the decoder with encoder states
        decoder_output = decoder_model(decoder_concat_inputs,
                                       initial_state=_encoder_states)
        decoder_output = Dense(self._prediction_size,
                               activation='linear',
                               name='decoder_dense')(decoder_output)

        net_model = Model(inputs=[_encoder_input, _decoder_input],
                          outputs=decoder_output)
        net_model.summary()

        return net_model 
Example 72
Project: stock-price-prediction   Author: chinuy   File: nn.py    MIT License 4 votes vote down vote up
def main():

    stock_name = 'SPY'
    delta = 4
    start = datetime.datetime(2010,1,1)
    end = datetime.datetime(2015,12,31)
    start_test = datetime.datetime(2015,1,1)

    dataset = util.get_data(stock_name, start, end)
    delta = range(1, delta)
    dataset = util.applyFeatures(dataset, delta)
    dataset = util.preprocessData(dataset)
    X_train, y_train, X_test, y_test  = \
        classifier.prepareDataForClassification(dataset, start_test)

    X_train = numpy.reshape(numpy.array(X_train), (X_train.shape[0], 1, X_train.shape[1]))

    X_test = numpy.reshape(numpy.array(X_test), (X_test.shape[0], 1, X_test.shape[1]))

    #Step 2 Build Model
    model = Sequential()

    model.add(LSTM(
        128,
        input_shape=(None, X_train.shape[2]),
        return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(
        240,
        return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(
        units=1))
    model.add(Activation('sigmoid'))

    start = time.time()
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

    #Step 3 Train the model
    model.fit(
        X_train,
        y_train,
        batch_size=4,
        epochs=4,
        validation_split=0.1)

    print model.predict(X_train)
    print model.evaluate(X_train, y_train) 
Example 73
Project: StockRecommendSystem   Author: doncat99   File: Stock_Prediction_Model_Stateless_LSTM.py    MIT License 4 votes vote down vote up
def lstm_model(self):
        model = Sequential()
        first = True
        for idx in range(len(self.paras.model['hidden_layers'])):
            if idx == (len(self.paras.model['hidden_layers']) - 1):
                model.add(LSTM(int(self.paras.model['hidden_layers'][idx]), return_sequences=False))
                model.add(Activation(self.paras.model['activation']))
                model.add(Dropout(self.paras.model['dropout']))
            elif first == True:
                model.add(LSTM(input_shape=(None, int(self.paras.n_features)),
                               units=int(self.paras.model['hidden_layers'][idx]),
                               return_sequences=True))
                model.add(Activation(self.paras.model['activation']))
                model.add(Dropout(self.paras.model['dropout']))
                first = False
            else:
                model.add(LSTM(int(self.paras.model['hidden_layers'][idx]), return_sequences=True))
                model.add(Activation(self.paras.model['activation']))
                model.add(Dropout(self.paras.model['dropout']))

        if self.paras.model['optimizer'] == 'sgd':
            #optimizer = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
            optimizer = optimizers.SGD(lr=self.paras.model['learning_rate'], decay=1e-6, momentum=0.9, nesterov=True)
        elif self.paras.model['optimizer'] == 'rmsprop':
            #optimizer = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
            optimizer = optimizers.RMSprop(lr=self.paras.model['learning_rate']/10, rho=0.9, epsilon=1e-08, decay=0.0)
        elif self.paras.model['optimizer'] == 'adagrad':
            #optimizer = optimizers.Adagrad(lr=0.01, epsilon=1e-08, decay=0.0)
            optimizer = optimizers.Adagrad(lr=self.paras.model['learning_rate'], epsilon=1e-08, decay=0.0)
        elif self.paras.model['optimizer'] == 'adam':
            #optimizer = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
            optimizer = optimizers.Adam(lr=self.paras.model['learning_rate']/10, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        elif self.paras.model['optimizer'] == 'adadelta':
            optimizer = optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=1e-08, decay=0.0)
        elif self.paras.model['optimizer'] == 'adamax':
            optimizer = optimizers.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        elif self.paras.model['optimizer'] == 'nadam':
            optimizer = optimizers.Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004)
        else:
            optimizer = optimizers.Adam(lr=self.paras.model['learning_rate']/10, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

        # output layer
        model.add(Dense(units=self.paras.model['out_layer']))
        model.add(Activation(self.paras.model['out_activation']))
        model.compile(loss=self.paras.model['loss'], optimizer=optimizer, metrics=['accuracy'])

        return model 
Example 74
Project: dlopt   Author: acamero   File: nn.py    GNU General Public License v3.0 4 votes vote down vote up
def build_model(layers,
                    cell=LSTM,
                    weights=None,
                    dense_activation='tanh',
                    embedding=None,
                    verbose=0,
                    **kwargs):
        """ Architecture from layers array:
        lstm layers <- len(layers) - 2
        input dim <- layers[0]
        output dim <- layers[-1]
        """
        K.clear_session()
        tf.keras.backend.clear_session()
        gc.collect()
        if verbose > 1:
            print('Session cleared (RNNBuilder class)')
        if embedding and len(layers) <= 3:
            raise Exception("Provide more than 3 layers when using embedding")
        model = Sequential()
        for i in range(len(layers) - 2):
            if (embedding
               and i == 0
               and len(layers) > 3):
                model.add(Embedding(input_dim=layers[i],
                                    output_dim=layers[i+1],
                                    embeddings_initializer='zeros'))
            else:
                model.add(cell(
                    # Keras API 2
                    input_shape=(None, layers[i]),
                    units=layers[i+1],
                    # Keras API 1
                    # input_dim=layers[i],
                    # output_dim=layers[i+1],
                    kernel_initializer='zeros',
                    recurrent_initializer='zeros',
                    bias_initializer='zeros',
                    # Uncomment to use last batch state to init
                    # next training step.
                    # Specify shuffle=False when calling fit()
                    # batch_size=batch_size, stateful=True,
                    # return_sequences=True if i < (len(layers) - 3)
                    #                       else False))
                    return_sequences=True if i < (len(layers) - 3) else False))
        model.add(Dense(layers[-1],
                  activation=dense_activation,
                  kernel_initializer='zeros',
                  bias_initializer='zeros'))
        if weights:
            model.set_weights(weights)
        if verbose > 1:
            model.summary()
        return model 
Example 75
Project: recipe-summarization   Author: rtlee9   File: model.py    MIT License 4 votes vote down vote up
def create_model(vocab_size, embedding_size, LR, rnn_layers, rnn_size, embedding=None):
    """Construct and compile LSTM model."""
    # create a standard stacked LSTM
    if embedding is not None:
        embedding = [embedding]
    model = Sequential()
    model.add(Embedding(vocab_size, embedding_size,
                        input_length=maxlen,
                        W_regularizer=regularizer, dropout=p_emb, weights=embedding, mask_zero=True,
                        name='embedding_1'))
    for i in range(rnn_layers):
        lstm = LSTM(rnn_size, return_sequences=True,
                    W_regularizer=regularizer, U_regularizer=regularizer,
                    b_regularizer=regularizer, dropout_W=p_W, dropout_U=p_U,
                    name='lstm_{}'.format(i + 1))
        model.add(lstm)
        model.add(Dropout(p_dense, name='dropout_{}'.format(i + 1)))

    def simple_context(X, mask, n=activation_rnn_size):
        """Reduce the input just to its headline part (second half).

        For each word in this part it concatenate the output of the previous layer (RNN)
        with a weighted average of the outputs of the description part.
        In this only the last `rnn_size - activation_rnn_size` are used from each output.
        The first `activation_rnn_size` output is used to computer the weights for the averaging.
        """
        desc, head = X[:, :maxlend, :], X[:, maxlend:, :]
        head_activations, head_words = head[:, :, :n], head[:, :, n:]
        desc_activations, desc_words = desc[:, :, :n], desc[:, :, n:]

        # RTFM http://deeplearning.net/software/theano/library/tensor/basic.html#theano.tensor.batched_tensordot
        # activation for every head word and every desc word
        activation_energies = K.batch_dot(head_activations, desc_activations, axes=(2, 2))
        # make sure we dont use description words that are masked out
        activation_energies = activation_energies + -1e20 * K.expand_dims(
            1. - K.cast(mask[:, :maxlend], 'float32'), 1)

        # for every head word compute weights for every desc word
        activation_energies = K.reshape(activation_energies, (-1, maxlend))
        activation_weights = K.softmax(activation_energies)
        activation_weights = K.reshape(activation_weights, (-1, maxlenh, maxlend))

        # for every head word compute weighted average of desc words
        desc_avg_word = K.batch_dot(activation_weights, desc_words, axes=(2, 1))
        return K.concatenate((desc_avg_word, head_words))

    if activation_rnn_size:
        model.add(SimpleContext(simple_context, rnn_size, name='simplecontext_1'))

    model.add(TimeDistributed(Dense(
        vocab_size,
        W_regularizer=regularizer,
        b_regularizer=regularizer,
        name='timedistributed_1')))
    model.add(Activation('softmax', name='activation_1'))

    # opt = Adam(lr=LR)  # keep calm and reduce learning rate
    model.compile(loss='categorical_crossentropy', optimizer=optimizer)

    K.set_value(model.optimizer.lr, np.float32(LR))
    return model 
Example 76
Project: autonomio   Author: autonomio   File: lstm.py    MIT License 4 votes vote down vote up
def lstm(data,param):

    if param['prediction_len'] is 'auto':
        param['prediction_len'] = param['seq_len']

    X_train, y_train, X_test, y_test = _lstm_load_data(data,
                                                       param['seq_len'],
                                                       param['normalize_window'])

    dimensions = [1, param['seq_len'], param['dense_neurons'], 1]

    model = Sequential()

    model.add(LSTM(
        input_shape=(dimensions[1], dimensions[0]),
        output_dim=dimensions[1],
        return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(
        dimensions[2],
        return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(
        output_dim=dimensions[3]))
    model.add(Activation("linear"))

    model.compile(loss="mse", optimizer="rmsprop")

    history = model.fit(X_train,
                        y_train,
                        batch_size=param['batch_size'],
                        nb_epoch=param['epoch'],
                        validation_split=0.05,
                        verbose=param['verbose'])

    predicted = model.predict(X_test)
    predicted = np.reshape(predicted, (predicted.size,))

    temp = pd.DataFrame({'predicted': predicted, 'actual': y_test})

    out = pd.Series({'pred_std': temp.predicted.std(),
                     'actual_std': temp.actual.std(),
                     'diff_std': round(temp.actual.std() / temp.predicted.std(), 3) -1,
                     'pred_max': temp.predicted.max(),
                     'actual_max': temp.actual.max(),
                     'diff_max': round(temp.actual.max() / temp.predicted.max(), 3) -1,
                     'pred_min': temp.predicted.min(),
                     'actual_min': temp.actual.min(),
                     'diff_min': round(temp.actual.min() / temp.predicted.min(), 3) -1})

    #print("Median prediction error: %.2f%%" % ((temp.predicted / temp.actual * 100).median() - 100))

    display(pd.DataFrame(out).transpose())

    lstm_plot(predicted, y_test)
    histplot(predicted, y_test, 50) 
Example 77
Project: applications   Author: geomstats   File: recurrent_test.py    MIT License 4 votes vote down vote up
def rnn_test(f):
    """
    All the recurrent layers share the same interface,
    so we can run through them with a single function.
    """
    f = keras_test(f)
    return pytest.mark.parametrize('layer_class', [
        recurrent.SimpleRNN,
        recurrent.GRU,
        recurrent.LSTM
    ])(f) 
Example 78
Project: StarTrader   Author: jiewwantan   File: compare.py    MIT License 4 votes vote down vote up
def train_model(model, train_X, train_y, model_type):
        """
        Try to load a pre-built model.
        Otherwise fit a new mode with the training data. Once training is done, save the model.
        """
        es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=PATIENCE)
        if model_type == "LSTM":
            batch_size = 4
            mc = ModelCheckpoint('./model/best_lstm_model.h5', monitor='val_loss', save_weights_only=False,
                                 mode='min', verbose=1, save_best_only=True)
            try:
                model = load_model('./model/best_lstm_model.h5')
                print("\n")
                print("Loading pre-saved model ...")
            except:
                print("\n")
                print("No pre-saved model, training new model.")
                pass
        elif model_type == "CNN":
            batch_size = 8
            mc = ModelCheckpoint('./model/best_cnn_model.h5'.format(symbol), monitor='val_loss', save_weights_only=False,
                                 mode='min', verbose=1, save_best_only=True)
            try:
                model = load_model('./model/best_cnn_model.h5')
                print("\n")
                print("Loading pre-saved model ...")
            except:
                print("\n")
                print("No pre-saved model, training new model.")
                pass
        # fit network
        history = model.fit(
            train_X,
            train_y,
            epochs=500,
            batch_size=batch_size,
            validation_split=0.2,
            verbose=2,
            shuffle=True,
            # callbacks=[es, mc, tb, LearningRateTracker()])
            callbacks=[es, mc])

        if model_type == "LSTM":
            model.save('./model/best_lstm_model.h5')
        elif model_type == "CNN":
            model.save('./model/best_cnn_model.h5')

        return history, model 
Example 79
Project: StarTrader   Author: jiewwantan   File: compare.py    MIT License 4 votes vote down vote up
def build_rnn_model(train_X):
        """
        Build the RNN model architecture.
        """
        # design network
        print("\n")
        print("RNN LSTM model architecture >")
        model = Sequential()
        model.add(LSTM(128, kernel_initializer='random_uniform',
                       bias_initializer='zeros', return_sequences=True,
                       recurrent_dropout=0.2,
                       input_shape=(train_X.shape[1], train_X.shape[2])))
        model.add(Dropout(0.5))
        model.add(LSTM(64, kernel_initializer='random_uniform',
                       return_sequences=True,
                       # bias_regularizer=regularizers.l2(0.01),
                       # kernel_regularizer=regularizers.l1_l2(l1=0.01,l2=0.01),
                       # activity_regularizer=regularizers.l2(0.01),
                       bias_initializer='zeros'))
        model.add(Dropout(0.5))
        model.add(LSTM(64, kernel_initializer='random_uniform',
                       # bias_regularizer=regularizers.l2(0.01),
                       # kernel_regularizer=regularizers.l1_l2(l1=0.01,l2=0.01),
                       # activity_regularizer=regularizers.l2(0.01),
                       bias_initializer='zeros'))
        model.add(Dropout(0.5))
        model.add(Dense(5))
        # optimizer = keras.optimizers.RMSprop(lr=0.25, rho=0.9, epsilon=1e-0)
        # optimizer = keras.optimizers.Adagrad(lr=0.0001, epsilon=1e-08, decay=0.00002)
        # optimizer = keras.optimizers.Adam(lr=0.0001)
        # optimizer = keras.optimizers.Nadam(lr=0.0002, beta_1=0.9, beta_2=0.999, schedule_decay=0.004)
        # optimizer = keras.optimizers.Adamax(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
        optimizer = keras.optimizers.Adadelta(lr=0.2, rho=0.95, epsilon=None, decay=0.00001)

        model.compile(loss='mae', optimizer=optimizer, metrics=['mse', 'mae'])
        model.summary()
        print("\n")
        return model



# ------------------------------ Main Program --------------------------------- 
Example 80
Project: ML_learn   Author: jeffmxh   File: train.py    MIT License 4 votes vote down vote up
def build_model(wordvec_weight, params, logger):
    if wordvec_weight!='':
        word_embedding_layer = Embedding(
            input_dim=wordvec_weight.shape[0],
            output_dim=wordvec_weight.shape[1],
            weights=[wordvec_weight],
            trainable=params.embedding_train)
    else:
        word_embedding_layer = Embedding(params.dict_len+1, 256)
    logger.info('Build model...')
    model = Sequential()
    model.add(word_embedding_layer)
    model.add(Dropout(0.1))
    if params.layer=='lstm':
        model.add(LSTM(128, return_sequences = False))
    elif params.layer=='bilstm':
        model.add(Bidirectional(LSTM(128, return_sequences = False))) 
    elif params.layer=='gru':
        model.add(GRU(128, return_sequences = False))
    elif params.layer=='cnn':
        model.add(Conv1D(256,
                         3,
                         padding='valid',
                         activation='relu',
                         strides=1))
        model.add(GlobalMaxPooling1D())
        # model.add(MaxPooling1D(pool_size = 2))
        # model.add(Flatten())
        model.add(Dropout(0.5))
        model.add(Dense(128))
        model.add(Dropout(0.5))
        model.add(Activation('relu'))
    elif params.layer=='ConvRnn':
        model.add(Conv1D(250,
                         3,
                         padding='valid',
                         activation='relu',
                         strides=1))
        # we use max pooling:
        model.add(MaxPooling1D(pool_size = 2))
        model.add(Dropout(0.3))
        model.add(LSTM(128))
    else: # 采取自定义的模型结构
        model.add(GRU(128, return_sequences = True))
        model.add(Dropout(0.2))
        model.add(GRU(64, return_sequences = False))
    model.add(Dropout(0.5))
    model.add(Dense(params.num_classes))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=[metrics.mae, metrics.categorical_accuracy])
    return model