Ejemplo n.º 1
0
def lstm():
    in_out_neurons = 1
    n_hidden = 200

    model = Sequential()
    model.add(
        LSTM(n_hidden, batch_input_shape=(None, time_step, in_out_neurons)))
    model.add(Dense(in_out_neurons))
    model.add(Activation("linear"))
    optimizer = Adam(lr=0.006)
    model.compile(loss="mean_squared_error", optimizer=optimizer)

    early_stopping = EarlyStopping(monitor='val_loss',
                                   mode='auto',
                                   patience=20)
    model.fit(train_x,
              train_y,
              batch_size=300,
              epochs=5,
              validation_split=0.1,
              callbacks=[early_stopping])
    model_json = model.to_json()
    with open(path + "lstm_model.json", "w") as json_file:
        json_file.write(model_json)
    model.save_weights(path + "lstm_model.h5")
    print("=====Saved model Ok======")
Ejemplo n.º 2
0
def lstm_model(model_name, train=False):
    if train:
        # define model
        model = Sequential()
        model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
        model.add(Dense(1))
        model.compile(optimizer='adam', loss='mse', metrics=[r2_keras])
        # fit model
        model.fit(X_train, y_train[:, 0], epochs=60, verbose=0)

        # Save model
        model_json = model.to_json()
        with open("%s_model.json" % model_name, "w") as json_file:
            json_file.write(model_json)
        model.save_weights("%s_model.h5" % model_name)
        print("Saving %s model to disk .." % model_name)
    else:
        json_file = open('%s_model.json' % model_name, 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        model = model_from_json(loaded_model_json)
        model.load_weights('%s_model.h5' % model_name)
        print('loaded model from disk')

        model.compile(optimizer='adam', loss='mse', metrics=[r2_keras])

    return model
Ejemplo n.º 3
0
 def train(self):
     print("===============Training Model===============")
     self.__prepare()
     model = Sequential()
     model.add(
         LSTM(self.n_hidden,
              batch_input_shape=(None, time_step, self.in_out_neurons)))
     model.add(Dense(self.in_out_neurons))
     model.add(Activation("linear"))
     optimizer = Adam(lr=self.lr)
     model.compile(loss="mean_squared_error", optimizer=optimizer)
     early_stopping = EarlyStopping(monitor='val_loss',
                                    mode='auto',
                                    patience=20)
     model.fit(self.train_x,
               self.train_y,
               batch_size=300,
               epochs=self.epoch,
               validation_split=0.1,
               callbacks=[early_stopping])
     model_json = model.to_json()
     with open(self.model_path + "lstm_model.json", "w") as json_file:
         json_file.write(model_json)
     model.save_weights("lstm_model.h5")
     self.model = model
Ejemplo n.º 4
0
class DNN_Model(Common_Model):
    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)

    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):

            print("NUMBER OF EPOCH :", i)
            # epoch (randomize training data)
            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)

            # accuracy, loss
            acc.append(history.history['acc'])
            loss.append(history.history['loss'])

            # validation: accuracy, 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.º 5
0
def build_model():
    ##model building
    model = Sequential()
    #convolutional layer with rectified linear unit activation
    model.add(
        layers.Conv2D(32,
                      kernel_size=(3, 3),
                      activation='relu',
                      input_shape=input_shape))
    #32 convolution filters used each of size 3x3
    #again
    model.add(layers.Conv2D(64, (3, 3), activation='relu'))
    #64 convolution filters used each of size 3x3
    #choose the best features via pooling
    model.add(layers.MaxPooling2D(pool_size=(2, 2)))
    #randomly turn neurons on and off to improve convergence
    model.add(layers.Dropout(0.25))
    #flatten since too many dimensions, we only want a classification output
    model.add(layers.Flatten())
    #fully connected to get all relevant data
    model.add(layers.Dense(128, activation='relu'))
    #one more dropout for convergence' sake :)
    model.add(layers.Dropout(0.5))
    #output a softmax to squash the matrix into output probabilities
    model.add(layers.Dense(num_category, activation='softmax'))

    #Adaptive learning rate (adaDelta) is a popular form of gradient descent rivaled only by adam and adagrad
    #categorical ce since we have multiple classes (10)
    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adadelta(),
                  metrics=['accuracy'])

    batch_size = 128
    num_epoch = 10
    #model training
    model_log = model.fit(X_train,
                          y_train,
                          batch_size=batch_size,
                          epochs=num_epoch,
                          verbose=1,
                          validation_data=(X_test, y_test))

    score = model.evaluate(X_test, y_test, verbose=0)
    print('Test loss:', score[0])  #Test loss: 0.0296396646054
    print('Test accuracy:', score[1])  #Test accuracy: 0.9904

    #Save the model
    # serialize model to JSON
    model_digit_json = model.to_json()
    with open("model_digit.json", "w") as json_file:
        json_file.write(model_digit_json)
    # serialize weights to HDF5
    model.save_weights("model_digit.h5")
    print("Saved model to disk")
Ejemplo n.º 6
0
def keras_model(force=False, verbose=False):
    """
    Método para retornar um modelo iterativo do Keras. Se um modelo já existir, carregará os pesos e retornará um objeto
    de modelo iterativo. Caso não exista um modelo, um será gerado.
    :param force: se deve ou não forçar a geração de um modelo fresco
    :param verbose: quando o verbose estiver ligado durante a criação de um modelo, mostrará a precisão do mesmo
    :return: modelo iterativo do keras
    """
    from keras import Sequential
    from keras.layers import Dense
    model_path = module_path + "/data/kerasdump.data"
    weights_path = module_path + "/data/kerasweights.h5"
    if Path(model_path).is_file() and force is False:
        from keras.models import model_from_json
        clear_session()
        loaded_model = model_from_json(open(model_path, 'r').read())
        loaded_model.load_weights(weights_path)
        loaded_model.compile(loss='binary_crossentropy',
                             optimizer='adam',
                             metrics=['accuracy'])
        return loaded_model
    else:
        clear_session()
        dataset = pandas.read_csv(module_path + "/data/dataset.csv",
                                  names=headers)
        # Separar dados de validação, e dados de treino
        array = dataset.values
        # Dados
        X = array[:, 0:(len(headers) - 1)]  # Dados
        Y = array[:, (len(headers) - 1)]  # Resultados
        numpy.random.seed(491826658)
        model = Sequential()
        model.add(Dense(52, input_dim=(len(headers) - 1), activation='relu'))
        model.add(Dense(29, activation='relu'))
        model.add(Dense(17, activation='relu'))
        model.add(Dense(5, activation='relu'))
        model.add(Dense(2, activation='relu'))
        model.add(Dense(1, activation='sigmoid'))
        # Compile model
        model.compile(loss='binary_crossentropy',
                      optimizer='adam',
                      metrics=['accuracy'])
        model.fit(X, Y, epochs=150, batch_size=10, verbose=0, shuffle=True)
        if verbose:
            print(evaluate(model, X, Y))
        model_json = model.to_json()
        with open(model_path, "w") as json_file:
            json_file.write(model_json)
        model.save_weights(weights_path)
        return model
Ejemplo n.º 7
0
 def keras_model(self, force=False, verbose=False, final=False):
     """
     Método para retornar um modelo iterativo do Keras. Se um modelo já existir, carregará os pesos e retornará um objeto
     de modelo iterativo. Caso não exista um modelo, um será gerado.
     :param final: Se deve utilizar todos os dados (True) do dataset ou dividi-lo (False)
     :param verbose: quando o verbose estiver ligado durante a criação de um modelo, mostrará a precisão do mesmo
     :param force: se deve ou não forçar a geração de um modelo fresco
     :return: modelo iterativo do keras
     """
     from keras import Sequential
     from keras.layers import Dense
     model_path = self.MODULE_PATH + "/data/model.data"
     weights_path = self.MODULE_PATH + "/data/weights.h5"
     if Path(model_path).is_file() and force is False:
         from keras.models import model_from_json
         clear_session()
         loaded_model = model_from_json(open(model_path, 'r').read())
         loaded_model.load_weights(weights_path)
         loaded_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
         return loaded_model
     else:
         clear_session()
         model = Sequential()
         model.add(Dense(30, input_dim=(len(self.HEADERS) - 1), activation='relu'))
         model.add(Dense(51, activation='relu'))
         model.add(Dense(36, activation='relu'))
         model.add(Dense(10, activation='relu'))
         model.add(Dense(1, activation='sigmoid'))
         # Compile model
         model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
         model_json = model.to_json()
         from sklearn import model_selection
         X_train, X_validation, Y_train, Y_validation = \
             model_selection.train_test_split(self.X, self.Y, test_size=self.validation_size)
         if not final:
             model.fit(X_train, Y_train, epochs=150, batch_size=20, verbose=0, shuffle=True)
         else:
             model.fit(self.X, self.Y, epochs=150, batch_size=20, verbose=0, shuffle=True)
         if verbose:
             if not final:
                 print(self.evaluate(model, evaluate_x=X_validation, evaluate_y=Y_validation))
             else:
                 print(self.evaluate(model, evaluate_x=self.X, evaluate_y=self.Y))
         with open(model_path, "w") as json_file:
             json_file.write(model_json)
         model.save_weights(weights_path)
         return model
Ejemplo n.º 8
0
    def learn_and_fit(self, learning_rate=0.001, epochs=200):
        for i in range(1, self.unique_classes_count + 1):
            print("Learning model " + str(i))

            # Loading the data
            X = np.array(pd.read_csv("model/x" + str(i) + ".csv",
                                     delimiter=";"),
                         dtype=int)
            Y = np.array(pd.read_csv("model/y" + str(i) + ".csv",
                                     delimiter=";"),
                         dtype=int)

            # Defining checkpoints
            mc = ModelCheckpoint("networks/class" + str(i) + '.h5',
                                 save_best_only=True,
                                 verbose=1,
                                 mode='min')

            # Defining model
            model = Sequential()
            model.add(
                Dense(int(X.shape[1] / 2),
                      input_dim=X.shape[1],
                      activation='relu'))
            model.add(
                Dense(int(X.shape[1] / 2),
                      input_dim=X.shape[1],
                      activation='relu'))
            model.add(Dense(Y.shape[1], activation='sigmoid'))
            model.compile(loss='mean_squared_error',
                          optimizer='adam',
                          metrics=['acc'])
            model.fit(X, Y, epochs=epochs, callbacks=[mc])

            # Saving model to array
            model.save_weights("networks/class" + str(i) + '.h5')
            model_json = model.to_json()
            with open("networks/class" + str(i) + '.json', "w") as json_file:
                json_file.write(model_json)
        # self.models.append(model)
        self.learned = True
Ejemplo n.º 9
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 = config.checkpoint_path + config.checkpoint_name + '.h5'
        self.model.save_weights(h5_save_path)

        save_json_path = 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.º 10
0
np.random.seed(10)

myModel = Sequential()

myModel.add(Dense(units=64, input_dim=39, activation='relu'))
myModel.add(Dense(units=8, activation='relu'))
myModel.add(Dense(1, activation='sigmoid'))

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

myModel.fit(x=trainDATA, y=trainLABLE, epochs=numOfEpoch, batch_size=64)

# Save the trained model
myModel_json = myModel.to_json()
with open(jsonModelFILE, 'w') as json_file:
    json_file.write(myModel_json)
myModel.save_weights(weightModelFILE)
print("The trained model has been saved.")

print("Start testing!")
print("Time point 3 is " + str(time.time() - startTime))

# Load the trained model
# Load the model
with open(jsonModelFILE) as modelFile:
    model_json = modelFile.read()

myModel = model_from_json(model_json)
myModel.load_weights(weightModelFILE)
Ejemplo n.º 11
0
class Model:

    def __init__(self, use_glove=False):
        self.use_glove = use_glove

        self.model = None
        self.max_len = 15
        self.embedding_size = 300
        self.vocabulary_size = 10000
        self.tokenizer = Tokenizer(num_words=self.vocabulary_size)

        self.log_dir = pathlib.Path('.log') / get_timestamp()
        self.log_dir.mkdir(parents=True, exist_ok=True)

    def build_model(self, embedding_initializer):
        self.model = Sequential()

        self.model.add(Embedding(
            input_dim=self.vocabulary_size,
            output_dim=self.embedding_size,
            input_length=self.max_len,
            embeddings_initializer=embedding_initializer if embedding_initializer is not None else 'uniform',
            trainable=embedding_initializer is None
        ))
        self.model.add(LSTM(32, return_sequences=True, dropout=0.3, kernel_regularizer=l2(0.001)))
        self.model.add(LSTM(32, dropout=0.3, kernel_regularizer=l2(0.001)))
        self.model.add(Dense(32, activation='relu', kernel_regularizer=l2(0.001)))
        self.model.add(Dense(1, activation='sigmoid'))

    def train(self, X_train, Y_train, X_val, Y_val, batch_size=32, epochs=10):
        self.tokenizer.fit_on_texts(X_train)

        # pickle word dictionary for later use
        with open('./checkpoints/tokenizer.pickle', 'wb') as handle:
            pickle.dump(self.tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL)

        encoded_train_titles = self.tokenizer.texts_to_sequences(X_train)
        encoded_val_titles = self.tokenizer.texts_to_sequences(X_val)

        X_train = encoded_train_titles
        X_val = encoded_val_titles

        X_train = pad_sequences(X_train, maxlen=self.max_len, value=0)
        X_val = pad_sequences(X_val, maxlen=self.max_len, value=0)

        word_index = self.tokenizer.word_index
        self.build_model(embedding_initializer=self.load_glove_embeddings(word_index) if self.use_glove else None)

        # ('Y_train mean:', np.mean(Y_train))
        # print('Y_val mean:', np.mean(Y_val))

        optimizer = Adam()
        callbacks = None
        if len(X_val) > 0:
            callbacks = [
                keras.callbacks.EarlyStopping(
                    monitor='val_acc',
                    mode='max',
                    verbose=1,
                    patience=15,
                ),
                keras.callbacks.ModelCheckpoint(
                    str(self.log_dir / 'best_model.hdf5'),
                    monitor='val_acc',
                    verbose=1,
                    save_best_only=True,
                    mode='max'
                )
            ]

        self.model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
        self.model.fit(X_train, Y_train, validation_data=(X_val, Y_val), batch_size=batch_size, epochs=epochs,
                       callbacks=callbacks, verbose=0)
        self.model.save('./checkpoints/model')

        with (self.log_dir / 'arch.json').open('w') as handle:
            handle.write(self.model.to_json())

        # scores = self.model.evaluate(X_val, Y_val, verbose=0)
        # print('val accuracy:', scores[1])

    def load_glove_embeddings(self, word_index):

        print('Loading embeddings.')
        embeddings_index = {}
        embeddings_path = './data/glove.6B/glove.6B.' + str(self.embedding_size) + 'd.txt'

        if not pathlib.Path(embeddings_path).exists():
            raise FileNotFoundError(
                'Download glove embeddings from http://nlp.stanford.edu/data/glove.6B.zip (822 MB file) and unzzip\n' +
                'Linux command:\n\n\t wget http://nlp.stanford.edu/data/glove.6B.zip; unzip glove.6B.zip'
            )

        f = open(embeddings_path, encoding='utf-8')
        for line in f:
            values = line.split()
            word = values[0]
            coefs = np.asarray(values[1:], dtype='float32')
            embeddings_index[word] = coefs
        f.close()

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

        print('Embeddings loaded.')

        return Constant(embedding_matrix)

    def load_model(self, path, dict_path):
        self.model = load_model(path)

        with open(dict_path, 'rb') as handle:
            self.tokenizer = pickle.load(handle)

    def classify(self, input):
        X = self.tokenizer.texts_to_sequences(input)
        X = pad_sequences(X, maxlen=self.max_len, value=0)
        return self.model.predict(X)

    def predict_classes(self, input):
        X = self.tokenizer.texts_to_sequences(input)
        X = pad_sequences(X, maxlen=self.max_len, value=0)
        return self.model.predict_classes(X)
Ejemplo n.º 12
0
class RNNKerasModelImpl(Model):
    MODEL_FILENAME = "model.json"

    def _restore_model(self, path):
        with open(os.path.join(path, RNNKerasModelImpl.MODEL_FILENAME)) as f:
            self.model = keras.models.model_from_json(f.read())

        self.model.load_weights(
            os.path.join(
                path, "{}_{}.h5".format(Model.WEIGHTS_DEFAULT_FILENAME,
                                        Model.WEIGHTS_DEFAULT_ID)))
        self._update_output_size()
        keras.utils.print_summary(self.model)

    def _init_new_model(self, input_embedding_size, output_size):
        self.model = Sequential()
        self.model.add(
            Bidirectional(LSTM(self.model_config.rnn_size,
                               return_sequences=True),
                          input_shape=(self.model_config.sequence_length,
                                       input_embedding_size)))
        self.model.add(Reshape((-1, )))
        self.model.add(Dense(output_size))
        self.model.add(Activation('softmax'))
        opt = RMSprop(lr=0.001, decay=0.)
        self.model.compile(loss='categorical_crossentropy', optimizer=opt)
        self._update_output_size()
        keras.utils.print_summary(self.model)

    def __init__(self) -> None:
        super().__init__()
        self.model = None
        self.output_size = None

    def _update_output_size(self):
        self.output_size = self.model.layers[-1].output_shape[-1]

    def get_log_callback(self):
        pass

    def classify(self, frames):
        classes = self.model.predict(frames, batch_size=frames.shape[0])
        return np.argmax(classes, 1)

    def train(self, x_batch, y_batch):
        y_batch = to_categorical(y_batch, self.output_size)
        return self.model.train_on_batch(x_batch, y_batch)

    def _save_model(self, config_dir):
        with open(os.path.join(config_dir, RNNKerasModelImpl.MODEL_FILENAME),
                  "w") as f:
            f.write(self.model.to_json())
        self.save_weights(config_dir, Model.WEIGHTS_DEFAULT_ID)

    def save_weights(self, config_dir, id=None) -> str:
        file_name = Model.WEIGHTS_DEFAULT_FILENAME
        if id is not None:
            file_name = "{}_{}".format(file_name, id)
        self.model.save_weights(
            os.path.join(config_dir, "{}.h5".format(file_name)))
        return file_name
Ejemplo n.º 13
0
class Player:
    """Mandatory class with the player methods"""
    def __init__(self, name='DQN', load_model=None, env=None):
        """Initiaization of an agent"""
        self.equity_alive = 0
        self.actions = []
        self.last_action_in_stage = ''
        self.temp_stack = []
        self.name = name
        self.autoplay = True

        self.dqn = None
        self.model = None
        self.env = env

        if load_model:
            self.load(load_model)

    def initiate_agent(self, env):
        """initiate a deep Q agent"""
        tf.compat.v1.disable_eager_execution()
        self.env = env

        nb_actions = self.env.action_space.n

        self.model = Sequential()
        self.model.add(
            Dense(512, activation='relu', input_shape=env.observation_space))
        self.model.add(Dropout(0.2))
        self.model.add(Dense(512, activation='relu'))
        self.model.add(Dropout(0.2))
        self.model.add(Dense(512, activation='relu'))
        self.model.add(Dropout(0.2))
        self.model.add(Dense(nb_actions, activation='linear'))

        # Finally, we configure and compile our agent. You can use every built-in Keras optimizer and
        # even the metrics!
        memory = SequentialMemory(limit=memory_limit,
                                  window_length=window_length)
        policy = TrumpPolicy()

        class CustomProcessor(Processor):
            """The agent and the environment"""
            def process_state_batch(self, batch):
                """
                Given a state batch, I want to remove the second dimension, because it's
                useless and prevents me from feeding the tensor into my CNN
                """
                return np.squeeze(batch, axis=1)

            def process_info(self, info):
                processed_info = info['player_data']
                if 'stack' in processed_info:
                    processed_info = {'x': 1}
                return processed_info

        nb_actions = env.action_space.n

        self.dqn = DQNAgent(model=self.model,
                            nb_actions=nb_actions,
                            memory=memory,
                            nb_steps_warmup=nb_steps_warmup,
                            target_model_update=1e-2,
                            policy=policy,
                            processor=CustomProcessor(),
                            batch_size=batch_size,
                            train_interval=train_interval,
                            enable_double_dqn=enable_double_dqn)
        self.dqn.compile(tf.optimizers.Adam(lr=1e-3), metrics=['mae'])

    def start_step_policy(self, observation):
        """Custom policy for random decisions for warm up."""
        log.info("Random action")
        _ = observation
        action = self.env.action_space.sample()
        return action

    def train(self, env_name):
        """Train a model"""
        # initiate training loop
        timestr = time.strftime("%Y%m%d-%H%M%S") + "_" + str(env_name)
        tensorboard = TensorBoard(log_dir='./Graph/{}'.format(timestr),
                                  histogram_freq=0,
                                  write_graph=True,
                                  write_images=False)

        self.dqn.fit(self.env,
                     nb_max_start_steps=nb_max_start_steps,
                     nb_steps=nb_steps,
                     visualize=False,
                     verbose=2,
                     start_step_policy=self.start_step_policy,
                     callbacks=[tensorboard])

        # Save the architecture
        dqn_json = self.model.to_json()
        with open("dqn_in_json.json", "w") as json_file:
            json.dump(dqn_json, json_file)

        # After training is done, we save the final weights.
        self.dqn.save_weights('dqn_{}_weights.h5'.format(env_name),
                              overwrite=True)

        # Finally, evaluate our algorithm for 5 episodes.
        self.dqn.test(self.env, nb_episodes=5, visualize=False)

    def load(self, env_name):
        """Load a model"""

        # Load the architecture
        with open('dqn_in_json.json', 'r') as architecture_json:
            dqn_json = json.load(architecture_json)

        self.model = model_from_json(dqn_json)
        self.model.load_weights('dqn_{}_weights.h5'.format(env_name))

    def play(self, nb_episodes=5, render=False):
        """Let the agent play"""
        memory = SequentialMemory(limit=memory_limit,
                                  window_length=window_length)
        policy = TrumpPolicy()

        class CustomProcessor(Processor):
            """The agent and the environment"""
            def process_state_batch(self, batch):
                """
                Given a state batch, I want to remove the second dimension, because it's
                useless and prevents me from feeding the tensor into my CNN
                """
                return np.squeeze(batch, axis=1)

            def process_info(self, info):
                processed_info = info['player_data']
                if 'stack' in processed_info:
                    processed_info = {'x': 1}
                return processed_info

        nb_actions = self.env.action_space.n

        self.dqn = DQNAgent(model=self.model,
                            nb_actions=nb_actions,
                            memory=memory,
                            nb_steps_warmup=nb_steps_warmup,
                            target_model_update=1e-2,
                            policy=policy,
                            processor=CustomProcessor(),
                            batch_size=batch_size,
                            train_interval=train_interval,
                            enable_double_dqn=enable_double_dqn)
        self.dqn.compile(tf.optimizers.Adam(lr=1e-3), metrics=['mae'])

        self.dqn.test(self.env, nb_episodes=nb_episodes, visualize=render)

    def action(self, action_space, observation, info):  # pylint: disable=no-self-use
        """Mandatory method that calculates the move based on the observation array and the action space."""
        _ = observation  # not using the observation for random decision
        _ = info

        this_player_action_space = {
            Action.FOLD, Action.CHECK, Action.CALL, Action.RAISE_POT,
            Action.RAISE_HALF_POT, Action.RAISE_2POT
        }
        _ = this_player_action_space.intersection(set(action_space))

        action = None
        return action
Ejemplo n.º 14
0
def main():
    training_images = load_data(train_data)

    tr_img_data = np.array([i[0]
                            for i in training_images]).reshape(-1, 64, 64, 1)
    tr_lbl_data = np.array([i[1] for i in training_images])

    model = Sequential()

    model.add(InputLayer(input_shape=[64, 64, 1]))
    model.add(
        Conv2D(filters=4,
               kernel_size=5,
               strides=1,
               padding='same',
               activation='relu'))
    model.add(MaxPool2D(pool_size=5, padding='same'))

    model.add(
        Conv2D(filters=8,
               kernel_size=5,
               strides=1,
               padding='same',
               activation='relu'))
    model.add(MaxPool2D(pool_size=5, padding='same'))

    model.add(
        Conv2D(filters=10,
               kernel_size=5,
               strides=1,
               padding='same',
               activation='relu'))
    model.add(MaxPool2D(pool_size=5, padding='same'))

    model.add(
        Conv2D(filters=20,
               kernel_size=5,
               strides=1,
               padding='same',
               activation='relu'))
    model.add(MaxPool2D(pool_size=5, padding='same'))

    model.add(Dropout(rate=0.25))
    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(rate=0.5))
    model.add(Dense(2, activation='softmax'))

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

    datagen = preprocessing.image.ImageDataGenerator(rotation_range=10,
                                                     width_shift_range=0.2,
                                                     height_shift_range=0.2,
                                                     zoom_range=0.2,
                                                     shear_range=0.2)

    model.fit_generator(datagen.flow(tr_img_data, tr_lbl_data, batch_size=256),
                        epochs=500)
    model.summary()

    # Save the weights
    model.save_weights('trained_model/model_weights.h5')

    # Save the model architecture
    with open('trained_model/model_architecture.json', 'w') as f:
        f.write(model.to_json())

    test()
Ejemplo n.º 15
0
model.add(Conv1D(filters = 128, kernel_size = Ckernel_size, strides=Cstrides, padding=padding))
model.add(BatchNormalization())
model.add(Activation(activation = acti))
model.add(MaxPooling1D(pool_size=Ppool_size*2, strides=Pstrides*2, padding=padding))

model.add(Flatten())
model.add(Dense(1024))
model.add(Dense(y_train.shape[1], activation='softmax'))

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

#save model to json file
model_json = model.to_json()
with open("model/model_json.json", "w") as json_file:
    json_file.write(model_json)

#callback funtion
tensorboard = TensorBoard('tensorboard/log_model_{}'.format(int(time.time())), write_graph=True , write_images=True)
checkpointer = ModelCheckpoint("./model/weights_best.h5", monitor='val_acc', verbose=1, save_best_only=True, mode='auto')

print('===================================================')
print('Start trainning CNN model')
print('Run "tensorboard --logdir tensorboard" to view logs')
print('===================================================')

#fit
model.fit(x_train, y_train, batch_size=32, epochs=50, verbose=1, validation_split = 0.2, callbacks= [tensorboard,checkpointer])
Ejemplo n.º 16
0
class QReplayNetworkModel(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).
    """
    default_check_convergence_every = 200  # by default check for convergence every # episodes

    def __init__(self, game, **kwargs):
        """ Create a new prediction model for 'game'.

        :param class Maze game: maze game object
        :param kwargs: model dependent init parameters
        """
        super().__init__(game, **kwargs)

        if kwargs.get("load", False) is False:
            self.model = Sequential()
            self.model.add(
                Dense(game.maze.size, input_shape=(2, ), activation="relu"))
            self.model.add(Dense(game.maze.size, activation="relu"))
            self.model.add(Dense(len(game.actions)))
        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, stop_at_convergence=False, **kwargs):
        """ Train the model.

            :param stop_at_convergence: stop training as soon as convergence is reached

            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
        """
        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 = max(kwargs.get("episodes", 1000), 1)
        sample_size = kwargs.get("sample_size", 32)
        check_convergence_every = kwargs.get(
            "check_convergence_every", self.default_check_convergence_every)

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

        # 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()

        # training starts here
        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)"""
            start_cell = (1, 1)

            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)
                    # action = random.choice(np.nonzero(q == np.max(q))[0])
                    action = self.predict(state)

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

                cumulative_reward += reward

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

                if status in (
                        Status.WIN,
                        Status.LOSE):  # terminal state reached, stop episode
                    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

                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.name, loss,
                        exploration_rate))

            if episode % check_convergence_every == 0:
                # check if the current model does win from all starting cells
                # only possible if there is a finite number of starting states
                w_all, win_rate = self.environment.check_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

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

        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. """
        if type(state) == tuple:
            state = np.array(state, ndmin=2)

        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: selected action
        """
        q = self.q(state)

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

        actions = np.nonzero(
            q == np.max(q))[0]  # get index of the action(s) with the max value
        return random.choice(actions)
Ejemplo n.º 17
0
# Encoder
lstm_autoencoder.add(
    LSTM(32,
         activation='relu',
         input_shape=(timesteps, n_features),
         return_sequences=True))
lstm_autoencoder.add(LSTM(16, activation='relu', return_sequences=False))
lstm_autoencoder.add(RepeatVector(timesteps))
# Decoder
lstm_autoencoder.add(LSTM(16, activation='relu', return_sequences=True))
lstm_autoencoder.add(LSTM(32, activation='relu', return_sequences=True))
lstm_autoencoder.add(TimeDistributed(Dense(n_features)))
lstm_autoencoder.summary()

with open('checkpoints/model.json', 'w') as f:
    f.write(lstm_autoencoder.to_json())

adam = optimizers.Adam(lr)
lstm_autoencoder.compile(metrics=['accuracy'], loss='mse', optimizer='adam')

cp = ModelCheckpoint("checkpoints/weights.h5", save_best_only=True)
tb = TensorBoard()
es = EarlyStopping(patience=patience)

history = lstm_autoencoder.fit(X_train_y0_scaled,
                               X_train_y0_scaled,
                               epochs=epochs,
                               batch_size=batch,
                               validation_data=(X_valid_y0_scaled,
                                                X_valid_y0_scaled),
                               verbose=2,
Ejemplo n.º 18
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(3,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')
def build_model():

    model = Sequential()
    model.add(
        Conv2D(filters=32,
               kernel_size=(3, 3),
               activation="relu",
               padding="same",
               input_shape=(256, 256, 1)))
    model.add(
        Conv2D(filters=32,
               kernel_size=(3, 3),
               activation="relu",
               padding="same"))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(rate=0.25))
    model.add(
        Conv2D(filters=64,
               kernel_size=(3, 3),
               activation="relu",
               padding="same"))
    model.add(
        Conv2D(filters=64,
               kernel_size=(3, 3),
               activation="relu",
               padding="same"))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(rate=0.25))
    model.add(Flatten())
    model.add(Dense(1024, activation="relu"))
    model.add(BatchNormalization())
    model.add(Dropout(rate=0.4))
    model.add(Dense(6, activation="softmax"))

    gen = ImageDataGenerator(rescale=1. / 255,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True)

    train_batches = gen.flow_from_directory("runes/mutated",
                                            model.input_shape[1:3],
                                            color_mode="grayscale",
                                            shuffle=True,
                                            seed=1,
                                            batch_size=16)
    valid_batches = gen.flow_from_directory("runes/validation",
                                            model.input_shape[1:3],
                                            color_mode="grayscale",
                                            shuffle=True,
                                            seed=1,
                                            batch_size=16)
    test_batches = gen.flow_from_directory("runes/testing",
                                           model.input_shape[1:3],
                                           shuffle=False,
                                           color_mode="grayscale",
                                           batch_size=8)

    model.compile(Adam(lr=0.001),
                  loss="categorical_crossentropy",
                  metrics=["accuracy"])
    history1 = model.fit_generator(train_batches,
                                   steps_per_epoch=163,
                                   epochs=5,
                                   validation_data=valid_batches,
                                   validation_steps=624)

    p = model.predict_generator(test_batches, verbose=True)

    # recall_score(pre["label"], pre["pre"])

    #roc_auc_score(pre["label"], pre[1])

    #true_positive_rate, false_positive_rate, threshold = roc_curve(pre["label"], pre[1])
    # roc = DataFrame([true_positive_rate, false_positive_rate]).T
    # roc.plot(x=0,y=1)

    plt.plot(history1.history['accuracy'])
    plt.plot(history1.history['val_accuracy'])
    plt.axhline(0, color="black")
    plt.axvline(0, color="black")
    plt.title('Model Accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Training set', 'Validation set'], loc='upper left')
    plt.show()

    plt.plot(history1.history['val_loss'])
    plt.plot(history1.history['loss'])
    plt.axhline(0, color="black")
    plt.axvline(0, color="black")
    plt.title('Model Loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Training set', 'Test set'], loc='upper left')
    plt.show()

    model_json = model.to_json()

    with open("model.json", "w") as json_file:
        json_file.write(model_json)

    model.save_weights_only = False
    model.save_weights("keras_model.h5")
Ejemplo n.º 20
0
def main(stock, r=0.1, s=0.1):
    result_dir = 'res_mlp_pca'

    data_length = 24000
    svm_gdf_res = gdf_pca.SvmGdfResults(
        stock,
        r=r,
        s=s,
        data_length=data_length,
        gdf_filename_pattern='gdf_{}_r{}_s{}_K50')
    feature_name = 'pca_n_gdf_que'
    n = svm_gdf_res.get_pca(feature_name).n_components
    hidden_layer_sizes = [(n, ), (n, n), (2 * n, n), (2 * n, 2 * n),
                          (n, 2 * n), (n, n, n)]

    weights = svm_gdf_res.get_classes_weights()
    epochs = 10
    batch_size = 300
    filename = os.path.join(
        result_dir,
        'mlp_pca_gdf_{}_len{}_r{}_s{}.csv'.format(stock, data_length, r, s))
    if os.path.exists(filename):
        print(f'Exists {filename}')
        return
    filename_partial = os.path.join(
        result_dir, 'mlp_pca_gdf_{}_len{}_r{}_s{}.csv_partial'.format(
            stock, data_length, r, s))
    df_partial = pd.DataFrame()
    if os.path.exists(filename_partial):
        print(f'Reading partial file {filename_partial}')
        df_partial = pd.read_csv(filename_partial)
    for hidden_layer_size in hidden_layer_sizes:
        for learning_rate in [0.001
                              ]:  #[0.00001, 0.0001, 0.001, 0.01, 0.1, 1.0]:
            if np.any(df_partial):

                print(filename_partial)
                row = df_partial[df_partial['hidden_layer_sizes'] ==
                                 hidden_layer_size]
                if np.any(row) and len(row) >= 1:
                    print(row)
                    row = df_partial[
                        df_partial['hidden_layer_sizes'] == hidden_layer_size][
                            df_partial['learning_rate'] == learning_rate]
                    print(row)
                    if np.any(row):
                        print(
                            f'Read result for hidden layer {hidden_layer_size} lr {learning_rate} in {filename_partial}'
                        )
                        continue
            print(
                f'Training {stock} {r} {s} {hidden_layer_size} {learning_rate}'
            )
            solver = optimizers.Adam(lr=learning_rate)
            model = Sequential()
            if isinstance(hidden_layer_size, int):
                model.add(Dense(hidden_layer_size))
            else:
                for h in hidden_layer_size:
                    model.add(Dense(h))
            model.add(Dense(1, activation='sigmoid'))

            plot_name = f'plot_mlp/{stock}_mlp_pca_n_r{r}_s{s}'
            score = svm_gdf_res.train_mlp(model,
                                          feature_name=feature_name,
                                          method='mlp',
                                          fit_kwargs={
                                              'epochs': epochs,
                                              'batch_size': batch_size,
                                              'verbose': 0,
                                              'shuffle': False
                                          },
                                          compile_kwargs={
                                              'loss': 'binary_crossentropy',
                                              'optimizer': solver,
                                              'metrics': [auc_roc, 'acc']
                                          },
                                          plot_name=plot_name,
                                          class_weight=weights)
            score = {
                **score, 'r': r,
                's': s,
                'arch': model.to_json(),
                'epochs': epochs,
                'batch_size': batch_size
            }
            score = {
                'solver': solver,
                'hidden_layer_sizes': hidden_layer_size,
                'learning_rate': learning_rate,
                **score
            }
            df_partial = df_partial.append(pd.DataFrame([score]),
                                           ignore_index=True)
            df_partial.index = list((range(len(df_partial))))
            #  df_partial.drop(columns=[[c for c in df_partial.columns if 'Unnamed' in c]], inplace=True)
            df_partial.to_csv(filename_partial)
    df_partial.to_csv(filename)
    return True
Ejemplo n.º 21
0
X_test = X_test.reshape(-1,96,96)
print("X_test", X_test.shape)
model =  Sequential()
# x = (7049,96,96)
# y = (7049,30)
model.add(Flatten(input_shape= (96,96)))
model.add(Dense(128,activation = "relu"))
model.add(Dropout(0.1))

model.add(Dense(64,activation = "relu"))
model.add(Dense(30))

model.compile(optimizer = 'adam',
              loss = 'mse',
              metrics = ['mae','accuracy'])
model.fit(X_train,y_train,epochs = 50, batch_size = 128,validation_split = 0.2)
model.save('model.h5')

json_string = model.to_json()
model = model_from_json(json_string)
model.load_weights('model.h5',by_name = True)
model.load_model('model.h5')
def loaded_model():
  model = load_model('model.h5')
  return model
def show_results(images_index):
  pred = model.predict(X_test[images_index:(images_index+1)])
  show_images(X_test[images_index], pred[0])
show_images(3)

Ejemplo n.º 22
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(100, dropout=0.2, recurrent_dropout=0.2, return_sequences=True)))
        self.model.add(Bidirectional(LSTM(20, dropout=0.2, recurrent_dropout=0.2, return_sequences=True)))
        self.model.add(TimeDistributed(Dense(17, activation='softmax')))
        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=150,validation_split=0.1,batch_size=128)
        pass

    def test_model(self):
        Y_pred = self.model.predict(self.X_test)
        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)):
            #Y_t_one = []
            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_testing.append(Y_t_one)

        Y_pred_F = []

        for i in range(0,len(Y_pred)):
            #Y_pred_one = []
            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_pred_F.append(Y_pred_one)

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


    def save_mode(self):
        self.model.save_weights("model.h5")
        model_json = self.model.to_json()
        with open("model.json", "w") as json_file:
            json_file.write(model_json)
        # serialize weights to HDF5
        self.model.save_weights("model.h5")
        print("Saved model to disk")
        pass
Ejemplo n.º 23
0
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()
class Mediapipe_Detector():
    def __init__(self, X_train, Y_train, X_test, Y_test):
        # Importing package for training and running models
        self.path = data_set_path
        self.classifier = None
        self.X_train = X_train
        self.Y_train = Y_train
        self.X_test = X_test
        self.Y_test = Y_test

    def label_encoder(self):
        encoder = LabelEncoder()
        encoder.fit(self.Y_train)
        Y_train_encoded = encoder.transform(self.Y_train)
        self.Y_train = np_utils.to_categorical(Y_train_encoded)

        encoder.fit(self.Y_test)
        Y_test_encoded = encoder.transform(self.Y_test)
        self.Y_test = np_utils.to_categorical(Y_test_encoded)

    def train(self):
        # if os.path.exists(model_weight_name) and os.path.exists(model_structure_name):
        #     # Loading the model #######################################
        #     json_file = open(model_structure_name, 'r')
        #     classifier_json = json_file.read()
        #     json_file.close()
        #     self.classifier = model_from_json(classifier_json)
        #     self.classifier.load_weights(model_weight_name)

        # else:
        #     # # Building the dense layer model #######################################
        #     # self.classifier = Sequential()
        #     # self.classifier.add(Dense(20, activation='relu',
        #     #                           kernel_initializer='random_normal',
        #     #                           input_dim=10))
        #     # # self.classifier.add(Conv1D(100, 2, 2, activation='relu',
        #     # #                            kernel_initializer='relu'))
        #     # self.classifier.add(Dense(12, activation='relu',
        #     #                           kernel_initializer='random_normal'))
        #     # self.classifier.add(Dense(10, activation='relu',
        #     #                           kernel_initializer='random_normal'))

        # Building the CNN layer model ########################
        self.classifier = Sequential()
        self.classifier.add(
            Conv1D(1000,
                   2,
                   strides=2,
                   padding='valid',
                   activation='relu',
                   use_bias=True,
                   kernel_initializer='random_normal',
                   input_shape=(15, 1),
                   name='convolution_1d_layer_1'))
        self.classifier.add(
            Conv1D(1000,
                   5,
                   padding='valid',
                   activation='relu',
                   use_bias=True,
                   kernel_initializer='random_normal',
                   name='convolution_1d_layer_2'))

        self.classifier.add(Flatten(name='reshape_layer'))
        self.classifier.add(
            Dense(
                100,
                kernel_initializer='random_normal',
                # bias_initializer=' random_normal',
                use_bias=True,
                name='full_connect_layer_1',
                activation='softmax'))
        # self.classifier.add(Dropout(0.5))
        self.classifier.add(
            Dense(
                10,
                kernel_initializer='random_normal',
                # bias_initializer=' random_normal',
                use_bias=True,
                name='full_connect_layer_2',
                activation='softmax'))

        self.classifier.compile(
            optimizer=Adam(adam_lr),
            # loss='binary_crossentropy',
            loss='categorical_crossentropy',
            metrics=['accuracy'])

        # Record acc on the log #################################
        losshistory = LossHistory()

        # Saving the model if the validation loss decresed
        checkpointer = ModelCheckpoint(filepath=model_weight_name,
                                       verbose=1,
                                       save_best_only=True)

        # Fitting/training the model ##############################
        self.label_encoder()
        print(self.Y_train)  # just for test
        self.classifier.fit(self.X_train,
                            self.Y_train,
                            epochs=2000,
                            validation_data=(self.X_test, self.Y_test),
                            callbacks=[checkpointer, losshistory],
                            batch_size=80)

        # Reporting loss history ##################################
        print(losshistory.losses)

        # Serializing model to JSON ##############################
        classifier_json = self.classifier.to_json()
        with open(model_structure_name, 'w') as json_file:
            json_file.write(classifier_json)
Ejemplo n.º 25
0
class Training():

	def __init__(self, name_pickle):
		dataset = pd.read_pickle(name_pickle)
		data = pd.DataFrame(dataset)
		self.data = data.sample(frac=1)


	def split_data(self, nb_features=54):
		'''Split dataset into training and testing data
		Input: dataset, number of features to take into account
		'''
		# Creating input features and target variables
		X = self.data.iloc[:, :nb_features]
		y = self.data.iloc[:, -1]

		self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size=0.2, shuffle=True)

		# Create pickle file with the input matrix
		with open('data_test.pkl', 'wb') as f:
			pickle.dump([self.X_test, self.y_test] , f, 2)


	def buildDNN(self, hidden_layers=2):
		'''Building the architecture of the network (perceptron)
		Input: number of hidden layers
		'''
		self.classifier = Sequential()

		# First Layer
		self.classifier.add(Dense(54, kernel_regularizer=l2(0.0001), bias_regularizer=l2(0.0007), activation='tanh', kernel_constraint=unit_norm(), kernel_initializer='random_normal', input_dim=54))
		self.classifier.add(Dropout(0.8))

		# Hidden Layer(s)
		for nbLayer in range(hidden_layers - 1):
			self.classifier.add(Dense(54, kernel_regularizer=l2(0.0001), bias_regularizer=l2(0.0007), activation='tanh', kernel_initializer='random_normal'))
			self.classifier.add(Dropout(0.8))

		# Output Layer
		self.classifier.add(Dense(1, activation='sigmoid', kernel_initializer='random_normal'))

		#try:
			#load weights
		#	self.classifier.load_weights("weights.best.hdf5")
		#except e:
		#	pass

		# Optimizer
		adam = optimizers.Adam(lr=0.001)

		# Compiling the neural network
		self.classifier.compile(optimizer=adam, loss='binary_crossentropy', metrics =['accuracy'])


	def train(self, epochNb=1000):
		'''Train the network
		Input: Number of epochs
		Output: Save a json and h5 file with the weights
		'''
		logdir = "tensorboard/keras_model/" + datetime.now().strftime("%Y%m%d-%H%M%S")
		tensorboard_callback = TensorBoard(log_dir=logdir)

		# checkpoint
		filepath="weights.best.hdf5"
		checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
		callbacks_list = [checkpoint]

		# Fitting the data to the training dataset
		self.history = self.classifier.fit(self.X_train, self.y_train, validation_split=0.20, callbacks=callbacks_list, batch_size=32, epochs=epochNb)

		# Save model as json file
		model_json = self.classifier.to_json()
		with open("model.json", "w") as f:
			f.write(model_json)
			f.close()

		# serialize weights to HDF5
		self.classifier.save_weights("model.h5")
		print("\nModel train and saved as 'model.json' and 'model.h5'")


	def plot_acc(self, all=None):
		'''Plot of the accuracy after training
		'''
		# summarize history for accuracy
		plt.plot(self.history.history['acc'])
		plt.plot(self.history.history['val_acc'])
		plt.title('model accuracy')
		plt.ylabel('accuracy')
		plt.xlabel('epoch')
		plt.legend(['train', 'test'], loc='upper left')

		if not all:
			plt.show()


	def plot_loss(self):
		'''Plot of the loss after training
		'''
		# summarize history for loss
		plt.plot(self.history.history['loss'])
		plt.plot(self.history.history['val_loss'])
		plt.title('model loss')
		plt.ylabel('loss')
		plt.xlabel('epoch')
		plt.legend(['train', 'test'], loc='upper left')
		plt.show()


	def plot_all(self):
		'''Plot of the accuracy and loss (combination of plot_loss and plot_acc) after training
		'''
		plt.figure()
		plt.subplot(1,2,1)
		self.plot_acc(True)
		plt.subplot(1,2,2)
		self.plot_loss()
Ejemplo n.º 26
0
class QReplayNetworkModel(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, env, **kwargs):
        super().__init__(env, **kwargs)

        if kwargs.get("load", False) is False:
            self.model = Sequential()
            print(env.maze.size)
            self.model.add(
                Dense(env.maze.size, input_shape=(2, ), activation="relu"))
            self.model.add(Dense(env.maze.size, activation="relu"))
            self.model.add(Dense(len(actions)))
        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, stop_at_convergence=False, **kwargs):
        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)
        sample_size = kwargs.get("sample_size", 32)

        experience = ExperienceReplay(discount=discount)

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

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

        for episode in range(1, episodes + 1):

            state = self.environment.reset()
            loss = 0.0

            while True:
                if np.random.random() < exploration_rate:
                    action = random.choice(self.environment.actions)
                else:
                    print(state)
                    action = self.predict(state)
                    print(state)

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

                cumulative_reward += reward

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

                if status in ("win",
                              "lose"):  # terminal state reached, stop episode
                    if status == "win":
                        win_history += 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

                # 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 % 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)
            #     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

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

        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. """
        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.
        """
        print("asdasd")
        q = self.q(state)
        print("asdasd")

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

        actions = np.nonzero(
            q == np.max(q))[0]  # get index of the action(s) with the max value
        return random.choice(actions)
class NER_BiLSTM_Glove_i2b2(object):
    """Class that implements and performs named entity recognition using BiLSTM neural network architecture. The architecture uses GloVe
    embeddings trained on common crawl dataset. Then the algorithm is trained on i2b2 2014 dataset. """
    def __init__(self):
        """Implementation of initialization"""
        # load json and create model
        self.model = None
        if os.path.exists('Models/BiLSTM_Glove_de_identification_model.json'):
            json_file = open(
                'Models/BiLSTM_Glove_de_identification_model.json', 'r')
            loaded_model_json = json_file.read()
            json_file.close()
            self.model = model_from_json(loaded_model_json)
        self.GLOVE_DIR = "Resources/"
        if os.path.isdir(self.GLOVE_DIR) == False or os.path.isfile(
                self.GLOVE_DIR + "glove.840B.300d.txt") == False:
            if os.path.exists(self.GLOVE_DIR) == False:
                os.mkdir(self.GLOVE_DIR)
            print('Beginning file download with urllib2...')
            url = 'http://nlp.stanford.edu/data/glove.840B.300d.zip'
            urllib.request.urlretrieve(url,
                                       self.GLOVE_DIR + 'glove.840B.300d.zip')
            with ZipFile(self.GLOVE_DIR + 'glove.840B.300d.zip',
                         'r') as zipObj:
                # Extract all the contents of zip file in current directory
                zipObj.extractall(self.GLOVE_DIR)
            os.remove(self.GLOVE_DIR + "glove.840B.300d.zip")
        # load weights into new model
        self.model.load_weights(
            "Models/BiLSTM_Glove_de_identification_model.h5")
        print("Loaded model from disk")
        self.model.compile(loss='categorical_crossentropy',
                           optimizer='rmsprop',
                           metrics=['accuracy'])
        self.word_index = pickle.load(open("Models/word_index.pkl", "rb"))
        self.MAX_SEQUENCE_LENGTH = 200
        self.EMBEDDING_DIM = 300
        self.MAX_NB_WORDS = 2200000

    def build_tensor2(self,
                      sequences,
                      numrecs,
                      word2index,
                      maxlen,
                      makecategorical=False,
                      num_classes=0,
                      is_label=False):
        """
        Function to create tensors out of sequences

        :param sequences: Sequences of words
        :param numrecs: size of the tensor
        :param word2index: mapping between words and its numerical representation (index). Loaded from file
        :param maxlen: Maximal lenght of the sequence
        :param makecategorical: Not used
        :param num_classes: Not used
        :param is_label: Not used, leave default for action performing
        :return:
        """
        data = np.empty((numrecs, ), dtype=list)
        label_index = {'O': 0}
        label_set = [
            "DATE", "LOCATION", "NAME", "ID", "AGE", "CONTACT", "PROFESSION",
            "PHI"
        ]
        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(sequences, desc='Building tensor'):
            wids = []
            pl = []
            for word, label in sent:
                if is_label == False:
                    if word in word2index:
                        wids.append(word2index[word])
                    else:
                        wids.append(word2index['the'])
                else:
                    pl.append(label_index[label])
            plabels.append(pl)
            if not is_label:
                data[i] = wids
            i += 1
        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

    def build_tensor(self,
                     sequences,
                     numrecs,
                     word2index,
                     maxlen,
                     makecategorical=False,
                     num_classes=0,
                     is_label=False):
        """
        Function to create tensors out of sequences

        :param sequences: Sequences of words
        :param numrecs: size of the tensor
        :param word2index: mapping between words and its numerical representation (index). Loaded from file
        :param maxlen: Maximal lenght of the sequence
        :param makecategorical: Not used
        :param num_classes: Not used
        :param is_label: Not used, leave default for action performing
        :return:
        """
        data = np.empty((numrecs, ), dtype=list)
        label_index = {'O': 0}
        label_set = [
            "DATE", "LOCATION", "NAME", "ID", "AGE", "CONTACT", "PROFESSION",
            "PHI"
        ]
        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(sequences, desc='Building tensor'):
            wids = []
            pl = []
            for word in sent:
                if is_label == False:
                    if word[0] in word2index:
                        wids.append(word2index[word[0]])
                    else:
                        wids.append(word2index['the'])
            plabels.append(pl)
            if not is_label:
                data[i] = wids
            i += 1
        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

    def transform(self, sequence):
        X = self.build_tensor(sequence, len(sequence), self.word_index, 70)
        Y = self.build_tensor(sequence, len(sequence), self.word_index, 70,
                              True, 9, True)

    def perform_NER(self, text):
        """
        Function that perform BiLSTM-based NER

        :param text: Text that should be analyzed and tagged
        :return: returns sequence of sequences with labels
        """
        sequences = tokenize_fa([text])
        word_sequences = []
        X_test = []
        tokens = []
        for seq in sequences:
            features_seq = []
            sentence = []
            for i in range(0, len(seq)):
                features_seq.append(seq[i][0])
                tokens.append(seq[i][0])
                sentence.append(seq[i][0])
            X_test.append(sentence)
            word_sequences.append(sentence)
        tensor = self.build_tensor(sequences, len(sequences), self.word_index,
                                   70)
        predictions = self.model.predict(tensor)
        Y_pred_F = []
        for i in range(0, len(predictions)):
            seq = []
            for j in range(0, len(predictions[i])):
                max_k = 0
                max_k_val = 0
                max_str = ""
                for k in range(0, len(predictions[i][j])):
                    if predictions[i][j][k] > max_k_val:
                        max_k_val = predictions[i][j][k]
                        max_k = k
                if max_k == 0:
                    max_str = "O"
                elif max_k == 1:
                    max_str = "DATE"
                elif max_k == 2:
                    max_str = "LOCATION"
                elif max_k == 3:
                    max_str = "NAME"
                elif max_k == 4:
                    max_str = "ID"
                elif max_k == 5:
                    max_str = "AGE"
                elif max_k == 6:
                    max_str = "CONTACT"
                elif max_k == 7:
                    max_str = "PROFESSION"
                elif max_k == 8:
                    max_str = "PHI"
                seq.append(max_str)
            Y_pred_F.append(seq)
        final_sequences = []
        for j in range(0, len(Y_pred_F)):
            sentence = []
            for i in range(
                    len(Y_pred_F[j]) - len(sequences[j]), len(Y_pred_F[j])):
                sentence.append(
                    (sequences[j][i -
                                  (len(Y_pred_F[j]) - len(sequences[j]))][0],
                     Y_pred_F[j][i]))
            final_sequences.append(sentence)
        return final_sequences

    def createModel(self, text, GLOVE_DIR):
        self.embeddings_index = {}
        if os.path.isdir(GLOVE_DIR) == False or os.path.isfile(
                GLOVE_DIR + "glove.840B.300d.txt") == False:
            print('Beginning GloVe file download with urllib2...')
            url = 'http://nlp.stanford.edu/data/glove.840B.300d.zip'
            if os.path.exists(self.GLOVE_DIR) == False:
                os.mkdir(self.GLOVE_DIR)
            urllib.request.urlretrieve(url,
                                       self.GLOVE_DIR + 'glove.840B.300d.zip')
            with ZipFile(self.GLOVE_DIR + 'glove.840B.300d.zip',
                         'r') as zipObj:
                # Extract all the contents of zip file in current directory
                zipObj.extractall(GLOVE_DIR)
            os.remove(self.GLOVE_DIR + "glove.840B.300d.zip")
        f = open(os.path.join(GLOVE_DIR, 'glove.840B.300d.txt'),
                 encoding='utf')
        for line in f:
            values = line.split()
            word = ''.join(values[:-300])
            coefs = np.asarray(values[-300:], 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)

        self.word_index = tokenizer.word_index
        pickle.dump(self.word_index, open("word_index.pkl", 'wb'))

        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

        self.embedding_layer = Embedding(len(self.word_index) + 1,
                                         self.EMBEDDING_DIM,
                                         weights=[self.embedding_matrix],
                                         input_length=70,
                                         trainable=True)
        self.model = Sequential()
        self.model.add(self.embedding_layer)
        self.model.add(
            Bidirectional(
                LSTM(150,
                     dropout=0.3,
                     recurrent_dropout=0.6,
                     return_sequences=True))
        )  #{'sum', 'mul', 'concat', 'ave', None}
        self.model.add(
            Bidirectional(
                LSTM(60,
                     dropout=0.2,
                     recurrent_dropout=0.5,
                     return_sequences=True)))
        self.model.add(TimeDistributed(Dense(
            9,
            activation='softmax')))  # a dense layer as suggested by neuralNer
        self.model.compile(loss="categorical_crossentropy",
                           optimizer='rmsprop',
                           metrics=['accuracy'])
        self.model.summary()
        pass

    def transform_sequences(self, token_sequences):
        text = []
        for ts in token_sequences:
            for t in ts:
                text.append(t[0])
        self.createModel(text, self.GLOVE_DIR)
        X = self.build_tensor2(token_sequences, len(token_sequences),
                               self.word_index, 70)
        Y = self.build_tensor2(token_sequences, len(token_sequences),
                               self.word_index, 70, True, 9, True)
        return X, Y

    def learn(self, X, Y, epochs=1):
        self.model.fit(X,
                       Y,
                       epochs=epochs,
                       validation_split=0.1,
                       batch_size=64)

    def evaluate(self, X, Y):
        Y_pred = self.model.predict(X)
        from sklearn import metrics
        labels = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        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(Y)):
            for j in range(0, len(Y[i])):
                max_k = 0
                max_k_val = 0
                for k in range(0, len(Y[i][j])):
                    if Y[i][j][k] > max_k_val:
                        max_k_val = Y[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(self, model_path):
        # serialize model to JSON
        model_json = self.model.to_json()
        with open("Models\\" + model_path + ".json", "w") as json_file:
            json_file.write(model_json)
        # serialize weights to HDF5
        self.model.model.save_weights("Models\\" + model_path + ".h5")
        print("Saved model to disk")
Ejemplo n.º 28
0
# plotting the metrics
fig = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(model_log.history['acc'])
plt.plot(model_log.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='lower right')

plt.subplot(2, 1, 2)
plt.plot(model_log.history['loss'])
plt.plot(model_log.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper right')

plt.tight_layout()

fig

#Save the model
# serialize model to JSON
model_digit_json = model.to_json()
with open("model_digit.json", "w") as json_file:
    json_file.write(model_digit_json)
# serialize weights to HDF5
model.save_weights("model_digit.h5")
print("Saved model to disk")
Ejemplo n.º 29
0
class QReplayDoublePrior(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 = (2, )

        if kwargs.get("load", False) is False:
            print(len(self.environment.empty))
            self.model = Sequential()
            self.model.add(
                Dense(len(self.environment.empty) * 4,
                      input_shape=self.state_size,
                      activation="relu"))
            self.model.add(
                Dense(len(self.environment.empty) * 2, 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(len(self.environment.empty) * 4,
                  input_shape=self.state_size,
                  activation="relu"))
        self.target_model.add(
            Dense(len(self.environment.empty) * 2, 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
        experience.maze = self.game.maze
        experience.cells = self.game.cells
        experience.exit_cell = self.game.exit_cell
        experience.state_size = self.state_size
        experience.walls = self.game.walls

        # 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] * 15
                            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)
                cumulative_reward += reward
                experience.remember(state, action_index, reward, next_state,
                                    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 % 5 == 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. """
        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
Ejemplo n.º 30
0
eval_model=classifier.evaluate(X_train, y_train)
print("\n\t eval_model=",eval_model)

y_pred=classifier.predict(X_test)


#print("\n\t y_test=",type(y_test),"\t y_pred=",type(y_pred))
y_pred =(y_pred>0.5)
#y_pred=pd.Series(y_pred)

'''
    saving model
'''

model_json=classifier.to_json()

with open("/home/kapitsa/PycharmProjects/upwork/models//model.json","w") as json_file:
    json_file.write(model_json)

classifier.save_weights("/home/kapitsa/PycharmProjects/upwork/models//model.h5")

'''
'''

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)

print("2.cm=",cm)