예제 #1
0
def get_model(maxlen, len_chars):
    model = Sequential()
    model.add(PLSTM(128, input_shape=(maxlen, len_chars)))
    model.add(Dense(len_chars))
    model.add(Activation('softmax'))
    optimizer = RMSprop(lr=0.01)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])
    return model
예제 #2
0
def main():
    batch_size = 128
    nb_classes = 10
    nb_epoch = 2

    # input image dimensions
    img_rows, img_cols = 28, 28

    # the data, shuffled and split between train and test sets
    (X_train, y_train), (X_test, y_test) = mnist.load_data()
    X_train = X_train.reshape(X_train.shape[0], img_rows * img_cols, 1)
    X_test = X_test.reshape(X_test.shape[0], img_rows * img_cols, 1)
    X_train, X_test = X_train.astype('float32'), X_test.astype('float32')
    X_mean, X_std = np.mean(X_train), np.std(X_train)
    X_train, X_test = (X_train - X_mean) / X_std, (X_test - X_mean) / X_std
    print(('X_train shape:', X_train.shape))
    print(('X_test shape:', X_test.shape))

    # convert class vectors to binary class matrices
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)
    print(('Y_train shape:', Y_train.shape))
    print(('Y_test shape:', Y_test.shape))

    # LSTM with timegate
    model_PLSTM = Sequential()
    model_PLSTM.add(PLSTM(32, input_shape=(28 * 28, 1), implementation=2))
    model_PLSTM.add(Dense(10, activation='softmax'))
    model_PLSTM.compile(optimizer='rmsprop',
                        loss='categorical_crossentropy',
                        metrics=['accuracy'])
    model_PLSTM.summary()
    acc_PLSTM = AccHistory()
    loss_PLSTM = LossHistory()
    model_PLSTM.fit(X_train,
                    Y_train,
                    epochs=nb_epoch,
                    batch_size=batch_size,
                    callbacks=[acc_PLSTM, loss_PLSTM])
    score_PLSTM = model_PLSTM.evaluate(X_test, Y_test, verbose=0)

    # Vanilla LSTM
    model_LSTM = Sequential()
    model_LSTM.add(LSTM(32, input_shape=(28 * 28, 1), implementation=2))
    model_LSTM.add(Dense(10, activation='softmax'))
    model_LSTM.compile(optimizer='rmsprop',
                       loss='categorical_crossentropy',
                       metrics=['accuracy'])
    model_LSTM.summary()
    acc_LSTM = AccHistory()
    loss_LSTM = LossHistory()
    model_LSTM.fit(X_train,
                   Y_train,
                   epochs=nb_epoch,
                   batch_size=batch_size,
                   callbacks=[acc_LSTM, loss_LSTM])
    score_LSTM = model_LSTM.evaluate(X_test, Y_test, verbose=0)

    # plot results
    plt.figure(1, figsize=(10, 10))
    plt.title('Accuracy on MNIST training dataset')
    plt.xlabel('Iterations, batch size ' + str(batch_size))
    plt.ylabel('Classification accuracy')
    plt.plot(acc_LSTM.losses, color='k', label='LSTM')
    plt.hold(True)
    plt.plot(acc_PLSTM.losses, color='r', label='PLSTM')
    plt.savefig('mnist_plstm_lstm_comparison_acc.png', dpi=100)

    plt.figure(2, figsize=(10, 10))
    plt.title('Loss on MNIST training dataset')
    plt.xlabel('Iterations, batch size ' + str(batch_size))
    plt.ylabel('Categorical cross-entropy')
    plt.plot(loss_LSTM.losses, color='k', label='LSTM')
    plt.hold(True)
    plt.plot(loss_PLSTM.losses, color='r', label='PLSTM')
    plt.savefig('mnist_plstm_lstm_comparison_loss.png', dpi=100)

    # Compare test performance
    print(('Test score LSTM:', score_LSTM[0]))
    print(('Test score Phased LSTM:', score_PLSTM[0]))
예제 #3
0
print('Pad sequences (samples x time)')
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)

print('Build model...')

# LSTM with timegate
model_PLSTM = Sequential()
model_PLSTM.add(Embedding(max_features, 128, dropout=0.2))
model_PLSTM.add(
    PLSTM(128,
          dropout_W=0.3,
          dropout_U=0.3,
          W_regularizer=l2(0.002),
          consume_less='gpu'))
model_PLSTM.add(Dense(1, W_regularizer=l2(0.002), activation='sigmoid'))
model_PLSTM.compile(loss='binary_crossentropy',
                    optimizer='adamax',
                    metrics=['accuracy'])

print('Train...')
model_PLSTM.fit(X_train,
                y_train,
                nb_epoch=5,
                batch_size=batch_size,
                validation_data=(X_test, y_test))
score_PLSTM, acc = model_PLSTM.evaluate(X_test, y_test, batch_size=batch_size)
        (np.mean(word_id_len) + 2 * np.std(word_id_len))).astype(int)

    #pad sequences
    word_id_train = sequence.pad_sequences(np.array(word_id_train),
                                           maxlen=seq_len)
    word_id_test = sequence.pad_sequences(np.array(word_id_test),
                                          maxlen=seq_len)
    y_train_enc = np_utils.to_categorical(sentiment_train, num_labels)

    #PLSTM
    print "fitting PLSTM ..."
    from phased_lstm_keras.PhasedLSTM import PhasedLSTM as PLSTM

    model_PLSTM = Sequential()
    model_PLSTM.add(Embedding(dictionary_size, 128, dropout=0.2))
    model_PLSTM.add(PLSTM(128, consume_less='gpu'))
    model_PLSTM.add(Dense(5, activation='softmax'))
    model_PLSTM.compile(optimizer='rmsprop',
                        loss='categorical_crossentropy',
                        metrics=['accuracy'])
    model_PLSTM.summary()
    acc_PLSTM = AccHistory()
    loss_PLSTM = LossHistory()
    model_PLSTM.fit(word_id_train,
                    y_train_enc,
                    nb_epoch=300,
                    batch_size=128,
                    callbacks=[acc_PLSTM, loss_PLSTM])

    #LSTM
    print "fitting LSTM ..."
예제 #5
0
def main():
    #SteamAppDB = pd.read_csv("C:/users/unrea/Desktop/AppIDDB.json")
    #print(SteamAppDB.head())
    print("yes")
    reviewDF = createReviewDF()
    print("nooo")
    print(reviewDF.head())
    # input image dimensions
    x = reviewDF.iloc[:, range(2, 26)]
    y = reviewDF.iloc[:, 0]
    z = reviewDF.iloc[:, 1]
    X_train, X_test, y_train, y_test = train_test_split(x,
                                                        y,
                                                        train_size=0.8,
                                                        test_size=0.2,
                                                        random_state=42,
                                                        shuffle=True)

    # the data, shuffled and split between train and test sets
    sc_x = StandardScaler()
    X_train = sc_x.fit_transform(X_train)
    X_test = sc_x.transform(X_test)
    # convert class vectors to binary class matrices
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)
    print('Y_train shape:', Y_train.shape)
    print('Y_test shape:', Y_test.shape)

    # LSTM with timegate
    model_PLSTM = Sequential()
    model_PLSTM.add(PLSTM(32, input_shape=(28 * 28, 1), implementation=2))
    model_PLSTM.add(Dense(10, activation='softmax'))
    model_PLSTM.compile(optimizer='rmsprop',
                        loss='categorical_crossentropy',
                        metrics=['accuracy'])
    model_PLSTM.summary()
    acc_PLSTM = AccHistory()
    loss_PLSTM = LossHistory()
    model_PLSTM.fit(X_train,
                    Y_train,
                    epochs=nb_epoch,
                    batch_size=batch_size,
                    callbacks=[acc_PLSTM, loss_PLSTM])
    score_PLSTM = model_PLSTM.evaluate(X_test, Y_test, verbose=0)

    # Vanilla LSTM
    model_LSTM = Sequential()
    model_LSTM.add(LSTM(32, input_shape=(28 * 28, 1), implementation=2))
    model_LSTM.add(Dense(10, activation='softmax'))
    model_LSTM.compile(optimizer='rmsprop',
                       loss='categorical_crossentropy',
                       metrics=['accuracy'])
    model_LSTM.summary()
    acc_LSTM = AccHistory()
    loss_LSTM = LossHistory()
    model_LSTM.fit(X_train,
                   Y_train,
                   epochs=nb_epoch,
                   batch_size=batch_size,
                   callbacks=[acc_LSTM, loss_LSTM])
    score_LSTM = model_LSTM.evaluate(X_test, Y_test, verbose=0)

    # plot results
    plt.figure(1, figsize=(10, 10))
    plt.title('Accuracy on MNIST training dataset')
    plt.xlabel('Iterations, batch size ' + str(batch_size))
    plt.ylabel('Classification accuracy')
    plt.plot(acc_LSTM.losses, color='k', label='LSTM')
    plt.hold(True)
    plt.plot(acc_PLSTM.losses, color='r', label='PLSTM')
    plt.savefig('mnist_plstm_lstm_comparison_acc.png', dpi=100)

    plt.figure(2, figsize=(10, 10))
    plt.title('Loss on MNIST training dataset')
    plt.xlabel('Iterations, batch size ' + str(batch_size))
    plt.ylabel('Categorical cross-entropy')
    plt.plot(loss_LSTM.losses, color='k', label='LSTM')
    plt.hold(True)
    plt.plot(loss_PLSTM.losses, color='r', label='PLSTM')
    plt.savefig('mnist_plstm_lstm_comparison_loss.png', dpi=100)

    # Compare test performance
    print('Test score LSTM:', score_LSTM[0])
    print('Test score Phased LSTM:', score_PLSTM[0])
예제 #6
0
print(len(X_train_ori), 'train sequences')
print(len(X_test_ori), 'test sequences')

print('Pad sequences (samples x time)')
X_train = sequence.pad_sequences(X_train_ori, maxlen=maxlen)
X_test = sequence.pad_sequences(X_test_ori, maxlen=maxlen)
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)

print('Build model...')


# LSTM with timegate
model_PLSTM = Sequential()
model_PLSTM.add(Embedding(max_features, 128, dropout=0.2))
model_PLSTM.add(PLSTM(64, dropout_W=0.3, dropout_U=0.3, W_regularizer=l2(0.08),consume_less='gpu'))
model_PLSTM.add(Dense(1,W_regularizer=l2(0.08), activation='sigmoid'))
model_PLSTM.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

print('Train...')
model_PLSTM.fit(X_train, y_train, nb_epoch=3, batch_size=batch_size,
                validation_data=(X_test, y_test))
score_PLSTM, acc = model_PLSTM.evaluate(X_test, y_test, batch_size=batch_size)

print('Test score:', score_PLSTM)
print('Test accuracy:', acc)

predictions = model_PLSTM.predict_classes(X_test, batch_size, 1)

max_length = 0
for i in range(0, len(predictions)):
예제 #7
0

# In[ ]:


model_PLSTM.fit(X, Y, epochs=training_epochs, batch_size=batch_size, callbacks=[tbCallBack])
model_PLSTM.save('model_plstm'+str(n_hidden_1)+'.h5')
'''

# In[ ]:


n_hidden_1 = 256
# create model
model_PLSTM = Sequential()
model_PLSTM.add(PLSTM(n_hidden_1, input_shape=input_shape, implementation=2))
model_PLSTM.add(Dense(n_classes, activation='softmax'))
# Compile model
model_PLSTM.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model_PLSTM.summary()
tbCallBack = TensorBoard(log_dir='./Graph/plstm'+str(n_hidden_1)+'_e'+str(training_epochs), histogram_freq=0, write_graph=True, write_images=True)


# In[ ]:


model_PLSTM.fit(X, Y, epochs=training_epochs, batch_size=batch_size, callbacks=[tbCallBack])
model_PLSTM.save('model_plstm'+str(n_hidden_1)+'.h5')

'''
# # LSTM
예제 #8
0
def create_model_plstm(features, win_size):
    if win_size >= 12:
        # LSTM with timegate
        inputs = Input(shape=(win_size, features))
        x = PLSTM(32,
                  activation='relu',
                  implementation=1,
                  return_sequences=True,
                  name='plstm1')(inputs)
        x = Conv1D(64,
                   activation='relu',
                   kernel_size=5,
                   padding='valid',
                   name='conv1')(x)
        x = Conv1D(128,
                   activation='relu',
                   kernel_size=5,
                   padding='valid',
                   name='conv2')(x)
        x = Conv1D(256,
                   activation='relu',
                   kernel_size=3,
                   padding='valid',
                   name='conv3')(x)
    elif win_size >= 6 and win_size < 12:
        inputs = Input(shape=(win_size, features))
        x = PLSTM(32,
                  activation='relu',
                  implementation=1,
                  return_sequences=True,
                  name='plstm1')(inputs)
        x = Conv1D(64,
                   activation='relu',
                   kernel_size=3,
                   padding='valid',
                   name='conv1')(x)
        x = Conv1D(128,
                   activation='relu',
                   kernel_size=3,
                   padding='valid',
                   name='conv2')(x)
        x = Conv1D(256,
                   activation='relu',
                   kernel_size=2,
                   padding='valid',
                   name='conv3')(x)
    else:
        inputs = Input(shape=(win_size, features))
        x = PLSTM(32,
                  activation='relu',
                  implementation=1,
                  return_sequences=True,
                  name='plstm1')(inputs)
        x = Conv1D(64,
                   activation='relu',
                   kernel_size=2,
                   padding='valid',
                   name='conv1')(x)
        x = Conv1D(128,
                   activation='relu',
                   kernel_size=1,
                   padding='valid',
                   name='conv2')(x)
    x = Flatten()(x)
    x = Dense(32, activation='relu', name='dense1')(x)
    out = Dense(2, activation='linear', name='out')(x)
    model_PLSTM = Model(inputs, out)
    radam = RAdam(learning_rate=0.005)
    model_PLSTM.compile(optimizer=radam, loss='mse', metrics=['mae'])
    return model_PLSTM
def construct_rnn_model(look_back,
                        d,
                        n_units=[20, 20],#[500, 500],
                        dense_units=[50, 10],#[1000, 200, 50, 10],
                        filters=64,
                        kernel_size=5,
                        pool_size=4,
                        method='sgru',
                        bias_reg=None,
                        input_reg=None,
                        recurr_reg=None):
    model = Sequential()
    if method == 'lstm':
        model.add(LSTM(n_units[0],
                       input_shape=(look_back, d),
                       return_sequences=True,
                       bias_regularizer=bias_reg,
                       kernel_regularizer=input_reg,
                       recurrent_regularizer=recurr_reg))
        for n_unit in n_units[1:]:
            model.add(LSTM(n_unit,
                           bias_regularizer=bias_reg,
                           kernel_regularizer=input_reg,
                           recurrent_regularizer=recurr_reg))
    elif method == 'gru':
        model.add(GRU(n_units[0],
                      input_shape=(look_back, d),
                      return_sequences=True,
                      bias_regularizer=bias_reg,
                      kernel_regularizer=input_reg,
                      recurrent_regularizer=recurr_reg))
        for n_unit in n_units[1:]:
            model.add(GRU(n_unit,
                          bias_regularizer=bias_reg,
                          kernel_regularizer=input_reg,
                          recurrent_regularizer=recurr_reg))
    elif method == 'sgru':
        model.add(sGRU(n_units[0],
                       input_shape=(look_back, d),
                       return_sequences=True))
        for n_unit in n_units[1:]:
            model.add(sGRU(n_unit))
    elif method == 'plstm':
        model.add(PLSTM(n_units[0],
                        input_shape=(look_back, d),
                        return_sequences=True))
        for n_unit in n_units[1:]:
            model.add(PLSTM(n_unit))
    elif method == 'bi-lstm':
        model.add(Bidirectional(LSTM(n_units[0],
                                input_shape=(look_back, d),
                                return_sequences=True)))
        for n_unit in n_units[1:]:
            model.add(Bidirectional(LSTM(n_unit)))
    elif method == 'bi-gru':
        model.add(Bidirectional(GRU(n_units[0],
                                input_shape=(look_back, d),
                                return_sequences=True)))
        for n_unit in n_units[1:]:
            model.add(Bidirectional(GRU(n_unit)))
    elif method == 'bi-sgru':
        model.add(Bidirectional(sGRU(n_units[0],
                                input_shape=(look_back, d),
                                return_sequences=True)))
        for n_unit in n_units[1:]:
            model.add(Bidirectional(sGRU(n_unit)))
    elif method == 'bi-plstm':
        model.add(Bidirectional(PLSTM(n_units[0],
                                input_shape=(look_back, d),
                                return_sequences=True)))
        for n_unit in n_units[1:]:
            model.add(Bidirectional(PLSTM(n_unit)))
    elif method == 'lstm-cnn':
        model.add(Conv1D(filters,
                  kernel_size,
                  input_shape=(look_back, d),
                  padding='valid',
                  activation='relu',
                  strides=1))
        model.add(MaxPooling1D(pool_size=pool_size))
        model.add(LSTM(n_units[0],
                       return_sequences=True))
        for n_unit in n_units[1:]:
            model.add(LSTM(n_unit))
    elif method == 'gru-cnn':
        model.add(Conv1D(filters,
                  kernel_size,
                  input_shape=(look_back, d),
                  padding='valid',
                  activation='relu',
                  strides=1))
        model.add(MaxPooling1D(pool_size=pool_size))
        model.add(GRU(n_units[0],
                      return_sequences=True))
        for n_unit in n_units[1:]:
            model.add(GRU(n_unit))
    elif method == 'sgru-cnn':
        model.add(Conv1D(filters,
                  kernel_size,
                  input_shape=(look_back, d),
                  padding='valid',
                  activation='relu',
                  strides=1))
        model.add(MaxPooling1D(pool_size=pool_size))
        model.add(sGRU(n_units[0],
                       return_sequences=True))
        for n_unit in n_units[1:]:
            model.add(sGRU(n_unit))
    elif method == 'plstm-cnn':
        model.add(Conv1D(filters,
                  kernel_size,
                  input_shape=(look_back, d),
                  padding='valid',
                  activation='relu',
                  strides=1))
        model.add(MaxPooling1D(pool_size=pool_size))
        model.add(PLSTM(n_units[0],
                        return_sequences=True))
        for n_unit in n_units[1:]:
            model.add(PLSTM(n_unit))
    elif method == 'bi-lstm-cnn':
        model.add(Conv1D(filters,
                  kernel_size,
                  input_shape=(look_back, d),
                  padding='valid',
                  activation='relu',
                  strides=1))
        model.add(MaxPooling1D(pool_size=pool_size))
        model.add(Bidirectional(LSTM(n_units[0],
                                return_sequences=True)))
        for n_unit in n_units[1:]:
            model.add(LSTM(n_unit))
    elif method == 'bi-gru-cnn':
        model.add(Conv1D(filters,
                  kernel_size,
                  input_shape=(look_back, d),
                  padding='valid',
                  activation='relu',
                  strides=1))
        model.add(MaxPooling1D(pool_size=pool_size))
        model.add(Bidirectional(GRU(n_units[0],
                                return_sequences=True)))
        for n_unit in n_units[1:]:
            model.add(GRU(n_unit))
    elif method == 'bi-sgru-cnn':
        model.add(Conv1D(filters,
                  kernel_size,
                  input_shape=(look_back, d),
                  padding='valid',
                  activation='relu',
                  strides=1))
        model.add(MaxPooling1D(pool_size=pool_size))
        model.add(Bidirectional(sGRU(n_units[0],
                                return_sequences=True)))
        for n_unit in n_units[1:]:
            model.add(Bidirectional(sGRU(n_unit)))
    elif method == 'bi-plstm-cnn':
        model.add(Conv1D(filters,
                  kernel_size,
                  input_shape=(look_back, d),
                  padding='valid',
                  activation='relu',
                  strides=1))
        model.add(MaxPooling1D(pool_size=pool_size))
        model.add(Bidirectional(PLSTM(n_units[0],
                                return_sequences=True)))
        for n_unit in n_units[1:]:
            model.add(Bidirectional(PLSTM(n_unit)))
    for dense_n_unit in dense_units:
        model.add(Dense(dense_n_unit, activation='relu'))
    model.add(Dense(d + 1))
    if 'plstm' in method:
        adam = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
        model.compile(loss='mean_squared_error', optimizer=adam)
    else:
        model.compile(loss='mean_squared_error', optimizer='adam')
    return model
예제 #10
0
class LossHistory(Callback):
    def on_train_begin(self, logs={}):
        self.losses = []

    def on_batch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))

data = pd.read_csv('D:\meisai\CARNT.csv')
data = data.ix[0:, 2:]
data = data.dropna()
T = data.columns.drop(['R_N_T', 'TETCB_Data', 'RETCB_Data'], 1)
T = T[0:5]

batch_index,train_x,train_y=get_train_data(data,T,'R_N_T')
mean,std,test_x,test_y=get_test_data(data,T,'R_N_T')

model_PLSTM = Sequential()
model_PLSTM.add(PLSTM(32, input_shape=(28 * 28, 1), implementation=2))
model_PLSTM.add(Dense(10, activation='softmax'))
model_PLSTM.compile(optimizer='rmsprop', loss='categorical_crossentropy',
                        metrics=['accuracy'])
model_PLSTM.summary()
acc_PLSTM = AccHistory()
loss_PLSTM = LossHistory()
model_PLSTM.fit(train_x, train_y, epochs=10000, batch_size=12,
                    callbacks=[acc_PLSTM, loss_PLSTM])
score_PLSTM = model_PLSTM.evaluate(test_x, test_y, verbose=0)
print(score_PLSTM)