示例#1
0
def fit_lstm(train, batch_size, nb_epoch, neurons):
    X, y = train[:, 0:-1], train[:, -1]
    X = X.reshape(X.shape[0], 1, X.shape[1])
    model = Sequential()
    model.add(LSTM(neurons, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    for i in range(nb_epoch):
        model.fit(X, y, epochs=1, batch_size=batch_size, verbose=0, shuffle=False)
        model.reset_states()
    return model
def fit_lstm(train, n_vars, n_seq, n_batch, nb_epoch, n_neurons):
    # reshape training into [samples, timesteps, features]
    X, y = train[:, 0:n_vars], train[:, n_vars:]
    X = X.reshape(X.shape[0], 1, X.shape[1])
    # design network
    model = Sequential()
    model.add(
        LSTM(n_neurons,
             batch_input_shape=(n_batch, X.shape[1], X.shape[2]),
             stateful=True))
    model.add(Dense(y.shape[1]))
    model.compile(loss='mean_squared_error', optimizer='adam')
    # fit network
    for i in range(nb_epoch):
        model.fit(X, y, epochs=1, batch_size=n_batch, verbose=0, shuffle=False)
        model.reset_states()
        print('epoch %d is finished' % i)
    return model
示例#3
0
"""
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1.]]
"""
print(seq2Y)

model = Sequential()
model.add(LSTM(20, batch_input_shape=(1, 1, 5), stateful=True))
model.add(Dense(5, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')

for i in range(650):
    model.fit(seq1X, seq1Y, epochs=1, batch_size=1, verbose=1, shuffle=False)
    model.reset_states()
    model.fit(seq2X, seq2Y, epochs=1, batch_size=1, verbose=0, shuffle=False)
    model.reset_states()

n_batch = 1

print('Sequence 1')
result = model.predict_classes(seq1X, batch_size=n_batch, verbose=0)
model.reset_states()
for i in range(len(result)):
    print('X=%.1f y=%.1f, yhat=%.1f' % (seq1[i], seq1[i + 1], result[i]))

# 测试 LSTM对“数列2”预测
print('Sequence 2')
result = model.predict_classes(seq2X, batch_size=n_batch, verbose=0)
model.reset_states()
    def fitting(self):   
   
        dim_row = self.lags   # tiempo
        dim_col = 1    # features or chanels (Volume)
        output_dim = 3  # 3 for categorical
        
        
        #data = np.random.random((1000, dim_row, dim_col))
        #clas = np.random.randint(3, size=(1000, 1))
        ##print(clas)
        #clas = to_categorical(clas)
        ##print(clas)
        data = self.X_train
        data_test = self.X_test
                
        data = data.values.reshape(-1, dim_row, dim_col)
        data_test = data_test.values.reshape(-1, dim_row, dim_col)
        
        clas = self.y_train
        clas_test = self.y_test 
        clas = to_categorical(clas)
        clas_test = to_categorical(clas_test)

        cat0 = self.y_train.tolist().count(0)
        cat1 = self.y_train.tolist().count(1)
        cat2 = self.y_train.tolist().count(2)
        
        print("may: ", cat1, "  ", "menor: ", cat2, " ", "neutro: ", cat0)
        
        n_samples_0 = cat0
        n_samples_1 = (cat1 + cat2)/2.0
        n_samples_2 = (cat1 + cat2)/2.0

        class_weight={
                0: 1.0,
                1: n_samples_0/n_samples_1,
                2: n_samples_0/n_samples_2}            
        
        def class_1_accuracy(y_true, y_pred):
        # cojido de: http://www.deepideas.net/unbalanced-classes-machine-learning/
            class_id_true = K.argmax(y_true, axis=-1)
            class_id_preds = K.argmax(y_pred, axis=-1)
            
            accuracy_mask = K.cast(K.equal(class_id_preds, 1), 'int32')
            class_acc_tensor = K.cast(K.equal(class_id_true, class_id_preds), 'int32') * accuracy_mask
            
            class_acc = K.sum(class_acc_tensor) / K.maximum(K.sum(accuracy_mask), 1)
            return class_acc
        
        
        class SecondOpinion(Callback):
            def __init__(self, model, x_test, y_test, N):
                self.model = model
                self.x_test = x_test
                self.y_test = y_test
                self.N = N
                self.epoch = 1
        
            def on_epoch_end(self, epoch, logs={}):
                if self.epoch % self.N == 0:
                    y_pred = self.model.predict(self.x_test)
                    pred_T = 0
                    pred_F = 0
                    for i in range(len(y_pred)):
                        if np.argmax(y_pred[i]) == 1 and np.argmax(self.y_test[i]) == 1:
                            pred_T += 1
                        if np.argmax(y_pred[i]) == 1 and np.argmax(self.y_test[i]) != 1:
                            pred_F += 1
                    if pred_T + pred_F > 0:
                        Pr_pos = pred_T/(pred_T + pred_F)
                        print("Yoe: epoch, Probabilidad pos: ", self.epoch, Pr_pos)
                    else:
                        print("Yoe Probabilidad pos: 0")
                self.epoch += 1
        
        
        
        
        
#################################################################################################################        
        model = Sequential()
#        model.add(Reshape(input_shape=(dim_row, dim_col), target_shape=(dim_row, dim_col, 1)))

        if self.nConv > 0:
            #model.add(Reshape((dim_row, dim_col, 1)))
            model.add(Reshape(input_shape=(dim_row, dim_col), target_shape=(dim_row, dim_col, 1)))
            for i in range(self.nConv):
                model.add(Convolution2D(self.conv_nodes, kernel_size = (self.kernel_size, 1), padding = 'same', kernel_regularizer = regularizers.l2(0.01)))
                model.add(Activation('relu'))
            model.add(Reshape(target_shape=(dim_row, self.conv_nodes * dim_col)))
        # Como nuestro output tiene una sola dimension no es necesario "return_sequences='True'"
        # y tampoco es necesario usar TimeDistributed
        if self.nConv == 0:
            model.add(LSTM(units=self.lstm_nodes, return_sequences=True, activation='tanh', input_shape=(dim_row, dim_col)))
        for i in range(self.nLSTM - 1):
            model.add(LSTM(units=self.lstm_nodes, return_sequences=True, activation='tanh'))
        model.add(Dropout(0.5))
        model.add(TimeDistributed(Dense(units = output_dim))) # the dimension of index one will be considered to be the temporal dimension
        model.add(Activation('softmax'))  # for loss = 'categorical_crossentropy'
        #model.add(Activation('sigmoid'))  # for loss = 'binary_crossentropy'
        
        # haciendo x: x[:, -1, :], la segunda dimension desaparece quedando solo 
        # los ULTIMOS elementos (-1) de dicha dimension:
        # Try this to see:
        # data = np.random.random((5, 3, 4))
        # print(data)
        # print(data[:, -1, :])  
        
        model.add(Lambda(lambda x: x[:, -1, :], output_shape = [output_dim]))
        print(model.summary())
        
        tensorboard_active = False
        val_loss = False
        second_opinion = True
        callbacks = []
        if tensorboard_active:
            callbacks.append(TensorBoard(
                log_dir=self.putmodel + "Tensor_board_data",
                histogram_freq=0,
                write_graph=True,
                write_images=True))
        if val_loss:
            callbacks.append(EarlyStopping(
                monitor='val_loss', 
                patience=5))
        if second_opinion:
            callbacks.append(SecondOpinion(model, data_test, clas_test, 10))
        #model.compile(loss = 'categorical_crossentropy', optimizer='Adam', metrics = ['categorical_accuracy'])
        #model.compile(loss = 'binary_crossentropy', optimizer=Adam(lr=self.learning), metrics = ['categorical_accuracy'])
        model.compile(loss = 'categorical_crossentropy', optimizer='Adam', metrics = [class_1_accuracy])
                
        model.fit(x=data, 
                  y=clas,
                  batch_size=self.batch_size, epochs=800, verbose=2, 
                  callbacks = callbacks,
                  class_weight = class_weight)
                  #validation_data=(data_test, clas_test))
        
#####################################################################################################################
        
        # serialize model to YAML
        model_yaml = model.to_yaml()
        with open("model.yaml", "w") as yaml_file:
            yaml_file.write(model_yaml)
        # serialize weights to HDF5
        model.save_weights("model.h5")
        print("Saved model to disk")
        
#        # load YAML and create model
#        yaml_file = open('model.yaml', 'r')
#        loaded_model_yaml = yaml_file.read()
#        yaml_file.close()
#        loaded_model = model_from_yaml(loaded_model_yaml)
#        # load weights into new model
#        loaded_model.load_weights("model.h5")
#        print("Loaded model from disk")
#        loaded_model.compile(loss = 'categorical_crossentropy', optimizer='Adam', metrics = [class_1_accuracy])
#        
        print("Computing prediction ...")
        y_pred = model.predict_proba(data_test)
        
        model.reset_states()
        print("Computing train evaluation ...")
        score_train = model.evaluate(data, clas, verbose=2)
        print('Train loss:', score_train[0])
        print('Train accuracy:', score_train[1])

        model.reset_states()
#        score_train_loaded = loaded_model.evaluate(data, clas, verbose=2)
#        loaded_model.reset_states()
#        print('Train loss loaded:', score_train[0])
#        print('Train accuracy loaded:', score_train[1])

        print("Computing test evaluation ...")
        score_test = model.evaluate(data_test, clas_test, verbose=2)
        print('Test loss:', score_test[0])
        print('Test accuracy:', score_test[1])

        model.reset_states()
#        score_test_loaded = loaded_model.evaluate(data_test, clas_test, verbose=2)
#        loaded_model.reset_states()
#        print('Test loss loaded:', score_test[0])
#        print('Test accuracy loaded:', score_test[1])

        
        pred_T = 0
        pred_F = 0        
        for i in range(len(y_pred)):
            if np.argmax(y_pred[i]) == 1 and np.argmax(clas_test[i]) == 1:
                pred_T += 1
#                print(y_pred[i])
            if np.argmax(y_pred[i]) == 1 and np.argmax(clas_test[i]) != 1:
                pred_F += 1
        if pred_T + pred_F > 0:
            Pr_pos = pred_T/(pred_T + pred_F)
            print("Yoe Probabilidad pos: ", Pr_pos)
        else:
            print("Yoe Probabilidad pos: 0")
        
        history = DataFrame([[self.skip, self.nConv, self.nLSTM, 
                    self.learning, self.batch_size, 
                    self.conv_nodes, self.lstm_nodes, 
                    score_train[0], score_train[1], 
                    score_test[0], score_test[1]]], columns = ('Skip', 'cConv', 'nLSTM', 'learning', 
                                 'batch_size', 'conv_nodes', 'lstm_nodes', 
                                 'loss_train', 'acc_train', 'loss_test', 'acc_test'))
        self.history = self.history.append(history)
示例#5
0
class LstmForecasterMultipleLookAhead(object):

    def __create_sliding_window_dataset(self, input_time_series, look_back, look_forward):
        data_x, data_y = [], []
        for index in range(len(input_time_series) - look_back - look_forward):
            data_x.append(input_time_series[index:index + look_back])
            data_y.append(input_time_series[index + look_back: index + look_back + look_forward])

        return np.array(data_x), np.array(data_y)

    def __init__(self, time_series, look_back, look_forward, term_name, stateful=True, random_seed=None, verbose=False):

        self.__verbose = verbose
        self.__term_name = term_name

        if random_seed is not None:
            np.random.seed(random_seed)

        self.__source_time_series = np.reshape(time_series.astype(dtype=np.float), newshape=(-1, 1))
        self.__look_back = look_back
        self.__look_forward = look_forward

        time_series_delta = time_series[1:] - time_series[:-1]
        self.__source_time_series_delta = np.reshape(time_series_delta.astype(dtype=np.float), newshape=(-1, 1))

        self.__scaler = MinMaxScaler(feature_range=(-1, 1))
        data_set = self.__scaler.fit_transform(self.__source_time_series_delta)
        data_set_x, data_set_y = self.__create_sliding_window_dataset(data_set, look_back, look_forward)
        self.__test_train_split = 0.67
        train_size = int(len(data_set_x) * self.__test_train_split)
        train_x = data_set_x[0:train_size, :]
        train_y = data_set_y[0:train_size, :]

        test_x = data_set_x[train_size:, :]
        test_y = data_set_y[train_size:, :]

        self.__hidden_neurons = 100  # 20  # 100

        if stateful:
            self.__model_name = 'encode-decode-stateful'
            self.__num_epochs = 200
            batch_size = 1

            self.__model = Sequential()
            self.__model.add(
                LSTM(batch_input_shape=(batch_size, None, 1), units=self.__hidden_neurons, return_sequences=False,
                     stateful=True))
            self.__model.add(RepeatVector(look_forward))
            self.__model.add(LSTM(units=self.__hidden_neurons, return_sequences=True, stateful=True))
            self.__model.add(TimeDistributed(Dense(1)))
            self.__model.add(Activation('linear'))
            if self.__verbose:
                self.__model.summary(print_fn=print)
            self.__model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])

            epoch_iterator = range(self.__num_epochs)
            if self.__verbose:
                epoch_iterator = tqdm(epoch_iterator, unit='epoch')
            for _ in epoch_iterator:
                history = self.__model.fit(x=train_x, y=train_y, validation_data=(test_x, test_y), epochs=1,
                                           batch_size=batch_size, verbose=0)
                val_loss = history.history['val_loss'][0]
                loss = history.history['loss'][0]
                if self.__verbose:
                    epoch_iterator.set_description(f"val loss={val_loss:0.3f} loss={loss:0.3f}")
                self.__model.reset_states()
        else:
            self.__model_name = 'encode-decode-stateless'
            self.__model = Sequential()
            self.__model.add(LSTM(input_shape=(None, 1), units=self.__hidden_neurons, return_sequences=False))
            self.__model.add(RepeatVector(look_forward))
            self.__model.add(LSTM(units=self.__hidden_neurons, return_sequences=True))
            self.__model.add(TimeDistributed(Dense(1)))
            self.__model.add(Activation('linear'))
            if self.__verbose:
                self.__model.summary(print_fn=print)
            self.__model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])

            early_stopping = EarlyStopping(monitor='loss', min_delta=0.000001,
                                           patience=50)  # this big patience is important
            callbacks = [early_stopping]
            if self.__verbose:
                callbacks.append(TQDMCallback())
            history = self.__model.fit(x=train_x, y=train_y, validation_data=(test_x, test_y), epochs=10000,
                                       callbacks=callbacks, verbose=0)
            self.__num_epochs = len(history.epoch)

    @property
    def configuration(self):
        return f'TTSplit={self.__test_train_split:.2} numEpochs={self.__num_epochs} numHN={self.__hidden_neurons}'

    def predict_counts(self, time_series=None):
        if time_series is None:
            time_series = self.__source_time_series
            source_time_series_delta = self.__source_time_series_delta
        else:
            time_series_delta = time_series[1:] - time_series[:-1]
            source_time_series_delta = np.reshape(time_series_delta.astype(dtype=np.float), newshape=(-1, 1))

        data_set = self.__scaler.fit_transform(source_time_series_delta.astype(dtype=np.float))

        data_set_seed = data_set[-self.__look_back:]
        reshaped_seed = np.reshape(data_set_seed, (1, -1, 1))
        predicted_sequence_scaled_diff = self.__model.predict(reshaped_seed).reshape(-1, 1)
        predicted_sequence_diff = self.__scaler.inverse_transform(predicted_sequence_scaled_diff)

        previous_predicted_value = time_series[-1]
        predicted_values = []
        for step in range(self.__look_forward):
            predicted_scaled_diff = previous_predicted_value + predicted_sequence_diff[step][0]
            predicted_values.append(predicted_scaled_diff)
            previous_predicted_value = predicted_scaled_diff

        return clip(predicted_values, 0, inf)

    def plot_prediction(self, ground_truth, prediction):
        common_plot_prediction(ground_truth, prediction, self.__term_name, self.__model_name, self.__look_back,
                               self.__look_forward, self.__hidden_neurons, self.__num_epochs)