Ejemplo n.º 1
0
model = Sequential()
model.add(
    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(48, 48, 1)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer=Adam(lr=0.0001, decay=1e-6),
              metrics=['accuracy'])

model_info = model.fit_generator(train_generator,
                                 steps_per_epoch=num_train // batch_size,
                                 epochs=num_epoch,
                                 validation_data=validation_generator,
                                 validation_steps=num_val // batch_size)
plot_model_history(model_info)
model.save_weights('model.h5')
Ejemplo n.º 2
0
    def fitting(self):

        timesteps = self.lags  # tiempo
        features = 1  # features or chanels (Volume)
        num_classes = 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
        print(data)

        data = data.values.reshape(len(data), timesteps, 1)
        data_test = data_test.values.reshape(len(data_test), timesteps, 1)
        print(data)

        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 np.argmax(y_pred[i]) == 2 and np.argmax(
                                self.y_test[i]) == 2:
                            pred_T += 1
                        if np.argmax(y_pred[i]) == 2 and np.argmax(
                                self.y_test[i]) != 2:
                            pred_F += 1

                    if pred_T + pred_F > 0:
                        Pr_pos = pred_T / (pred_T + pred_F)
                        print("Yoe: epoch, Probabilidad tot: ", self.epoch,
                              Pr_pos)
                        print("Yoe: epoch, Cantidad total pred: ", self.epoch,
                              pred_T + pred_F)
                    else:
                        print("Yoe Probabilidad pos: 0")
                self.epoch += 1

#################################################################################################################

        model = Sequential()
        if self.nConv == 0:
            model.add(
                LSTM(units=self.lstm_nodes,
                     return_sequences=True,
                     activation='tanh',
                     input_shape=(timesteps, features)))
        for i in range(self.nLSTM - 2):
            model.add(
                LSTM(units=self.lstm_nodes,
                     return_sequences=True,
                     activation='tanh'))
        model.add(LSTM(units=self.lstm_nodes, activation='tanh'))
        model.add(Dropout(0.5))
        model.add(
            Dense(num_classes, activation='softmax')
        )  # the dimension of index one will be considered to be the temporal dimension
        #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
            if np.argmax(y_pred[i]) == 1 and np.argmax(clas_test[i]) != 1:
                pred_F += 1
            if np.argmax(y_pred[i]) == 2 and np.argmax(clas_test[i]) == 2:
                pred_T += 1
            if np.argmax(y_pred[i]) == 2 and np.argmax(clas_test[i]) != 2:
                pred_F += 1

        if pred_T + pred_F > 0:
            Pr_pos = pred_T / (pred_T + pred_F)
            print("Yoe Probabilidad tot: ", Pr_pos)
            print("Cantidad total pred: ", pred_T + pred_F)
        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)
Ejemplo n.º 3
0

# fit model
model.fit(X_, Y, epochs=30)

#==================================================================================

# Saving model

# %cd /content/sample_data

model_json = model.to_json()
json_file = open("modelDENSE.json", "w")
json_file.write(model_json)
json_file.close()
model.save_weights("modelDENSE.h5")
print("Model saved to disk")

# # load json and create model
# json_file = open('modelDENSE.json', 'r')
# loaded_model_json = json_file.read()
# json_file.close()
# loaded_model = model_from_json(loaded_model_json)
# # load weights into new model
# loaded_model.load_weights("modelDENSE.h5")
# print("Loaded model from disk")
#
# model = loaded_model

# Print model
plot_model(model, to_file='modelDENSE.png')
class DNN_Model(Common_Model):
    '''
    __init__(): 初始化神经网络

    输入:
        input_shape: 特征维度
        num_classes(int): 标签种类数量
    '''
    def __init__(self, input_shape, num_classes, **params):
        super(DNN_Model, self).__init__(**params)
        self.input_shape = input_shape
        self.model = Sequential()
        self.make_model()
        self.model.add(Dense(num_classes, activation='softmax'))
        self.model.compile(loss='binary_crossentropy',
                           optimizer='adam',
                           metrics=['accuracy'])
        print(self.model.summary(), file=sys.stderr)

    '''
    save_model(): 将模型权重以 model_name.h5 和 model_name.json 命名存储在 /Models 目录下
    '''

    def save_model(self, model_name):
        h5_save_path = 'Models/' + model_name + '.h5'
        self.model.save_weights(h5_save_path)

        save_json_path = 'Models/' + model_name + '.json'
        with open(save_json_path, "w") as json_file:
            json_file.write(self.model.to_json())

    '''
    train(): 在给定训练集上训练模型

    输入:
        x_train (numpy.ndarray): 训练集样本
        y_train (numpy.ndarray): 训练集标签
        x_val (numpy.ndarray): 测试集样本
        y_val (numpy.ndarray): 测试集标签
        n_epochs (int): epoch数

    '''

    def train(self, x_train, y_train, x_val=None, y_val=None, n_epochs=50):
        acc = []
        loss = []
        val_acc = []
        val_loss = []

        if x_val is None or y_val is None:
            x_val, y_val = x_train, y_train
        for i in range(n_epochs):
            # 每个epoch都随机排列训练数据
            p = np.random.permutation(len(x_train))
            x_train = x_train[p]
            y_train = y_train[p]

            history = self.model.fit(x_train, y_train, batch_size=32, epochs=1)
            # 训练集上的损失率和准确率
            acc.append(history.history['acc'])
            loss.append(history.history['loss'])
            # 验证集上的损失率和准确率
            val_loss_single, val_acc_single = self.model.evaluate(x_val, y_val)
            val_acc.append(val_acc_single)
            val_loss.append(val_loss_single)

        plotCurve(acc, val_acc, 'LSTM Accuracy', 'acc')
        plotCurve(loss, val_loss, 'LSTM Loss', 'loss')
        self.trained = True

    '''
    predict(): 识别音频的情感

    输入:
        samples: 需要识别的音频特征

    输出:
        list: 识别结果
    '''

    def predict(self, sample):
        # 没有训练和加载过模型
        if not self.trained:
            sys.stderr.write("No Model.")
            sys.exit(-1)

        return np.argmax(self.model.predict(sample), axis=1)

    def make_model(self):
        raise NotImplementedError()
Ejemplo n.º 5
0
print('\ntrain loss: ', loss)
print('\ntrain accuracy: ', accuracy)

from sklearn import metrics
y_pred = model.predict(X_train)
print(y_pred)
fpr, tpr, thresholds = metrics.roc_curve(y_train, y_pred, pos_label=1)
metrics.auc(fpr, tpr)
print('\ntrain AUC: ', metrics.auc(fpr, tpr))

loss, accuracy = model.evaluate(X_test, y_test, batch_size=batch_size)

yaml_string = model.to_yaml()
with open('DNN.yml', 'w') as outfile:
    outfile.write(yaml.dump(yaml_string, default_flow_style=True))
model.save_weights('DNN.h5')

print('\ntest loss: ', loss)
print('\ntest accuracy: ', accuracy)

print(y_pred)

from sklearn import metrics
y_pred = model.predict(X_test)

fpr, tpr, thresholds = metrics.roc_curve(y_test, y_pred, pos_label=1)
metrics.auc(fpr, tpr)
print('\ntest AUC: ', metrics.auc(fpr, tpr))

plt.title('Receiver Operating Characteristic DNN')
plt.plot(fpr, tpr, 'b', label='AUC = %0.2f' % metrics.auc(fpr, tpr))
class LSTMAutoencoder:
    EPOCHS = 100
    MIN_EPOCHS = 1
    BATCH_SIZE = 64
    VALIDATION_SPLIT = 0.05

    EWMA_WINDOW_SIZE = 120
    EWMA_ALPHA = 1 - np.exp(-np.log(2) / EWMA_WINDOW_SIZE)

    def __init__(self,
                 signals_count: int,
                 models_dir: str = '',
                 tensorboard_dir: str = '') -> None:
        self._signals_count = signals_count
        self._models_dir = models_dir or os.path.join(PROJECT_PATH, 'models')
        self._tensorboard_dir = tensorboard_dir

        self._model = Sequential([
            LSTM(units=64, activation='relu', input_shape=(signals_count, 1)),
            RepeatVector(signals_count),
            LSTM(units=64, activation='relu', return_sequences=True),
            TimeDistributed(Dense(1)),
        ])

    def train(self,
              signals_group: SignalsGroup,
              clipnorm: float = 0.) -> History:
        if len(signals_group.signals_data) != self._signals_count:
            raise ValueError(
                f'Модель может обработать строго {self._signals_count} сигналов'
            )

        print(f'Обучение LSTM-автокодировщика для группы сигналов '
              f'"{signals_group.name}" {signals_group.signals}...')

        x_train = np.column_stack([
            self._preprocess(signal)
            for signal in signals_group.signals_data.values()
        ])
        x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1],
                                       1))  # samples, sample_len, features

        optimizer = optimizers.Adam(clipnorm=clipnorm)
        self._model.compile(optimizer=optimizer, loss='mse')

        callbacks = [
            EarlyStopping('val_loss', patience=self.MIN_EPOCHS,
                          min_delta=0.05),
        ]
        if self._tensorboard_dir:
            os.makedirs(os.path.dirname(self._tensorboard_dir), exist_ok=True)
            callbacks.append(
                TensorBoard(
                    log_dir=self._get_tensorboard_logs_dir(signals_group.name),
                    batch_size=self.BATCH_SIZE,
                    histogram_freq=0,
                    write_graph=True,
                    write_grads=True,
                    write_images=True,
                ))

        history = self._model.fit(
            x_train,
            x_train,
            batch_size=self.BATCH_SIZE,
            epochs=self.EPOCHS,
            validation_split=self.VALIDATION_SPLIT,
            shuffle=True,
            callbacks=callbacks,
        )

        models_path = self._get_model_path(signals_group.name)
        os.makedirs(os.path.dirname(models_path), exist_ok=True)
        self._model.save_weights(models_path)
        print(f'Модель сохранена в "{models_path}"')

        return history

    def analyze(self, signals_group: SignalsGroup) -> AutoencoderResult:
        if len(signals_group.signals) != self._signals_count:
            raise ValueError(
                f'Модель может обработать строго {self._signals_count} сигналов'
            )

        model_path = self._get_model_path(signals_group.name)
        if not os.path.exists(model_path):
            raise FileNotFoundError(
                f'Модель для группы сигналов {signals_group.name} не найдена. Выполните обучение'
            )
        self._model.load_weights(model_path)

        data = np.array([
            self._preprocess(signal)
            for signal in signals_group.signals_data.values()
        ])
        data_stacked = np.column_stack(data)
        # samples, sample_len, features
        data_reshaped = data_stacked.reshape(data_stacked.shape[0],
                                             data_stacked.shape[1], 1)

        decoded_data_reshaped = self._model.predict(data_reshaped,
                                                    batch_size=self.BATCH_SIZE)
        decoded_data = decoded_data_reshaped.reshape(data_stacked.shape)
        decoded_data = np.column_stack(decoded_data)

        mse = np.sum([
            squared_error(predictions, targets)
            for predictions, targets in zip(decoded_data, data)
        ],
                     axis=0)
        ewma_mse = ewma(mse,
                        window=self.EWMA_WINDOW_SIZE,
                        alpha=self.EWMA_ALPHA)

        return AutoencoderResult(
            signals=data,
            decoded_signals=decoded_data,
            mse=mse,
            ewma_mse=ewma_mse,
        )

    def plot_model(self,
                   img_path: str,
                   show_shapes=True,
                   show_layer_names=True) -> None:
        plot_model(
            self._model,
            to_file=img_path,
            show_shapes=show_shapes,
            show_layer_names=show_layer_names,
        )

    @staticmethod
    def _preprocess(data: np.ndarray) -> np.ndarray:
        return z_normalization(fill_zeros_with_previous(data))

    def _get_model_path(self, group_name: str) -> str:
        return os.path.join(self._models_dir, 'autoencoder',
                            f'{group_name}.h5')

    def _get_tensorboard_logs_dir(self, group_name: str) -> str:
        return os.path.join(self._tensorboard_dir, 'autoencoder', group_name)
X_test = sequence.pad_sequences(X_test, maxlen = 500)

from keras import Sequential
from keras.layers import Embedding, LSTM, Dense, Dropout, Bidirectional

model = Sequential()
model.add(Embedding(num_words, 32, input_length = 500))
model.add(LSTM(units = 100))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size = 64, epochs = 6)

model_json = model.to_json()
with open("Model_Save/model_json", "w") as json_file:
    json_file.write(model_json)

model.save_weights("Model_Save/model.h5")

if __name__ == '__main__':
    print(model.summary())
    scores = model.evaluate(X_test, y_test, verbose=0) 
    print("Accuracy: %.2f%%" % (scores[1]*100))








Ejemplo n.º 8
0
model.compile(loss=custom_loss, optimizer='adam')
model.summary()

print("model compiled")
# generators
data_generator = ImageDataGenerator()
train_generator = data_generator.flow_from_directory(
    TRAIN_DATASET,
    target_size=image_template.shape[0:2],
    batch_size=4,
    color_mode="rgb")
test_generator = data_generator.flow_from_directory(
    TEST_DATASET,
    target_size=image_template.shape[0:2],
    batch_size=4,
    color_mode="rgb")

# fitting
print(len(train_generator))
model.fit_generator(test_generator,
                    steps_per_epoch=16,
                    epochs=20,
                    verbose=1,
                    callbacks=[tb_call_back])

# Saving the model
model.save_weights('pklot_1.h5')

scores = model.evaluate_generator(train_generator)
print(scores)
class QReplayDoubleAugmPrior5fan(AbstractModel):
    """ Prediction model which uses Q-learning and a neural network which replays past moves.

        The network learns by replaying a batch of training moves. The training algorithm ensures that
        the game is started from every possible cell. Training ends after a fixed number of games, or
        earlier if a stopping criterion is reached (here: a 100% win rate).

        :param class Maze game: Maze game object.
    """
    def __init__(self, game, **kwargs):
        super().__init__(game, **kwargs)
        self.game = game
        self.state_size = (5 + 2, )

        if kwargs.get("load", False) is False:
            self.model = Sequential()
            self.model.add(
                Dense(game.maze.size,
                      input_shape=self.state_size,
                      activation="relu"))
            self.model.add(Dense(game.maze.size, activation="relu"))
            self.model.add(Dense(len(actions)))
        else:
            self.load(self.name)

        self.model.compile(optimizer="adam", loss="mse")

        self.target_model = Sequential()
        self.target_model.add(
            Dense(game.maze.size,
                  input_shape=self.state_size,
                  activation="relu"))
        self.target_model.add(Dense(game.maze.size, activation="relu"))
        self.target_model.add(Dense(len(actions)))
        self.target_model.compile(optimizer="adam", loss="mse")

    def save(self, filename):
        with open(filename + ".json", "w") as outfile:
            outfile.write(self.model.to_json())
        self.model.save_weights(filename + ".h5", overwrite=True)

    def load(self, filename):
        with open(filename + ".json", "r") as infile:
            self.model = model_from_json(infile.read())
        self.model.load_weights(filename + ".h5")

    def train(self, stop_at_convergence=False, **kwargs):
        """ Hyperparameters:

            :keyword float discount: (gamma) preference for future rewards (0 = not at all, 1 = only)
            :keyword float exploration_rate: (epsilon) 0 = preference for exploring (0 = not at all, 1 = only)
            :keyword float exploration_decay: exploration rate reduction after each random step (<= 1, 1 = no at all)
            :keyword int episodes: number of training games to play
            :keyword int sample_size: number of samples to replay for training
            :return int, datetime: number of training episodes, total time spent
        """
        max_memory = kwargs.get("max_memory", 1000)
        discount = kwargs.get("discount", 0.90)
        exploration_rate = kwargs.get("exploration_rate", 0.10)
        exploration_decay = kwargs.get(
            "exploration_decay",
            0.995)  # % reduction per step = 100 - exploration decay
        episodes = kwargs.get("episodes", 10000)
        batch_size = kwargs.get("sample_size", 32)
        experience = ExperienceReplay(self.model,
                                      self.target_model,
                                      discount=discount,
                                      max_memory=max_memory)
        self.experience = experience

        # variables for reporting purposes
        cumulative_reward = 0
        cumulative_reward_history = []
        win_history = []

        start_list = list()  # starting cells not yet used for training
        start_time = datetime.now()

        for episode in range(1, episodes + 1):
            if not start_list:
                start_list = self.environment.empty.copy()
            start_cell = random.choice(start_list)
            start_list.remove(start_cell)
            state = self.environment.reset(start_cell)
            actions_counter = 0
            loss = 0.0
            self.game.old_action = (0, 0)

            while True:
                if np.random.random() < exploration_rate:
                    c_state = state[0][0]
                    r_state = state[0][1]
                    c_target, r_target = self.environment.exit
                    delta_r = r_target - r_state
                    delta_c = c_target - c_state
                    delta = np.abs(delta_r) + np.abs(delta_c)
                    delta_r_percent = np.abs(delta_r) / delta * 100
                    delta_c_percent = np.abs(delta_c) / delta * 100
                    move = tuple(np.sign([delta_c, delta_r]))
                    actions_list = self.environment.actions.copy()
                    actions_list.remove(move)
                    if np.sum(np.abs(move)) == len(move):  #diagonal movement
                        move_c = (move[0], 0)
                        move_r = (0, move[1])
                        actions_list.remove(move_c)
                        actions_list.remove(move_r)
                        if np.abs(delta_r - delta_c) < 20:
                            action_d_pool = [move] * 20
                            action_r_pool = [move_r] * 15
                            action_c_pool = [move_c] * 15
                            actions_pool = np.concatenate(
                                (action_d_pool, action_c_pool, action_r_pool))
                            for i in range(len(actions_list)):
                                actions_pool = np.concatenate(
                                    (actions_pool, [actions_list[i]] * 9))
                        else:
                            action_d_pool = [move] * 10
                            action_r_pool = [move_r] * int(
                                np.round(delta_r_percent * 35))
                            action_c_pool = [move_c] * int(
                                np.round(delta_c_percent * 35))
                            actions_pool = np.concatenate(
                                (action_d_pool, action_c_pool, action_r_pool))
                            for i in range(len(actions_list)):
                                actions_pool = np.concatenate(
                                    (actions_pool, [actions_list[i]] * 9))
                    else:
                        action_move_pool = [move] * 25
                        actions_pool = action_move_pool
                        for i in range(len(actions_list)):
                            actions_pool = np.concatenate(
                                (actions_pool, [actions_list[i]] * 11))
                    action = tuple(random.choice(actions_pool))

                    #action = random.choice(self.environment.actions)
                    action_index = [actions[action][1]]

                else:
                    # q = experience.predict(state)
                    # action = random.choice(np.nonzero(q == np.max(q))[0])
                    action, action_index = self.predict(state)

                next_state, reward, status = self.environment.step(action)
                state_augm = self.state_creator(state)
                next_state_augm = self.state_creator(next_state)
                cumulative_reward += reward
                experience.remember(state_augm, action_index, reward,
                                    next_state_augm, status)
                if status in ("win",
                              "lose"):  # terminal state reached, stop episode
                    break
                if experience.buffer.size() > 2 * batch_size:
                    inputs, targets = experience.get_samples(
                        sample_size=batch_size)
                    self.model.fit(inputs,
                                   targets,
                                   epochs=1,
                                   batch_size=16,
                                   verbose=0)
                    if actions_counter % 10 == 0:
                        experience.target_train()
                    actions_counter += 1
                    loss += self.model.evaluate(inputs, targets, verbose=0)
                state = next_state

                self.environment.render_q(self)

            cumulative_reward_history.append(cumulative_reward)

            logging.info(
                "episode: {:d}/{:d} | status: {:4s} | loss: {:.4f} | e: {:.5f}"
                .format(episode, episodes, status, loss, exploration_rate))

            if episode % 500 == -1:
                # check if the current model wins from all starting cells
                # can only do this if there is a finite number of starting states
                w_all, win_rate = self.environment.win_all(self)
                win_history.append((episode, win_rate))
                if w_all is True and stop_at_convergence is True:
                    logging.info("won from all start cells, stop learning")
                    break

            exploration_rate *= exploration_decay  # explore less as training progresses
        self.save(self.name)  # Save trained models weights and architecture

        now = datetime.now()
        time_elapsed = now - start_time
        self.time = now.timestamp() - start_time.timestamp()
        logging.info("episodes: {:d} | time spent: {}".format(
            episode, time_elapsed))

        return cumulative_reward_history, win_history, episode, datetime.now(
        ) - start_time

    def q(self, state):
        """ Get q values for all actions for a certain state. """
        state = self.state_creator(state)
        return self.model.predict(state)[0]

    def predict(self, state):
        """ Policy: choose the action with the highest value from the Q-table.
            Random choice if multiple actions have the same (max) value.

            :param np.ndarray state: Game state.
            :return int: Chosen action.
        """
        q = self.q(state)

        logging.debug("q[] = {}".format(q))

        actions_index = np.nonzero(
            q == np.max(q))[0]  # get index of the action(s) with the max value
        return self.environment.actions[random.choice(
            actions_index)], actions_index

    def state_creator(self, state):
        state_augm = np.zeros((1, self.state_size[0]))
        cols = state[0][0]
        rows = state[0][1]
        flags = dict()
        obst = dict()
        delta_x = self.game.exit_cell[0] - cols
        delta_y = self.game.exit_cell[1] - rows
        rays = list(actions.keys())
        move = tuple(np.sign((-delta_x, -delta_y)))  #move to remove (1 of 3)
        if (cols, rows) not in [self.game.exit_cell]:
            if np.sum(np.abs(move)) == 2:
                move_l = (move[0], 0)
                move_r = (0, move[0])
            elif np.sum(np.abs(move)) == 1:
                if move[0] == 0:
                    move_l = (-1, move[1])
                    move_r = (1, move[1])
                if move[1] == 0:
                    move_l = (move[0], -1)
                    move_r = (move[0], 1)
            rays.remove(move)
            rays.remove(move_l)
            rays.remove(move_r)
            for action in rays:
                for i_dist in range(1, self.game.maze.shape[0] + 1):
                    cell = (cols + i_dist * action[0],
                            rows + i_dist * action[1])
                    if flags.get(action, 0) == 0 and (
                            cell in self.game.walls or
                        (cell in self.game.cells
                         and self.game.maze[cell[::-1]] == 1)):
                        obst[action] = i_dist - 1
                        flags[action] = 1

            target_delta_col = self.game.exit_cell[0] - cols
            target_delta_row = self.game.exit_cell[1] - rows
            state_augm = np.array([[
                *tuple(obst.values()) + (target_delta_col, target_delta_row)
            ]])
        else:
            state_augm = np.array([[*(0, 0, 0, 0, 0, 0, 0)]])

        return state_augm
class QReplayNetworkModel(AbstractModel):
    """ Prediction model which uses Q-learning and a neural network which replays past experiences.

        The network learns by replaying a batches of training games. The training algorithm ensures that
        the game is started from every possible cell. Training ends after a fixed number of games, or
        earlier if a stopping criterion is reached (here: a 100% win rate).

        :param class Maze game: Maze game object.
    """
    def __init__(self, game, **kwargs):
        super().__init__(game, **kwargs)

        if kwargs.get("load", False) is False:
            self.model = Sequential()
            self.model.add(
                Dense(game.maze.size,
                      input_shape=(game.maze.size, ),
                      activation="relu"))
            self.model.add(Dense(game.maze.size, activation="relu"))
            self.model.add(Dense(len(actions), activation="linear"))
        else:
            self.load(self.name)

        self.model.compile(optimizer="adam", loss="mse")

    def save(self, filename):
        with open(filename + ".json", "w") as outfile:
            outfile.write(self.model.to_json())
        self.model.save_weights(filename + ".h5", overwrite=True)

    def load(self, filename):
        with open(filename + ".json", "r") as infile:
            self.model = model_from_json(infile.read())
        self.model.load_weights(filename + ".h5")

    def train(self, **kwargs):
        """ Hyperparameters:

            :keyword float discount: (gamma) preference for future rewards (0 = not at all, 1 = only)
            :keyword float exploration_rate: (epsilon) 0 = preference for exploring (0 = not at all, 1 = only)
            :keyword int episodes: number of training games to play
            :keyword int sample_size: number of samples to replay for training
            :return int, datetime: number of training episodes, total time spent
        """
        discount = kwargs.get("discount", 0.90)
        exploration_rate = kwargs.get("exploration_rate", 0.10)
        episodes = kwargs.get("episodes", 10000)
        sample_size = kwargs.get("sample_size", 32)

        experience = ExperienceReplay(self.model, discount=discount)

        wins = 0
        hist = []
        start_list = list()  # starting cells not yet used for training
        start_time = datetime.now()

        for episode in range(1, episodes):
            if not start_list:
                start_list = self.environment.empty.copy()
            start_cell = random.choice(start_list)
            start_list.remove(start_cell)

            state = self.environment.reset(start_cell)

            loss = 0.0

            while True:
                if np.random.random() < exploration_rate:
                    action = random.choice(self.environment.actions)
                else:
                    q = experience.predict(state)
                    mv = np.amax(q)
                    actions = np.nonzero(q == mv)[0]
                    action = random.choice(actions)

                next_state, reward, status = self.environment.step(action)

                experience.remember(
                    [state, action, reward, next_state, status])

                if status in ("win",
                              "lose"):  # terminal state reached, stop episode
                    if status == "win":
                        wins += 1
                    break

                inputs, targets = experience.get_samples(
                    sample_size=sample_size)

                self.model.fit(inputs,
                               targets,
                               epochs=4,
                               batch_size=16,
                               verbose=0)
                loss += self.model.evaluate(inputs, targets, verbose=0)

                state = next_state

            logging.info(
                "episode: {:d}/{:d} | status: {:4s} | loss: {:.4f} | total wins: {:d} | e: {:.5f}"
                .format(episode, episodes, status, loss, wins,
                        exploration_rate))

            if episode % 5 == 0:
                # check if the current model wins from all starting cells
                # can only do this if there is a finite number of starting states
                w_all, win_rate = self.environment.win_all(self)
                hist.append(win_rate)
                if w_all is True:
                    logging.info("won from all start cells, stop learning")
                    break

        self.save(self.name)  # Save trained models weights and architecture

        logging.info("episodes: {:d} | time spent: {}".format(
            episode,
            datetime.now() - start_time))

        return hist, episode, datetime.now() - start_time

    def predict(self, state):
        """ Policy: choose the action with the highest Q from the Q-table. Random choice if there are multiple actions
            with an equal max Q.

            :param np.array state: Game state.
            :return int: Chosen action.
        """
        q = self.model.predict(state)

        mv = np.amax(q[0])  # determine max Q
        actions = np.nonzero(
            q[0] == mv)[0]  # extract (index of) action(s) with the max Q
        return random.choice(actions)
Ejemplo n.º 11
0
class Agent:
    def __init__(self, color, eps, df, alpha):
        self.color = color
        self.eps = eps
        self.discount_factor = df
        self.alpha = alpha
        self.state_value = {}
        self.game_states = []
        self.nnInp = set()
        self.rating = 1600
        self.nnModel = 0
        self.initNNModel()

    def initNNModel(self):
        if not os.path.isfile("nn_%s_weights.h5" % (str(self.color))):
            print("Creating h5 Weight File.")
            self.nnModel = Sequential()
            self.nnModel.add(Dense(32, activation="tanh", input_dim=42))
            self.nnModel.add(Dense(32, activation="tanh"))
            self.nnModel.add(Dense(7, activation="sigmoid"))
            self.nnModel.save_weights("nn_%s_weights.h5" % (str(self.color)))

    def train(self):
        print("Start Training for Model %s." % (str(self.color)))
        X, Y = self.getNNInput()
        print("Shape for input and labels:", X.shape, Y.shape)
        self.nnModel.load_weights("nn_%s_weights.h5" % (str(self.color)))

        sgd = optimizers.SGD(lr=0.5, clipnorm=1.)
        self.nnModel.compile(loss="mse", optimizer=sgd, metrics=["accuracy"])
        self.nnModel.fit(X, Y, epochs=100)
        self.nnModel.save_weights("nn_%s_weights.h5" % (str(self.color)))

        print("Training complete....Model For %s Saved" % (str(self.color)))

    def getBestMoveNN(self, board):
        inp = []
        inp.append(self.serialize(self.getHash(board.board)))
        wm = board.checkForWinningMove(self.color)
        if wm > -1:
            return wm
        self.nnModel.load_weights("nn_%s_weights.h5" % (str(self.color)))
        m = self.nnModel.predict(np.array(inp))
        # print(m)

        for i in range(7):
            if board.cols[i] == -1:
                m[0][i] = 0

        res = np.where(m[0] == max(m[0]))
        return res[0][rd.randint(0, len(res) - 1)]

    def getBoardFromHash(self, h):
        board = np.zeros((6, 7))
        k = 0
        hToNum = {"_": 0, "R": 2, "Y": 1}
        for i in range(6):
            for j in range(7):
                board[i][j] = hToNum[h[k]]
                k += 1
        return board

    def setEpsilon(self, eps):
        self.eps = eps

    def serialize(self, state):
        arr = []
        hToNum = {"_": 0, "R": 2, "Y": 1}
        for i in state:
            arr.append(hToNum[i])
        return arr

    def getNNInput(self):
        labels = []
        inp = []
        for i in self.nnInp:
            arr = self.serialize(i)
            inp.append(arr)
            board = self.getBoardFromHash(i)
            labels.append(self.getNextStateVals(board))
        return np.array(inp), np.array(labels)

    def calculateExpectedScore(self, oppRating):
        return 1 / (1 + (10**((oppRating - self.rating) / 400)))

    def calculateRating(self, oppRating, res):
        self.rating = self.rating + (
            16 * (res - self.calculateExpectedScore(oppRating)))

    def getMaxFromRes(self, boardHash):
        return max(
            self.nnModel.predict(np.array([self.serialize(boardHash)]))[0])

    def getReward(self, reward):
        tot_reward = reward
        for i in range(len(self.game_states) - 1, -1, -1):
            if self.state_value.get(self.game_states[i]) is None:
                self.state_value[self.game_states[i]] = 0.5

            if i == len(self.game_states) - 1:
                self.state_value[self.game_states[i]] = tot_reward
                continue

            self.state_value[self.game_states[i]] += (
                (self.alpha *
                 (tot_reward - self.state_value[self.game_states[i]])) *
                self.discount_factor)
            tot_reward = self.state_value[self.game_states[i]]

    def resetState(self):
        self.game_states = []

    def decayEps(self, dec):
        self.eps = self.eps * dec

    def getRandomMove(self, board):
        while True:
            colR = rd.randint(0, 6)
            if board.cols[colR] == -1:
                continue
            return colR

    def getBestMove(self, board):
        maxv = -1
        moves = []
        for i in range(7):
            if board.cols[i] != -1:
                bcopy = board.board.copy()
                bcopy[board.cols[i]][i] = self.color
                if board.checkWinVirtual(bcopy, board.cols[i], i):
                    return i
                h = self.getHash(bcopy)
                if self.state_value.get(h):
                    val = self.state_value[h]
                    if val > maxv:
                        maxv = val
                        moves = []
                        moves.append(i)
                    elif val == maxv:
                        moves.append(i)
                else:
                    val = 0.5
                    if val > maxv:
                        maxv = val
                        moves = []
                        moves.append(i)
                    elif val == maxv:
                        moves.append(i)
        if len(moves) == 1:
            return moves[0]
        else:
            return moves[rd.randint(0, len(moves) - 1)]

    def getNextStateVals(self, board):
        vals = [0, 0, 0, 0, 0, 0, 0]
        max_ = -10000
        max_ind = []
        for i in range(7):
            if board[0][i] == 0:
                bcopy = board.copy()
                k = 5
                while True:
                    if bcopy[k][i] == 0:
                        break
                    k = k - 1

                bcopy[k][i] = self.color

                b = c4Board()

                if b.checkWinVirtual(bcopy, k, i):
                    vals[i] = 1
                    return vals

                h = self.getHash(bcopy)

                if self.state_value.get(h):
                    val = self.state_value[h]
                    if max_ < val:
                        max_ = val
                        max_ind = [i]
                    elif max_ == val:
                        max_ind.append(i)
                else:
                    val = 0.5
                    if max_ < val:
                        max_ = val
                        max_ind = [i]
                    elif max_ == val:
                        max_ind.append(i)

        if len(max_ind) == 1:
            vals[max_ind[0]] = 1
        else:
            vals[max_ind[rd.randint(0, len(max_ind) - 1)]] = 1

        return vals

    def getHash(self, board):  #get hash of current board
        h = ""
        nToHash = {0: "_", 2: "R", 1: "Y"}
        for i in range(6):
            for j in range(7):
                h = h + nToHash[board[i][j]]
        return h
Ejemplo n.º 12
0
class DNN_Model(Common_Model):
    '''

    input:
        input_shape
        num_classes(int)
    '''
    def __init__(self, input_shape, num_classes, **params):
        super(DNN_Model, self).__init__(**params)
        self.input_shape = input_shape
        self.model = Sequential()
        self.make_model()
        self.model.add(Dense(num_classes, activation='softmax'))
        self.model.compile(loss='binary_crossentropy',
                           optimizer='adam',
                           metrics=['accuracy'])
        print(self.model.summary(), file=sys.stderr)

    '''
    save_model(): save as h5 and json
    '''

    def save_model(self, model_name):
        h5_save_path = 'Models/' + model_name + '.h5'
        self.model.save_weights(h5_save_path)

        save_json_path = 'Models/' + model_name + '.json'
        with open(save_json_path, "w") as json_file:
            json_file.write(self.model.to_json())

    def train(self, x_train, y_train, x_val=None, y_val=None, n_epochs=50):
        acc = []
        loss = []
        val_acc = []
        val_loss = []

        if x_val is None or y_val is None:
            x_val, y_val = x_train, y_train
        for i in range(n_epochs):

            p = np.random.permutation(len(x_train))
            x_train = x_train[p]
            y_train = y_train[p]

            history = self.model.fit(x_train, y_train, batch_size=32, epochs=1)

            acc.append(history.history['acc'])
            loss.append(history.history['loss'])

            val_loss_single, val_acc_single = self.model.evaluate(x_val, y_val)
            val_acc.append(val_acc_single)
            val_loss.append(val_loss_single)

        plotCurve(acc, val_acc, 'LSTM Accuracy', 'acc')
        plotCurve(loss, val_loss, 'LSTM Loss', 'loss')
        self.trained = True

    def predict(self, sample):

        if not self.trained:
            sys.stderr.write("No Model.")
            sys.exit(-1)

        return np.argmax(self.model.predict(sample), axis=1)

    def make_model(self):
        raise NotImplementedError()
Ejemplo n.º 13
0
class LSTM_NER():
    def __init__(self):
        self.MAX_SEQUENCE_LENGTH = 2000
        self.EMBEDDING_DIM = 300
        self.MAX_NB_WORDS = 20000
        pass

    def build_tensor(self,
                     sequences,
                     numrecs,
                     word2index,
                     maxlen,
                     makecategorical=False,
                     num_classes=0,
                     is_label=False):
        data = np.empty((numrecs, ), dtype=list)
        label_index = {'O': 0}
        label_set = [
            "B-geo", "B-gpe", "B-per", "I-geo", "B-org", "I-org", "B-tim",
            "B-art", "I-art", "I-per", "I-gpe", "I-tim", "B-nat", "B-eve",
            "I-eve", "I-nat"
        ]
        for lbl in label_set:
            label_index[lbl] = len(label_index)

        lb = LabelBinarizer()
        lb.fit(list(label_index.values()))
        i = 0
        plabels = []
        for sent in tqdm.tqdm(sequences, desc='Building tensor'):
            wids = []
            pl = []
            for word, label in sent:
                if is_label == False:
                    # wids.append(word2index[word])
                    if word in word2index:
                        wids.append(word2index[word])
                    # print(word2index[word])
                    else:
                        wids.append(word2index['the'])
                else:
                    pl.append(label_index[label])
            plabels.append(pl)
            if not is_label:
                data[i] = wids
                #print(data[i])
            i += 1
        # if makecategorical and is_label:
        #     pdata = sequence.pa100d_sequences(data,maxlen=maxlen)
        #     return pdata
        if is_label:
            plabels = sequence.pad_sequences(plabels, maxlen=maxlen)
            print(plabels.shape)
            pdata = np.array([lb.transform(l) for l in plabels])
        else:
            pdata = sequence.pad_sequences(data, maxlen=maxlen)
        return pdata
        #return data

    def createModel(self, text):
        self.embeddings_index = {}
        f = open(os.path.join(GLOVE_DIR, 'glove.6B.300d.txt'))
        for line in f:
            values = line.split()
            word = values[0]
            coefs = np.asarray(values[1:], dtype='float32')
            self.embeddings_index[word] = coefs
        f.close()

        print('Found %s word vectors.' % len(self.embeddings_index))
        tokenizer = Tokenizer(num_words=self.MAX_NB_WORDS, lower=False)
        tokenizer.fit_on_texts(text)
        #sequences = tokenizer.texts_to_sequences(text)

        self.word_index = tokenizer.word_index

        self.embedding_matrix = np.zeros(
            (len(self.word_index) + 1, self.EMBEDDING_DIM))
        print(self.embedding_matrix.shape)
        for word, i in self.word_index.items():
            embedding_vector = self.embeddings_index.get(word)
            if embedding_vector is not None:
                # words not found in embedding index will be all-zeros.
                self.embedding_matrix[i] = embedding_vector
        #print(word_index)

        self.embedding_layer = Embedding(len(self.word_index) + 1,
                                         self.EMBEDDING_DIM,
                                         weights=[self.embedding_matrix],
                                         input_length=70,
                                         trainable=False)
        self.model = Sequential()
        self.model.add(self.embedding_layer)
        self.model.add(
            Bidirectional(
                LSTM(500,
                     dropout=0.2,
                     recurrent_dropout=0.4,
                     return_sequences=True))
        )  #{'sum', 'mul', 'concat', 'ave', None}
        # self.model.add(TimeDistributed(Bidirectional(LSTM(60, dropout=0.2, recurrent_dropout=0.5, return_sequences=True))))
        #self.model.add(TimeDistributed(Dense(50, activation='relu')))
        self.model.add(TimeDistributed(Dense(
            17,
            activation='softmax')))  # a dense layer as suggested by neuralNer
        #crf = CRF(17, sparse_target=True)
        #self.model.add(crf)
        #self.model.compile(loss=crf_loss, optimizer='adam', metrics=[crf_viterbi_accuracy])
        self.model.compile(loss="categorical_crossentropy",
                           optimizer='rmsprop',
                           metrics=['accuracy'])
        self.model.summary()
        pass

    def load_GLoVe_embeddings(self, GLOVE_DIR, text):
        pass

    def train(self):
        self.model.fit(self.X_train,
                       self.Y_train,
                       epochs=1,
                       validation_split=0.1,
                       batch_size=16)
        pass

    def test_model(self):
        Y_pred = self.model.predict(self.X_test)
        print(Y_pred)
        from sklearn import metrics
        Y_testing = []
        labels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
        for i in range(0, len(self.Y_test)):
            for j in range(0, len(self.Y_test[i])):
                for k in range(0, len(self.Y_test[i][j])):
                    if self.Y_test[i][j][k] == 1:
                        Y_testing.append(k)

        Y_pred_F = []

        for i in range(0, len(Y_pred)):
            for j in range(0, len(Y_pred[i])):
                max_k = 0
                max_k_val = 0
                for k in range(0, len(Y_pred[i][j])):
                    if Y_pred[i][j][k] > max_k_val:
                        max_k_val = Y_pred[i][j][k]
                        max_k = k
                Y_pred_F.append(max_k)

        Y_test_F = []
        for i in range(0, len(self.Y_test)):
            for j in range(0, len(self.Y_test[i])):
                max_k = 0
                max_k_val = 0
                for k in range(0, len(self.Y_test[i][j])):
                    if self.Y_test[i][j][k] > max_k_val:
                        max_k_val = self.Y_test[i][j][k]
                        max_k = k
                Y_test_F.append(max_k)

        print(metrics.classification_report(Y_test_F, Y_pred_F, labels))

    def save_mode(self):
        self.model.save_weights("../Models/" + "model.h5")
        model_json = self.model.to_json()
        with open("../Models/" + "model.json", "w") as json_file:
            json_file.write(model_json)
        # serialize weights to HDF5
        self.model.save_weights("../Models/" + "model.h5")
        print("Saved model to disk")
        pass
Ejemplo n.º 14
0
    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/n_samples_0,
                1: 1.0/n_samples_1,
                2: 1.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
#################################################################################################################        
        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 = True
        callbacks = []
        if tensorboard_active:
            callbacks.append(TensorBoard(
                log_dir=self.putmodel + "Tensor_board_data",
                histogram_freq=0,
                write_graph=True,
                write_images=True))
    
        #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])
#        tensorboard = TensorBoard(log_dir=self.putmodel + "Tensor_board_data", 
#                                  write_graph=True, write_images=True, histogram_freq=1)
        #tensorboard = TensorBoard(log_dir="/Users/yoelvisorozco/Library/Mobile Documents/com~apple~CloudDocs/TestTF/yoe",
        #                          write_graph=True, write_images=True, histogram_freq=0)
        
        model.fit(x=data, 
                  y=clas,
                  batch_size=self.batch_size, epochs=10, verbose=1, 
                  callbacks = callbacks,
                  class_weight = class_weight)
#                  callbacks=[tensorboard])
        
#####################################################################################################################
        
        # 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])
        
        y_pred = loaded_model.predict_proba(data_test)
        
        loaded_model.reset_states()
        score_train = model.evaluate(data, clas)
        model.reset_states()
        score_train_loaded = loaded_model.evaluate(data, clas)
        loaded_model.reset_states()
        score_test = model.evaluate(data_test, clas_test)
        model.reset_states()
        score_test_loaded = loaded_model.evaluate(data_test, clas_test)
        loaded_model.reset_states()

        
        pred_T = 0
        pred_F = 0        
        for i in range(len(y_pred)):
            if np.argmax(y_pred[i]) == 1 and self.y_test[i] == 1:
                pred_T += 1
#                print(y_pred[i])
            if np.argmax(y_pred[i]) == 1 and self.y_test[i] == 0:
                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)

#        self.print_to_disk(self.putmodel + "Tune.txt", 
#                      "Skip_" + str(self.skip) +
#                      " cConv_" + str(self.nConv) + 
#                      " nLSTM_" + str(self.nLSTM) +
#                      " learning_" + str(self.learning) +
#                      " batch_size_" + str(self.batch_size) +
#                      " conv_nodes_" + str(self.conv_nodes) +
#                      " lstm_nodes_" + str(self.lstm_nodes) +
#                      " --> loss_train: " + str(score_train[0]) + " acc_train: " + str(score_train[1]) + 
#                      "  loss_test: " + str(score_test[0]) + " acc_test: " + str(score_test[1]) + "\n") 
        
        print('Train loss:', score_train[0])
        print('Train accuracy:', score_train[1])
        print('Train loss loaded:', score_train_loaded[0])
        print('Train accuracy loaded:', score_train_loaded[1])

        print('Test loss:', score_test[0])
        print('Test accuracy:', score_test[1])
        print('Test loss loaded:', score_test_loaded[0])
        print('Test accuracy loaded:', score_test_loaded[1])
Ejemplo n.º 15
0
def NewModel():
     #Array of classes
     #(0,1,2,3,4,5,6,7,8,9,10)
     Y_labels = []
     #random seed

     #collect data
     #load into python
     #pre-process
     #names -> dictionary
     #value -> numbers
     # Pre processing done in GetDeck

     #X_train = X_train[:250]
     print(X_train.shape)
     #print(X_train)
     #match decks to their training data speed e.g.house party - 5 speed
          #matched via dictionary labels
     #pad dataset (should be fine since all 60 cards
     #each add adds a layer (just doing Dense because it's like my brain haha)
     #X_trainShape = numpy.reshape(X_train,(60,8))
     print('done')
     kfold = KFold(n_splits=num_folds, shuffle=True)
     fold_no = 1
     for train, test in kfold.split(X_train,Y_train):
          model = Sequential()
          model.add(Dense(300,input_dim=(60*522),name="Input_Layer",activation="sigmoid"))
          model.add(Dense(250,name="Hidden"))
          model.add(Dense(180,name="Hidden2"))
          model.add(Dense(100,name="Hidden3", activity_regularizer=regularizers.l1(0.001)))
          model.add(Dense(2,name="Output",activation="softmax"))
          model.summary()
          #to_cat serialises classification ( e.g. "on" values)
          #sequential model
          print('starting stuff')
          #model.add function
          #train_test_split to do training sets test/train ratio of 10-20/80-90
          print(X_train.shape)
          model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=["accuracy"])
          model.fit(X_train[train],Y_train[train],epochs=100,batch_size=150,verbose=2)
          scores = model.evaluate(X_train[test],Y_train[test],batch_size=150,verbose=0)
          print("Accuracy: %.2f%%" % scores[1]*100,flush=True)
          fold_no = fold_no + 1
          models.append(model)
          mScores.append(scores[1])
     #model.compile
     #loss e.g. rms
     #optimiser
     #metrics
     #model.fit (verbose = 2) for less warnings
     bestIndex = 0
     currIndex = 0
     for s in mScores:
          if mScores[currIndex] > mScores[bestIndex]:
               bestIndex = currIndex
          currIndex += 1

     model = models[bestIndex]
     model.fit(X_train,Y_train,epochs=50,batch_size=128,verbose=2)
     scores = model.evaluate(X_train,Y_train,batch_size=128,verbose=0)
     print("Accuracy: %.2f%%" % scores[1]*100,flush=True)   
     #eval model
     #model.evaluate
     #xtest, ytest, batch size, verbose 2
     #print them out
     print(mScores)
     print(mScores[bestIndex])
     print(scores[1])
     #save model somewhere model = JSON h5 = weights
     model_json = model.to_json()
     with open("model.json",'w') as json_file:
          json_file.write(model_json)
     model.save_weights('model.h5')
     print('SAVED MODEL')
          batch_size=batch_size,
          epochs=epochs,
          validation_data=(x_test, y_test))

# evaluating our model on the test sets
score, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)

# serialize model to JSON
model_json = model.to_json()
with open("one_kernel_CNN_LSTM_model.json", "w") as json_file:
    json_file.write(model_json)

# serialize weights to HDF5
model.save_weights("one_kernel_CNN_LSTM_weights.h5")
print("Saved model to disk")

# creating the prediction on test set csv file
keras_prediction(
    model_path="one_kernel_CNN_LSTM_model.json",
    weights_path="one_kernel_CNN_LSTM_weights.h5",
    ids_test_path="../../data/our_trained_wordvectors/ids_test_sg_6.npy",
    csv_file_name="one_kernel_cnn_lstm_prediction.csv")

# From here, we save our metrics results for the comparison with plots
val_acc_epochs = history.epocs_val_acc
np.save("val_acc_one_kernel_CNN_LSTM.npy", val_acc_epochs)

val_loss_epochs = history.epocs_val_loss
np.save("val_loss_one_kernel_CNN_LSTM.npy", val_loss_epochs)
Ejemplo n.º 17
0
class ANN96_1:
    # global constant
    xcols = 6  # last_week_t, last_week_t-1, yesterday_t, yesterday_t-1
    norm1 = [0, 1, 2, 3]  # list of load normalization

    def __init__(self):  # construction function
        # build model
        self.model = Sequential()
        self.model.add(
            Dense(8, input_shape=(ANN96_1.xcols, ), activation='relu'))
        self.model.add(Dense(1, activation=None))
        self.model.summary()
        # loss = mean squared error
        sgd = optimizers.SGD(lr=0.05)
        self.model.compile(optimizer=sgd, loss='mse', metrics=['mse'])
        # load data
        self.load = np.load('load.npy')
        # and is_weekend_one_hot (1:weekday, 0:weekend) for yesterday, today
        self.loadmax = self.load.max()  # max load ever
        load0 = self.load[self.load > 0]  # delete the 0s
        self.loadmin = load0.min()  # min load ever (not 0)
        self.nepoch = 100  # default training epochs

#    def ANNmodel(self):     # ANN model structure for all 96 classifiers
#         # 2 layers dense network
#        self.model = Sequential()
#        self.model.add(Dense(8, input_shape=(ANN96.xcols,), activation='relu'))
#        self.model.add(Dense(1, activation=None))
#        self.model.summary()
#        # loss = mean squared error
#        sgd = optimizers.SGD(lr=0.5)
#        self.model.compile(optimizer=sgd, loss='mse', metrics=['mse'])

    def setEpochs(self, Nepoch):  # set training epochs
        self.nepoch = Nepoch

#    def load_data(self):
#        self.load = np.load('load.npy')
#        # and is_weekend_one_hot (1:weekday, 0:weekend) for yesterday, today
#        self.loadmax = self.load.max()                  # max load ever
#        load0 = self.load[self.load>0]                  # delete the 0s
#        self.loadmin = load0.min()                      # min load ever (not 0)

    def train(self, n):  # use 0~n-1 date for training
        start_time = time.time()
        for j in range(96):
            x = np.zeros((1, ANN96_1.xcols), dtype=np.float)  # train data
            y = list()  # train label
            index = list(
            )  # original date index, for Mon/Tues recognition in sample_weight
            if j == 0:
                # prepare data
                for i in range(8, n - 1):
                    xt = np.zeros((1, ANN96_1.xcols), dtype=np.float)
                    if self.load[i-7, j]!=0 and self.load[i-8, 95]!=0 and \
                    self.load[i-1, j]!=0 and self.load[i-2, 95]!=0 \
                    and self.load[i, j]!=0:
                        xt[0, 0] = self.load[i - 7, j]
                        xt[0, 1] = self.load[i - 8, 95]
                        xt[0, 2] = self.load[i - 1, j]
                        xt[0, 3] = self.load[i - 2, 95]
                        if (i - 1) % 7 == 3 or (i - 1) % 7 == 4:
                            xt[0, 4] = 0
                        else:
                            xt[0, 4] = 1
                        if i % 7 == 3 or i % 5 == 4:
                            xt[0, 5] = 0
                        else:
                            xt[0, 5] = 1
                        if x[0, 0] == 0:
                            x = xt
                        else:
                            x = np.concatenate((x, xt), axis=0)
                        y.append(self.load[i, j])
                        index.append(i)
            else:
                for i in range(7, n - 1):
                    xt = np.zeros((1, ANN96_1.xcols),
                                  dtype=np.float)  # temporary variable
                    if self.load[i-7, j]!=0 and self.load[i-7, j-1]!=0 \
                    and self.load[i-1, j]!=0 and self.load[i-1, j-1]!=0 \
                    and self.load[i, j]!=0:
                        xt[0, 0] = self.load[i - 7, j]
                        xt[0, 1] = self.load[i - 7, j - 1]
                        xt[0, 2] = self.load[i - 1, j]
                        xt[0, 3] = self.load[i - 1, j - 1]
                        if (i - 1) % 7 == 3 or (i - 1) % 7 == 4:
                            xt[0, 4] = 0
                        else:
                            xt[0, 4] = 1
                        if i % 7 == 3 or i % 5 == 4:
                            xt[0, 5] = 0
                        else:
                            xt[0, 5] = 1
                        if x[0, 0] == 0:
                            x = xt
                        else:
                            x = np.concatenate((x, xt), axis=0)
                        y.append(self.load[i, j])
                        index.append(i)
            # training
            # normalization
            nsample = x.shape[0]  # number of training samples
            for i in range(nsample):
                for k in ANN96_1.norm1:
                    x[i, k] = (x[i, k] - self.loadmin)\
                    / (self.loadmax - self.loadmin)
            y = (y - self.loadmin) / (self.loadmax - self.loadmin)
            # sample weight:
            # 1. the closer the date, the larger the weight in loss function
            # 2. the weight of Monday and Tuesday (days to be predicted) sample is amplified
            sw = np.zeros((nsample, ))
            for i in range(nsample):
                sw[i] = 0.998**(nsample - i)
                if index[i] % 7 == 5 or index[i] % 7 == 6:  # Mon or Tues
                    sw[i] = sw[i] * 2
                elif index[i] % 7 == 3 or index[i] % 7 == 4:  # delete weekend
                    sw[i] = 0
            # train
            if j == 0:  # j=0, nepoch *= 10
                history = self.model.fit\
            (x, y, batch_size=32, epochs=10*self.nepoch, \
             sample_weight=sw, verbose=0)
            else:
                history = self.model.fit\
            (x, y, batch_size=32, epochs=self.nepoch,sample_weight=sw, verbose=1)
            # verbose=0, not to show training details
            # save model
            self.model.save_weights(
                os.path.join('new_weights', str(j)) + '.h5')
            # evaluate model by mse
            mse = history.history['mean_squared_error']
            last_mse = mse[len(mse) - 1]
            print(str(j) + '/96: ' + str(last_mse))
        end_time = time.time()
        use_time = np.ceil((end_time - start_time) / 60)
        print(str(use_time) + ' min used in trainng process')

    def predict(self, n):  # predict the nth date
        neural = np.zeros((96, ))
        xp = np.zeros((1, ANN96_1.xcols), dtype=np.float)  # predict data
        prediction = 0
        for j in range(96):
            if j == 0:
                xp[0, 0] = self.load[n - 7, j]
                xp[0, 1] = self.load[n - 8, 95]
                xp[0, 2] = self.load[n - 1, j]
                xp[0, 3] = self.load[n - 2, 95]
                if (n - 1) % 7 == 3 or (n - 1) % 7 == 4:
                    xp[0, 4] = 0
                else:
                    xp[0, 4] = 1
                if n % 7 == 3 or n % 7 == 4:
                    xp[0, 5] = 0
                else:
                    xp[0, 5] = 1
                for k in ANN96_1.norm1:
                    xp[0, k] = (xp[0, k] - self.loadmin)\
                    / (self.loadmax - self.loadmin)
            else:
                xp[0, 0] = self.load[n - 7, j]
                xp[0, 1] = self.load[n - 7, j - 1]
                xp[0, 2] = self.load[n - 1, j]
                xp[0, 3] = self.load[n - 1, j - 1]
                if (n - 1) % 7 == 3 or (n - 1) % 7 == 4:
                    xp[0, 4] = 0
                else:
                    xp[0, 4] = 1
                if n % 7 == 3 or n % 7 == 4:
                    xp[0, 5] = 0
                else:
                    xp[0, 5] = 1
                for k in ANN96_1.norm1:
                    xp[0, k] = (xp[0, k] - self.loadmin)\
                    / (self.loadmax - self.loadmin)
            # predict
            self.model.load_weights(os.path.join('new_weights',
                                                 str(j) + '.h5'))
            prediction = self.model.predict(xp)
            prediction = \
            (self.loadmax - self.loadmin) * prediction + self.loadmin
            neural[j] = prediction
        return neural
Ejemplo n.º 18
0
else:
    encoder.add(autoencoder.layers[0])
    for i in range(1, n_hidden_layers + 1):
        encoder.add(autoencoder.layers[2 * i])

### Create and save low-dimensional representations of training data ###
lowd_train = encoder.predict(x_train_scaled)
np.save(path + "LowD_Train.npy", lowd_train)

### Save autoencoder ###
model_json_1 = autoencoder.to_json()
with open(path + "Autoencoder.json", "w") as json_file:
    json_file.write(model_json_1)

# serialize weights to HDF5
autoencoder.save_weights(path + "Autoencoder.h5")

### Save encoder ###
model_json_2 = encoder.to_json()
with open(path + "Encoder.json", "w") as json_file:
    json_file.write(model_json_2)

# serialize weights to HDF5
encoder.save_weights(path + "Encoder.h5")

### Plot loss ###
plt.figure(1)
plt.plot(autoencoder.history.history['val_loss'])
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.savefig(path + "Autoencoder_Train.png")
Ejemplo n.º 19
0
    testLabels = tempMatrix[:, 0]
    testData = testData.reshape(testData.shape[0], testData.shape[1], 1)

    scores = myModel.evaluate(x=testData, y=testLabels)
    print(scores)
    print("The accuracy for file {0} is {1}".format(f, scores[1] * 100))
'''
# test the model
NPYfiles = os.listdir(testDIR)
for f in NPYfiles:
    tempFile = testDIR + '/' + f
    tempMatrix = np.load(tempFile)
    print("The shape of {0} is {1}.".format(f, tempMatrix.shape))
    x_test = tempMatrix[:,1:]
    y_test = tempMatrix[:,0]
    y_test_binary = keras.utils.to_categorical(y_test, num_classes)
    print("y_test_binary is {}.".format(y_test_binary))
    x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], 1)
    print("The shape of x_test is {}.".format(x_test.shape))

    myPrediction = myModel.predict_classes(x_test)
    print(myPrediction)
'''
# Save the trained model
myModel_json = myModel.to_json()
with open(modelDIR + '/' + modelFILE, 'w') as json_file:
    json_file.write(myModel_json)
myModel.save_weights(modelDIR + '/' + weightFILE)
print("The trained model has been saved.")

print("Time point 3 is " + str(time.time() - startTime))
Ejemplo n.º 20
0
from keras import Sequential
from keras.layers import Dense, Dropout
from sklearn.metrics import confusion_matrix, accuracy_score

previsores = pd.read_csv('entradas-breast.csv')
classe = pd.read_csv('saidas-breast.csv')

classificador = Sequential()
classificador.add(
    Dense(units=8,
          activation='relu',
          kernel_initializer='normal',
          input_dim=30))
classificador.add(Dropout(0.2))
classificador.add(
    Dense(units=8, activation='relu', kernel_initializer='normal'))
classificador.add(Dropout(0.2))
classificador.add(Dense(units=1, activation='sigmoid'))
classificador.compile(optimizer='adam', loss='binary_crossentropy', metrics = \
                      ['binary_accuracy'])

classificador.fit(previsores, classe, batch_size=10, epochs=100)

classificador_json = classificador.to_json()
with open('classificador_breast.json', 'w') as json_file:
    json_file.write(classificador_json)

classificador.save_weights('classificador_breast.h5')
Ejemplo n.º 21
0
class DNN_Model(Common_Model):
    '''
    __init__(): 初始化神经网络

    输入:
        input_shape: 特征维度
        num_classes(int): 标签种类数量
        lr(float): 学习率
    '''
    def __init__(self, input_shape, num_classes, lr, **params):
        super(DNN_Model, self).__init__()

        self.input_shape = input_shape
        
        self.model = Sequential()
        self.make_model(**params)
        self.model.add(Dense(num_classes, activation = 'softmax'))

        optimzer = keras.optimizers.Adam(lr = lr)
        self.model.compile(loss = 'categorical_crossentropy', optimizer = optimzer, metrics = ['accuracy'])
       
        print(self.model.summary(), file = sys.stderr)


    '''
    save_model(): 将模型存储在 config.checkpoint_path 路径下

    输入:
        config(Class)
    '''
    def save_model(self, config):
        h5_save_path = os.path.join(config.checkpoint_path, config.checkpoint_name + '.h5')
        self.model.save_weights(h5_save_path)

        save_json_path = os.path.join(config.checkpoint_path, config.checkpoint_name + '.json')
        with open(save_json_path, "w") as json_file:
            json_file.write(self.model.to_json())


    def reshape_input(self):
        NotImplementedError()


    '''
    train(): 在给定训练集上训练模型

    输入:
        x_train(numpy.ndarray): 训练集样本
        y_train(numpy.ndarray): 训练集标签
        x_val(numpy.ndarray): 测试集样本
        y_val(numpy.ndarray): 测试集标签
        batch_size(int): 批大小
        n_epochs(int): epoch 数
    '''
    def train(self, x_train, y_train, x_val = None, y_val = None, 
                batch_size = 32, n_epochs = 50):
 
        if x_val is None or y_val is None:
            x_val, y_val = x_train, y_train
        
        x_train, x_val = self.reshape_input(x_train), self.reshape_input(x_val)

        history = self.model.fit(
            x_train, y_train, 
            batch_size = batch_size, 
            epochs = n_epochs,
            shuffle = True, # 每个 epoch 开始前随机排列训练数据
            validation_data = (x_val, y_val)
        )

        # 训练集上的损失和准确率
        acc = history.history['acc']
        loss = history.history['loss']
        # 验证集上的损失和准确率
        val_acc = history.history['val_acc']
        val_loss = history.history['val_loss']

        plotCurve(acc, val_acc, 'Accuracy', 'acc')
        plotCurve(loss, val_loss, 'Loss', 'loss')
        
        self.trained = True


    '''
    predict(): 识别音频的情感

    输入:
        samples: 需要识别的音频特征

    输出:
        list: 识别结果
    '''
    def predict(self, sample):
        sample = self.reshape_input(sample)
        
        # 没有训练和加载过模型
        if not self.trained:
            sys.stderr.write("No Model.")
            sys.exit(-1)

        return np.argmax(self.model.predict(sample), axis=1)


    def make_model(self):
        raise NotImplementedError()
Ejemplo n.º 22
0
class LSTMPredictor:
    LOOKBACK_SEQUENCE_LENGTH = 16
    PREDICTED_SEQUENCE_LENGTH = 4

    EPOCHS = 100
    MIN_EPOCHS = 1
    MIN_DELTA = 0.002
    BATCH_SIZE = 100
    VALIDATION_SPLIT = 0.05

    def __init__(self, models_dir: str = '', tensorboard_dir: str = '') -> None:
        self._models_dir = models_dir or os.path.join(PROJECT_PATH, 'models')
        self._tensorboard_dir = tensorboard_dir

        self._model = Sequential([
            LSTM(
                input_shape=(self.LOOKBACK_SEQUENCE_LENGTH, 1),
                units=32,
                return_sequences=True,
            ),
            Activation('relu'),
            LSTM(
                units=32,
            ),
            Activation('relu'),
            Dense(units=self.PREDICTED_SEQUENCE_LENGTH),
        ])

    def train(self, signal_name: str, normal_data: np.ndarray) -> History:
        print(f'Обучение LSTM-предиктора для сигнала "{signal_name}"...')

        x_train, y_train = self._create_subsequences(self._preprocess(normal_data))
        x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))  # samples, sample_len, features

        self._model.compile(optimizer='adam', loss='mse')

        callbacks = [
                EarlyStopping('val_loss', patience=self.MIN_EPOCHS, min_delta=self.MIN_DELTA),
        ]
        if self._tensorboard_dir:
            os.makedirs(self._tensorboard_dir, exist_ok=True)
            callbacks.append(
                TensorBoard(
                    log_dir=self._get_tensorboard_logs_dir(signal_name),
                    batch_size=self.BATCH_SIZE,
                    histogram_freq=0,
                    write_graph=True,
                    write_grads=True,
                    write_images=True,
                ))

        history = self._model.fit(
            x_train, y_train,
            batch_size=self.BATCH_SIZE,
            epochs=self.EPOCHS,
            validation_split=self.VALIDATION_SPLIT,
            shuffle=True,
            callbacks=callbacks,
        )

        models_path = self._get_model_path(signal_name)
        os.makedirs(os.path.dirname(models_path), exist_ok=True)
        self._model.save_weights(models_path)
        print(f'Модель сохранена в "{models_path}"')

        return history

    def analyze(self, signal: Signals) -> PredictorResult:
        assert len(signal) == 1, 'Allowed to analyze only 1 singal'

        signal_name, data = next(iter(signal.items()))

        model_path = self._get_model_path(signal_name)
        if not os.path.exists(model_path):
            raise FileNotFoundError(f'Model for {signal_name} is not found. Please train')
        self._model.load_weights(model_path)

        data = self._preprocess(data)
        subsequences, next_values = self._create_subsequences(data)
        subsequences = np.reshape(subsequences, (subsequences.shape[0], subsequences.shape[1], 1))

        predicted_next_values = self._model.predict(subsequences, batch_size=self.BATCH_SIZE)
        errors = next_values - predicted_next_values
        error_mean, error_cov = calculate_covariance_matrix(errors)

        return PredictorResult(
            signal=signal_name,
            data=data,
            predicted_data=predicted_next_values,
            mahalanobis_distance=np.array([
                mahalanobis_distance(e_vec, error_mean, error_cov)
                for e_vec in errors
            ])
        )

    def plot_model(self, img_path: str, show_shapes=True, show_layer_names=True) -> None:
        plot_model(
            self._model,
            to_file=img_path,
            show_shapes=show_shapes,
            show_layer_names=show_layer_names,
        )

    def _get_model_path(self, signal_name: str) -> str:
        return os.path.join(self._models_dir, 'predictor', f'{signal_name}.h5')

    def _get_tensorboard_logs_dir(self, signal_name: str) -> str:
        return os.path.join(self._tensorboard_dir, 'predictor', signal_name)

    @staticmethod
    def _preprocess(data: np.ndarray) -> np.ndarray:
        return z_normalization(fill_zeros_with_previous(data))

    @staticmethod
    def _create_subsequences(
            series: np.ndarray,
            previous_count: int = LOOKBACK_SEQUENCE_LENGTH,
            next_count: int = PREDICTED_SEQUENCE_LENGTH,
    ) -> Tuple[np.ndarray, np.ndarray]:
        assert previous_count > next_count

        previous_values, next_values = [], []
        for i in range(previous_count, len(series) - next_count):
            previous_values.append(series[i - previous_count:i])
            next_values.append(series[i:i + next_count])

        return np.array(previous_values), np.array(next_values)
def main():
    first_stage_network_depths = ((('Dense', {
        'units': 128,
        'activation': 'relu'
    }), ('Dropout', {
        'rate': 0.4
    }), ('Dense', {
        'units': 64,
        'activation': 'relu'
    }), ('Dense', {
        'units': 1,
        'activation': 'sigmoid'
    })), )

    first_stage_data = BacteriaAndVirusKMers(
        fp=
        '/home/jklynch/host/project/viral-learning/data/perm_training_testing.h5',
        training_sample_count=100000,
        development_sample_count=1000,
        half_batch_size=50)

    first_stage_model_name, first_stage_model = build_model(
        model=Sequential(),
        input_dim=first_stage_data.get_input_dim(),
        layers=first_stage_network_depths[0])

    first_stage_model_name = 'first_stage_' + first_stage_model_name

    training_metrics_df, dev_metrics_df = train_and_evaluate(
        model=first_stage_model,
        model_name=first_stage_model_name,
        training_epochs=5,
        the_data=first_stage_data)

    pprint(first_stage_model.get_config())

    # store the model
    with open(first_stage_model_name + '.json', 'wt') as model_json:
        model_json.write(first_stage_model.to_json())
    first_stage_model.save_weights(filepath=first_stage_model_name + '.h5',
                                   overwrite=True)

    second_stage_model = Sequential()
    second_stage_model.add(first_stage_model.get_layer(index=0))
    second_stage_model.add(first_stage_model.get_layer(index=1))
    second_stage_model.add(first_stage_model.get_layer(index=2))
    second_stage_layers = (
        (
            #('Dense', {'units': 64, 'activation': 'relu'}),
            ('Dense', {
                'units': 1,
                'activation': 'sigmoid'
            }), ), )
    second_stage_model_name, second_stage_model = build_model(
        model=second_stage_model, layers=second_stage_layers[0])

    second_stage_model_name = 'second_stage_' + second_stage_model_name

    second_stage_data = BacteriaAndVirusGenomeKMers(
        fp=
        '/home/jklynch/host/project/viral-learning/data/riveal_refseq_prok_phage_500pb_kmers8.h5',
        pb=500,
        k=8,
        training_sample_count=100000,
        development_sample_count=1000,
        half_batch_size=50)

    pprint(second_stage_model.get_config())

    genomic_training_metrics_df, genomic_dev_metrics_df = train_and_evaluate(
        model=second_stage_model,
        model_name=second_stage_model_name,
        training_epochs=5,
        the_data=second_stage_data)

    # store the model
    with open(second_stage_model_name + '.json', 'wt') as model_json:
        model_json.write(second_stage_model.to_json())
    second_stage_model.save_weights(filepath=second_stage_model_name + '.h5',
                                    overwrite=True)
Ejemplo n.º 24
0
class LSTM():
    def __init__(self, input_shape=(None, LATENT_DIM + NB_ACTIONS)):
        self.input_shape = input_shape
        self._build()

    def _build(self):
        self.model = Sequential()
        # Only one lstm layer
        # The output needs to be the same size as the LATENT_DIM because the LSTM predict the future latent vector
        self.model.add(
            layers.LSTM(units=LATENT_DIM,
                        input_shape=self.input_shape,
                        activation='sigmoid',
                        kernel_initializer='random_normal'))
        self.model.add(BatchNormalization())
        self.model.compile(loss='mse', optimizer='adam')

    def train(self, X_train, Y_train, X_test, Y_test, epochs=200):

        print(X_train.shape)
        print(Y_train.shape)
        print(X_test.shape)
        print(Y_test.shape)
        self.model.fit(x=X_train,
                       y=Y_train,
                       epochs=epochs,
                       validation_data=(X_test, Y_test),
                       batch_size=SEQ_LENGTH,
                       verbose=2,
                       shuffle=False)

    def save(self, path):
        self.model.save(path)

    def save_weights(self, file_path):
        self.model.save_weights(filepath=file_path)

    def load_weights(self, file_path):
        self.model.load_weights(filepath=file_path)

    '''
	We load the trained model of the LSTM in order to play inside it.
	Its output is connected to its input so it generates continuously new latent vectors.
	The actions of the player are also given to the input layer.
	'''

    def play_in_dream(self, start_image, decoder):
        latent_image = start_image
        while True:
            # Player's actions
            actions = [[0, 0, 0, 0]]
            # We check wich keys are pressed
            try:
                if keyboard.is_pressed('up'):
                    actions[0][Actions.JUMP] = 1
                    print('UP')
                if keyboard.is_pressed('left'):
                    actions[0][Actions.LEFT] = 1
                    print('LEFT')
                if keyboard.is_pressed('right'):
                    actions[0][Actions.RIGHT] = 1
                    print('RIGHT')
                if keyboard.is_pressed('down'):
                    actions[0][Actions.DOWN] = 1
                    print('DOWN')
                if keyboard.is_pressed('escape'):
                    print("exit")
                    sys.exit(1)
            except Exception as e:
                print(e)
                break

            # latent vector + actions are given to the input layer of the LSTM
            lstm_input = np.concatenate((latent_image, actions), axis=1)
            lstm_input = np.reshape(lstm_input,
                                    (1, 1, LATENT_DIM + NB_ACTIONS))
            # Futur latent vector is predicted
            latent_image = self.model.predict(lstm_input)
            # We pass the latent vector through the decoder to see the corresponding image
            reconstructed_image = decoder.predict(latent_image)
            reconstructed_image = reconstructed_image.reshape(IMG_SHAPE)
            plt.clf()
            plt.imshow(reconstructed_image, vmin=0, vmax=1)
            # Necessary to display something on screen
            plt.pause(0.0000001)
class CNNmodel3:
    def __init__(self, img_size=(256, 256), dump_path='dump/'):
        # Random parameters
        conv1_filters = np.random.randint(1, 65)
        conv2_filters = np.random.randint(1, 65)
        conv1_kernel = np.random.randint(2, 10)
        conv2_kernel = np.random.randint(2, 10)
        conv1_strides = np.random.randint(1, conv1_kernel / 2 + 1)
        conv2_strides = np.random.randint(1, conv2_kernel / 2 + 1)
        maxpool1_size = np.random.randint(2, 10)
        fc1_units = 2**np.random.randint(6, 11)
        fc2_units = 2**np.random.randint(6, 11)

        # Model architecture
        self.model = Sequential()
        self.model.add(
            Conv2D(filters=conv1_filters,
                   kernel_size=(conv1_kernel, conv1_kernel),
                   strides=(conv1_strides, conv1_strides),
                   activation='relu',
                   input_shape=(img_size[0], img_size[1], 3),
                   name='conv1'))
        self.model.add(
            MaxPooling2D(pool_size=(maxpool1_size, maxpool1_size),
                         strides=None,
                         name='maxpool1'))
        self.model.add(
            Conv2D(filters=conv2_filters,
                   kernel_size=(conv2_kernel, conv2_kernel),
                   strides=(conv2_strides, conv2_strides),
                   activation='relu',
                   name='conv2'))
        self.model.add(Flatten())
        self.model.add(Dense(units=fc1_units, activation='relu', name='fc1'))
        self.model.add(Dense(units=fc2_units, activation='relu', name='fc2'))
        self.model.add(Dense(units=8, activation='softmax', name='classif'))

        # Optimizer
        optimizer = Adam()

        # Compile
        self.model.compile(loss='categorical_crossentropy',
                           optimizer=optimizer,
                           metrics=['accuracy'])
        # Parameters
        self.born_time = time.strftime('%Y%m%d%H%M%S', time.gmtime())
        self.identifier = str(hash(str(self.model.get_config())))
        self.dump_path = os.path.join(
            dump_path,
            str(self.born_time) + '_' + self.identifier)
        self.input_img_size = img_size

        # Print
        if not os.path.exists(self.dump_path):
            os.makedirs(self.dump_path)
        self.model.summary()
        print('Current model: ' + self.identifier)
        plot_model(self.model,
                   show_shapes=True,
                   show_layer_names=True,
                   to_file=os.path.join(self.dump_path,
                                        self.identifier + '.png'))

    def _train_generator(self, path, batch_size):
        datagen = ImageDataGenerator(
            preprocessing_function=self._preprocess_input,
            rotation_range=0,
            width_shift_range=0.,
            height_shift_range=0.,
            shear_range=0.,
            zoom_range=0.,
            channel_shift_range=0.,
            fill_mode='reflect',
            cval=0.,
            horizontal_flip=False,
            vertical_flip=False)
        return datagen.flow_from_directory(path,
                                           target_size=self.input_img_size,
                                           batch_size=batch_size,
                                           class_mode='categorical')

    def _test_val_generator(self, path, batch_size):
        datagen = ImageDataGenerator(
            preprocessing_function=self._preprocess_input)
        return datagen.flow_from_directory(path,
                                           target_size=self.input_img_size,
                                           batch_size=batch_size,
                                           class_mode='categorical',
                                           shuffle=False)

    def fit_directory(self,
                      path,
                      batch_size,
                      epochs,
                      val_path=None,
                      save_weights=False):
        train_generator = self._train_generator(path, batch_size)
        if val_path is None:
            validation_generator = None
            validation_steps = None
        else:
            validation_generator = self._test_val_generator(
                val_path, batch_size)
            validation_steps = validation_generator.samples / batch_size

        history = self.model.fit_generator(
            train_generator,
            steps_per_epoch=train_generator.samples / batch_size,
            epochs=epochs,
            validation_data=validation_generator,
            validation_steps=validation_steps)
        utils.plot_history(history,
                           self.dump_path,
                           identifier='e' + str(epochs) + '_b' +
                           str(batch_size))
        with open(
                os.path.join(
                    self.dump_path, 'e' + str(epochs) + '_b' +
                    str(batch_size) + '_history.pklz'), 'wb') as f:
            cPickle.dump((history.epoch, history.history, history.params,
                          history.validation_data, self.model.get_config()), f,
                         cPickle.HIGHEST_PROTOCOL)
        if save_weights:
            self.model.save_weights(
                os.path.join(
                    self.dump_path, 'e' + str(epochs) + '_b' +
                    str(batch_size) + '_weights.h5'))
        return history

    def evaluate(self, path):
        test_generator = self._test_val_generator(path, batch_size=32)
        return self.model.evaluate_generator(test_generator)

    def _preprocess_input(self, x, dim_ordering='default'):
        if dim_ordering == 'default':
            dim_ordering = K.image_dim_ordering()
        assert dim_ordering in {'tf', 'th'}

        mean = [109.07621812, 115.45609435, 114.70990406]
        std = [56.91689916, 55.4694083, 59.14847488]
        if dim_ordering == 'th':
            # Zero-center by mean pixel
            x[0, :, :] -= mean[0]
            x[1, :, :] -= mean[1]
            x[2, :, :] -= mean[2]
            # Normalize by std
            x[0, :, :] /= std[0]
            x[1, :, :] /= std[1]
            x[2, :, :] /= std[2]
        else:
            # Zero-center by mean pixel
            x[:, :, 0] -= mean[0]
            x[:, :, 1] -= mean[1]
            x[:, :, 2] -= mean[2]
            # Normalize by std
            x[:, :, 0] /= std[0]
            x[:, :, 1] /= std[1]
            x[:, :, 2] /= std[2]
        return x
Ejemplo n.º 26
0
class Sentiment(object):
    def __init__(self, data_directory, model_directory, filename,
                 pickle_directory, output_directory, max_length, batch_size,
                 epochs):

        self.data_directory = data_directory
        self.model_directory = model_directory
        self.filename = filename
        self.pickle_directory = Path(pickle_directory)
        self.output_directory = output_directory
        self.max_length = max_length
        self.batch_size = batch_size
        self.epochs = epochs

    def load_data(self):

        logger.debug(
            f'Load the data {self.filename} from data directory {self.data_directory}'
        )
        data = pd.read_csv(self.data_directory / self.filename)
        indx = data[(data.stars != 5) & (data.stars != 1)].index
        data = data.drop(indx)
        self.data_sentiment = data.reset_index(drop=True)

    def data_prep(self):
        logger.debug(
            'Split the comments into sentences and obtain the vocabulary')
        all_words = []
        toxic_sentences = []
        attitude_list = []
        sentences_list = []

        for i in range(len(self.data_sentiment.index)):
            sentences = re.split(
                r'[.!]', self.data_sentiment['text'][i].replace('\n', ''))
            if self.data_sentiment['stars'][i] == 1:
                attitude = 0
            elif self.data_sentiment['stars'][i] == 5:
                attitude = 1
            for sentence in sentences:
                words = list(preprocessing.tokenize(sentence))
                if len(words) >= 3:
                    all_words += words
                    confid_score = sonar.ping(
                        text=sentence)['classes'][1]['confidence']
                    if confid_score > 0.8:
                        toxic_sentences.append(sentence)
                    attitude_list.append(attitude)
                    sentences_list.append(sentence)

        self.all_words = all_words
        self.toxic_sentences = toxic_sentences
        self.attitude_list = attitude_list
        self.sentences_list = sentences_list

    def words_encoder(self):
        logger.debug('Encoding the vocabulary')

        self.encoder = LabelEncoder()
        self.encoder.fit(self.all_words)
        label_encoder_dict = defaultdict(LabelEncoder)
        for key, encoder in label_encoder_dict.items():
            classes = np.array(self.encoder.classes_).tolist()
            bisect.insort_left(classes, 'UNK')
            self.encoder.classes_ = classes
        self.vocab_size = len(self.encoder.classes_)

    def encoder_transform(self):
        logger.debug('Transfer the words to the coders')

        X_sentiment = []
        for sentence in self.sentences_list:
            words = list(preprocessing.tokenize(sentence))
            if len(words) >= 3:
                try:
                    words = words[:self.max_length]
                except:
                    pass
                words_idx = np.array(self.encoder.transform(words))
                arr = np.full(self.max_length, 0)
                arr[:len(words)] = words_idx
                X_sentiment.append(arr)
        self.X_sentiment = np.array(X_sentiment)
        self.X_sentiment_label = np.array(self.attitude_list)

    def create_dict(self):
        self.encoder_dict = {
            'encoder': self.encoder,
            'X_sentiment': self.X_sentiment,
            'X_sentiment_label': self.X_sentiment_label,
            'toxic_sentences': self.toxic_sentences
        }

    def save_pickle(self):
        logger.debug('Save the pickle file')
        with open(self.pickle_directory / 'encoder_senti.pickle',
                  'wb') as handle:
            pickle.dump(self.encoder_dict,
                        handle,
                        protocol=pickle.HIGHEST_PROTOCOL)

    def load_pickle(self):
        logger.debug('Load the pickle file')
        with open(self.pickle_directory / 'encoder_senti.pickle',
                  'rb') as handle:
            x = pickle.load(handle)
        return x

    def train(self):
        logger.debug(
            f'Building the neural network with epochs as {self.epochs} and batch size as {self.batch_size}'
        )
        encoder_dict = self.load_pickle()
        encoder = encoder_dict['encoder']
        X_sentiment = encoder_dict['X_sentiment']
        X_sentiment_label = encoder_dict['X_sentiment_label']

        self.model_sentiment = Sequential()
        self.model_sentiment.add(Embedding(self.vocab_size, 100))
        self.model_sentiment.add(
            Bidirectional(LSTM(50, dropout=0.5, recurrent_dropout=0.5)))
        self.model_sentiment.add(Dense(1, activation='sigmoid'))
        self.model_sentiment.compile(optimizer=Adam(lr=1e-4),
                                     loss='binary_crossentropy',
                                     metrics=['accuracy', f1_score, auc])

        self.history_sentiment = self.model_sentiment.fit(
            X_sentiment,
            X_sentiment_label,
            validation_split=0.1,
            callbacks=[earlyStopping],
            epochs=self.epochs,
            batch_size=self.batch_size,
            class_weight='auto')

    def save_model(self):
        logger.debug(f'Save the model in {self.model_directory}')
        model_json = self.model_sentiment.to_json()
        with open(self.model_directory / 'sentiment_model.json',
                  'w') as json_file:
            json_file.write(model_json)
        self.model_sentiment.save_weights(self.model_directory /
                                          'sentiment_model.h5')

    def load_model(self):
        logger.debug(f'Load the model from {self.model_directory}')
        json_file = open(self.model_directory / 'sentiment_model.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
            self.loaded_model_sentiment = model_from_json(loaded_model_json)
            self.loaded_model_sentiment.load_weights(self.model_directory /
                                                     'sentiment_model.h5')

        return self.loaded_model_sentiment

    def metric_graph(self):
        history_dict = self.history_sentiment.history
        acc = history_dict['acc']
        val_acc = history_dict['val_acc']
        loss = history_dict['loss']
        val_loss = history_dict['val_loss']
        f1_score = history_dict['f1_score']
        val_f1_score = history_dict['val_f1_score']
        auc = history_dict['auc']
        val_auc = history_dict['val_auc']
        epochs = range(1, len(acc) + 1)

        train_set = ['acc', 'loss', 'f1_score', 'auc']
        val_set = ['val_acc', 'val_loss', 'val_f1_score', 'val_auc']

        for i in range(len(train_set)):
            logger.debug(f'Generate the figures for {train_set[i]}')
            fig = matplotlib.pyplot.figure()
            matplotlib.pyplot.title(f'Training and validation {train_set[i]}')
            matplotlib.pyplot.xlabel('epochs')
            matplotlib.pyplot.ylabel(f'{train_set[i]}')
            matplotlib.pyplot.plot(epochs,
                                   train_set[i],
                                   'r',
                                   label=f'{train_set[i]}')
            matplotlib.pyplot.plot(epochs,
                                   val_set[i],
                                   'b',
                                   label=f'{val_set[i]}')
            matplotlib.pyplot.legend()
            fig.savefig(self.output_directory /
                        f'sentiment_{train_set[i]}.png')
Ejemplo n.º 27
0
class DNN(Model):
    """
    This class is parent class for all Deep neural network models. Any class
    inheriting this class should implement `make_default_model` method which
    creates a model with a set of hyper parameters.
    """
    def __init__(self, input_shape, num_classes, **params):
        """
        Constructor to initialize the deep neural network model. Takes the input
        shape and number of classes and other parameters required for the
        abstract class `Model` as parameters.

        Args:
            input_shape (tuple): shape of the input
            num_classes (int): number of different classes ( labels ) in the data.
            **params: Additional parameters required by the underlying abstract
                class `Model`.

        """
        super(DNN, self).__init__(**params)
        self.input_shape = input_shape
        self.model = Sequential()
        self.make_default_model()
        self.model.add(Dense(num_classes, activation='softmax'))
        self.model.compile(loss='binary_crossentropy',
                           optimizer='adam',
                           metrics=['accuracy'])
        print(self.model.summary(), file=sys.stderr)
        self.save_path = self.save_path or self.name + '_best_model.h5'

    def load_model(self, to_load):
        """
        Load the model weights from the given path.

        Args:
            to_load (str): path to the saved model file in h5 format.

        """
        try:
            self.model.load_weights(to_load)
        except:
            sys.stderr.write("Invalid saved file provided")
            sys.exit(-1)

    def save_model(self):
        """
        Save the model weights to `save_path` provided while creating the model.
        """
        self.model.save_weights(self.save_path)

    def train(self, x_train, y_train, x_val=None, y_val=None, n_epochs=50):
        """
        Train the model on the given training data.


        Args:
            x_train (numpy.ndarray): samples of training data.
            y_train (numpy.ndarray): labels for training data.
            x_val (numpy.ndarray): Optional, samples in the validation data.
            y_val (numpy.ndarray): Optional, labels of the validation data.
            n_epochs (int): Number of epochs to be trained.

        """
        best_acc = 0
        if x_val is None or y_val is None:
            x_val, y_val = x_train, y_train
        for i in range(n_epochs):
            # Shuffle the data for each epoch in unison inspired
            # from https://stackoverflow.com/a/4602224
            p = np.random.permutation(len(x_train))
            x_train = x_train[p]
            y_train = y_train[p]
            self.model.fit(x_train, y_train, batch_size=32, epochs=1)
            loss, acc = self.model.evaluate(x_val, y_val)
            if acc > best_acc:
                best_acc = acc
        self.trained = True

    def predict_one(self, sample):
        print("predicting one")

        if not self.trained:
            sys.stderr.write(
                "Model should be trained or loaded before doing predict\n")
            sys.exit(-1)

        prediction = np.argmax(self.model.predict(np.array([sample])))
        return class_labels[prediction]

    def make_default_model(self) -> None:
        """
        Make the model with default hyper parameters
        """
        # This has to be implemented by child classes. The reason is that the
        # hyper parameters depends on the model.
        raise NotImplementedError()
Ejemplo n.º 28
0
model_cnn.add(Dropout(0.15))
model_cnn.add(Conv2D(128, (3,3), activation = 'relu'))
# model_cnn.add(Conv2D(128, (3,3), activation = 'relu'))
model_cnn.add(MaxPooling2D(pool_size=(2,2)))
model_cnn.add(Conv2D(128, (3,3), activation = 'relu'))

model_cnn.add(Flatten())
model_cnn.add(Dense(128, activation="sigmoid"))
model_cnn.add(Dropout(0.3))
model_cnn.add(Dense(128, activation='relu'))
model_cnn.add(Dense(num_classes, activation='softmax'))


model_cnn.compile(loss=keras.losses.categorical_crossentropy,
              optimizer='adam',
              metrics=['accuracy'])

model_cnn.fit(X_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1)
# model_cnn.summary()

print(datetime.datetime.now())

# save model face recognition
model_cnn.save('./keras_cnn_model_file/model_keras1.h5')
model_cnn.save_weights('./keras_cnn_model_file//model_keras_weights1.h5')
print(model_cnn.evaluate(X_test,y_test))

Ejemplo n.º 29
0
class DNN_Model(Common_Model):
    '''
    __init__(): 初始化神经网络

    输入:
        input_shape(tuple): 张量形状
        num_classes(int): 标签种类数量
    '''
    def __init__(self, input_shape, num_classes, **params):
        super(DNN_Model, self).__init__(**params)
        self.input_shape = input_shape
        self.model = Sequential()
        self.make_model()
        self.model.add(Dense(num_classes, activation = 'softmax'))
        self.model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
        print(self.model.summary(), file = sys.stderr)

    '''
    save_model(): 将模型权重以 model_name.h5 和 model_name.json 命名存储在 /Models 目录下
    '''
    def save_model(self, model_name):
        h5_save_path = 'Models/' + model_name + '.h5'
        self.model.save_weights(h5_save_path)

        save_json_path = 'Models/' + model_name + '.json'
        with open(save_json_path, "w") as json_file:
            json_file.write(self.model.to_json())

    '''
    train(): 在给定训练集上训练模型

    输入:
        x_train (numpy.ndarray): 训练集样本
        y_train (numpy.ndarray): 训练集标签
        x_val (numpy.ndarray): 测试集样本
        y_val (numpy.ndarray): 测试集标签
        n_epochs (int): epoch数

    '''
    def train(self, x_train, y_train, x_val = None, y_val = None, n_epochs = 50):
        best_acc = 0
        if x_val is None or y_val is None:
            x_val, y_val = x_train, y_train
        for i in range(n_epochs):
            # 每个epoch都随机排列训练数据
            p = np.random.permutation(len(x_train))
            x_train = x_train[p]
            y_train = y_train[p]
            self.model.fit(x_train, y_train, batch_size = 32, epochs = 1)
            # 训练过程的损失率变化图
            # 计算损失率和准确率
            loss, acc = self.model.evaluate(x_val, y_val)
            if acc > best_acc:
                best_acc = acc
        self.trained = True


    '''
    recognize_one(): 识别某个音频的情感

    输入:
        sample: 要预测的样本
    
    输出:
        预测结果,置信概率(int, numpy.ndarray)
    '''
    def recognize_one(self, sample):
        # 没有训练和加载过模型
        if not self.trained:
            sys.stderr.write("No Model.")
            sys.exit(-1)
        return np.argmax(self.model.predict(np.array([sample]))), self.model.predict(np.array([sample]))[0]

    def make_model(self):
        raise NotImplementedError()
Ejemplo n.º 30
0
class Model(MNIST_data):
    def __init__(self, dataset, name_json, name_weights, vers=1, params=None):
        super().__init__(dataset)
        self.name_json = name_json
        self.name_weights = name_weights
        self.vers = vers
        if params:
            self.params = params
            self.batch_size = params['batch_size']
        else:
            self.batch_size = 64

    def CNN_model(self):

        if self.vers == 1:
            self.model = Sequential()
            self.model.add(Conv2D(64, (3, 3), input_shape=(28, 28, 1)))
            self.model.add(Activation('relu'))
            self.model.add(MaxPool2D(pool_size=(2, 2)))
            self.model.add(Dropout(0.2))

            self.model.add(Conv2D(64, (3, 3)))
            self.model.add(Activation('relu'))
            self.model.add(MaxPool2D(pool_size=(2, 2)))
            self.model.add(Dropout(0.2))

            self.model.add(Flatten())
            self.model.add(Dense(64))
            #optional Dense layer
            #self.model.add(Activation('relu'))
            self.model.add(Dense(10))
            self.model.add(Activation('softmax'))

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

        elif self.vers == 2:

            params = self.params

            dropout_num = params['dropout']
            dense_neuron = params['dense_neuron']

            self.model = Sequential()
            self.model.add(Conv2D(64, (3, 3), input_shape=(28, 28, 1)))
            self.model.add(Activation(params['activation']))
            self.model.add(MaxPool2D(pool_size=(2, 2)))
            self.model.add(Dropout(dropout_num))

            self.model.add(Conv2D(64, (3, 3), input_shape=(28, 28, 1)))
            self.model.add(Activation(params['activation']))
            self.model.add(MaxPool2D(pool_size=(2, 2)))
            self.model.add(Dropout(dropout_num))

            self.model.add(Flatten())
            self.model.add(Dense(dense_neuron))
            self.model.add(Activation(params['activation']))
            self.model.add(Dense(10))
            self.model.add(Activation('softmax'))

            self.model.compile(loss='categorical_crossentropy',
                               optimizer=params['optimizer'],
                               metrics=['accuracy'])

    def fit_train(self):
        self.model.fit(x=self.train_X,
                       y=self.train_Y,
                       epochs=10,
                       batch_size=self.batch_size)

    def save_classifier(self):
        save_model = self.model.to_json()
        with open(self.name_json, 'w') as json_file:
            json_file.write(save_model)
        self.model.save_weights(self.name_weights)
        print("Successfully saved the CNN model")

    def load_classifier(self):
        with open(self.name_json, 'r') as json_file:
            saved_model = json_file.read()
        self.model = keras.models.model_from_json(saved_model)
        self.model.load_weights(self.name_weights)
        self.model.compile(loss='categorical_crossentropy',
                           optimizer='adam',
                           metrics=['accuracy'])

    def running_classifier(self):
        if os.path.isfile(self.name_json) and os.path.isfile(
                self.name_weights):
            self.load_classifier()
        else:
            self.CNN_model()
            self.fit_train()
            self.save_classifier()

    def evaluate_model(self):

        test_loss, test_acc = self.model.evaluate(self.test_X, self.test_Y)
        return (test_loss, test_acc)

    def predict(self):

        prediction = self.model.predict(self.test_X)
        print(np.argmax(np.round(prediction[0])))