コード例 #1
0
ファイル: train.py プロジェクト: wararaki718/scrapbox2
def train(dataset: 'Dataset', epochs: int = 10):
    loader = DataLoader(dataset, batch_size=2, shuffle=True)

    model = NNModel(n_input=2, n_output=3)
    # model.to(device='cpu')

    optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
    criterion = torch.nn.CrossEntropyLoss()

    start_tm = time.time()
    for epoch in range(1, epochs + 1):
        train_loss = 0.0
        train_acc = 0
        for x, y in loader:
            optimizer.zero_grad()

            y_pred = model(x)
            y = torch.max(torch.squeeze(y, dim=1), dim=1).indices

            loss = criterion(y_pred, y)
            loss.backward()
            optimizer.step()
            train_loss += loss.item()
            train_acc += (y_pred.argmax(1) == y).sum().item()
        print(f'[epoch {epoch:02d}]\tloss:{train_loss}\taccuracy:{train_acc}')
    finish_tm = time.time()
    print(f'train finished.({finish_tm-start_tm}sec)')
コード例 #2
0
ファイル: gym_task.py プロジェクト: xynmxy/ZOOpt
    def new_nnmodel(self, layers):
        """
        Generate a new model

        :param layers: layer information
        :return: no return
        """
        # initialize NN model as policy
        self.__policy_model = NNModel()
        self.__policy_model.construct_nnmodel(layers)

        return
コード例 #3
0
ファイル: robot_executor.py プロジェクト: f2276701/robot
    def __init__(self, args, rate=1):
        self.node = rospy.init_node(self.node_name)
        self.joint_sub = JointSubscriber(self.joint_topic)
        self.img_sub = ImageSubscriber(self.img_topic)
        self.rate = rate

        self.gen_motion = GenMotion(self.joint_service, self.gripper_service, rate=rate) 
        self.model = NNModel(args)

        self.app = QApplication([])
        self.window = GoalWidget(self.model.goal_img)

        self.crop_size = ((36, 36 + 260), (250, 250 + 260))
コード例 #4
0
    def make_predictions(self, hotel_name, platforms):
        all_reviews = self.read_from_platforms(hotel_name, platforms)
        reviews_df = self.make_dataframe(all_reviews)

        nnm = NNModel()
        predited_df = nnm.generate_predictions(reviews_df)

        print(predited_df.shape)

        predited_df.to_csv(
            'C:/Users/acfelk/Documents/IIT_Files/final year/FYP/fyp_workfiles/final_project/backend/predicted_data/'
            + hotel_name + '_' + platforms + '_predicted.csv')

        return predited_df
コード例 #5
0
def check_predict_ratings():
    data = pd.read_csv('test_data/test_revs.csv')

    nn_model = NNModel()

    predicted_df = nn_model.generate_predictions(data)

    predicted_df.to_csv('test_data/predicted_revs.csv')

    data = pd.read_csv('test_data/predicted_revs.csv')

    if data['pred_rating'] is not None:
        print('Testing passed - Reviews Precited Successfully !')
        os.remove('test_data/predicted_revs.csv')
    else:
        print('Review Prediction has failed')
コード例 #6
0
ファイル: main.py プロジェクト: wararaki718/scrapbox3
def main():
    X = torch.randn(1, 1, 32, 32)
    y = torch.randn(10).view(1, -1)

    model = NNModel()
    criterion = torch.nn.MSELoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

    optimizer.zero_grad()
    y_pred = model(X)
    loss = criterion(y_pred, y)

    print(y_pred)
    print(loss)

    loss.backward()
    optimizer.step()

    print('DONE')
コード例 #7
0
def print_experiment(exp_id, exp):
    from nn_model import NNModel

    class_2_labels = ['Normal', 'Abnormal']
    class_6_labels = ['NN', 'EM', 'BV', 'GG', 'GR', 'HC']
    mapped_class_6_labels = ['NN', 'EM', 'BV', 'GG', 'GR', 'HC', 'MX']

    print("\n\n\n=============== " + exp_id + "  ===============")
    class_count = np.max(exp['actual']) + 1

    actual_labels = class_2_labels if class_count == 2 else class_6_labels
    mapped_labels = actual_labels if exp[
        'class_map'] is None else mapped_class_6_labels

    class_report = classification_report(exp['actual'],
                                         exp['predicted'],
                                         target_names=actual_labels)

    mu = np.mean(exp['accuracy'])
    sigma = np.std(exp['accuracy'])

    print(scipy.stats.norm.interval(0.95, loc=mu, scale=sigma))
    print("μ = ", str(mu))
    print("σ = ", str(sigma))

    print(class_report_latex(class_report))
    print(
        format_conf_matrix(
            actual_labels, actual_labels,
            NNModel.confusion_matrix(exp['actual_labels'],
                                     exp['actual_labels'], exp['actual'],
                                     exp['predicted'])))

    if exp['class_map'] is not None:
        # print(exp['original'], exp['original_labels'])
        print(
            format_conf_matrix(
                actual_labels, mapped_labels,
                NNModel.confusion_matrix(exp['actual_labels'],
                                         exp['original_labels'],
                                         exp['original'], exp['predicted'])))
コード例 #8
0
def main():
    s_dim = 4
    a_dim = 2
    batch_size = 64

    env = "../envs/point_mass2d.xml"
    sim = Simulation(env, s_dim, a_dim, None, False)

    length = 500
    rb = ReplayBuffer(length,
                      env_dict={"obs": {"shape": (s_dim, 1)},
                      "act": {"shape": (a_dim, 1)},
                      "rew": {},
                      "next_obs": {"shape": (s_dim, 1)},
                      "done": {}})

    x = sim.getState()
    for _ in range(length):
        u = np.random.rand(1, a_dim, 1)
        x_next = sim.step(u)

        rb.add(obs=x, act=u, rew=0, next_obs=x_next, done=False)
        x = x_next


    model = NNModel(dt=0.1, state_dim=s_dim, action_dim=a_dim, name="nn_model")

    stamp = datetime.now().strftime("%Y.%m.%d-%H:%M:%S")
    logdir = "../graphs/test_training/{}".format(stamp)

    writer = tf.summary.create_file_writer(logdir)
    log = True

    epochs = 1000
    for e in range(epochs):
        sample = rb.sample(batch_size)
        gt = sample['next_obs']
        x = sample['obs']
        u = sample['act']
        model.train_step(gt, x, u, e, writer, log)
コード例 #9
0
class MyAdaptiveFilter(BasicFilter):
    def __init__(self, name, acc, trans, noits, meats, u0, v0_prob, wk_prob,
                 vk_prob, total_frame, param):
        super().__init__(name, acc, trans, noits, meats, u0, v0_prob, wk_prob,
                         vk_prob, total_frame)
        self.model = NNModel(param)

    def fit(self,
            X_train,
            y_train,
            is_valid=True,
            save_model=False,
            path=None):
        self.model.fit(X_train, y_train, is_valid=is_valid)
        if save_model:
            self.model.save_model(path)

    def predict(self, measure, TPM, *args, **kwargs):
        load_model = kwargs['load_model']
        path = kwargs['path']
        if load_model:
            self.model.load_model(path)
        sample = measure.shape[0]
        LH = np.empty([sample, 3], np.float32)
        uk = np.empty([sample, 3], np.float32)
        X = np.empty([sample, 3, 2], np.float32)
        P = np.empty([sample, 3, 2, 2], np.float32)
        mode = np.empty([sample, self.frame], np.float32)
        state = np.empty([sample, self.frame, 2], np.float32)
        his_TPM = np.empty([sample, self.frame, 3, 3], np.float32)
        for i in range(sample):
            mode[i], state[i], X[i], P[i], LH[i], uk[i] = self.initial_state(
                measure[i, 0])
        for k in range(1, self.frame):
            for i in range(sample):
                mode[i, k], state[i, k], X[i], P[i], LH[i], uk[i] = \
                    self.IMMFilter(measure[i, k], TPM[i], X[i], P[i], LH[i], uk[i])
            X_test = np.concatenate([
                np.zeros([sample, self.frame - 1 - k, 3], dtype=np.float32),
                np.array([
                    mode[:, :k + 1] == 0, mode[:, :k + 1] == 1, mode[:, :k + 1]
                    == 2
                ],
                         dtype=np.float32).transpose([1, 2, 0])
            ],
                                    axis=1).reshape([sample, -1])
            TPM = self.model.predict(X_test)
            his_TPM[:, k] = TPM
        return state, mode, his_TPM
コード例 #10
0
ファイル: gym_task.py プロジェクト: zhusj/ZOOpt
    def new_nnmodel(self, layers):
        # initialize NN model as policy
        self.__policy_model = NNModel()
        self.__policy_model.construct_nnmodel(layers)

        return
コード例 #11
0
ファイル: gym_task.py プロジェクト: zhusj/ZOOpt
class GymTask:
    __envir = None  # gym environment
    __envir_name = None  # environment name
    __obser_size = None  # the number of parameters in observation
    __obser_low_bound = []  # the lower bound of parameters in observation
    __obser_up_bound = []  # the upper bound of parameters in observation
    __action_size = None  # the number of parameters in action
    __action_sca = []  # environment action space, specified by gym
    __action_type = []  # the type of action, false means discrete
    __action_low_bound = []  # action lower bound
    __action_up_bound = []  # action upper bound
    __policy_model = None  # policy model, it's a neural network in this example
    __max_step = 0  # maximum stop step
    __stop_step = 0  # the stop step in recent trajectory

    def __init__(self, name):
        self.reset_task()
        self.__envir = gym.make(name)
        self.__envir_name = name
        self.__obser_size = self.__envir.observation_space.shape[0]
        self.__obser_up_bound = []
        self.__obser_low_bound = []
        for i in range(self.__obser_size):
            self.__obser_low_bound.append(
                self.__envir.observation_space.high[i])
            self.__obser_up_bound.append(self.__envir.observation_space.low[i])

        # if the dimension of action space is one
        if isinstance(self.__envir.action_space, Discrete):
            self.__action_size = 1
            self.__action_sca = []
            self.__action_type = []
            self.__action_sca.append(self.__envir.action_space.n)
            self.__action_type.append(False)
        # if action object is Box
        else:
            self.__action_size = self.__envir.action_space.shape[0]
            self.__action_type = []
            self.__action_low_bound = []
            self.__action_up_bound = []
            for i in range(self.__action_size):
                self.__action_type.append(True)
                self.__action_low_bound.append(
                    self.__envir.action_space.low[i])
                self.__action_up_bound.append(
                    self.__envir.action_space.high[i])
        # for i in range(self.__action_size):
        #     self.__action_type.append(False)

    def reset_task(self):
        self.__envir = None
        self.__envir_name = None
        self.__obser_size = None
        self.__obser_low_bound = []
        self.__obser_up_bound = []
        self.__action_type = []
        self.__policy_model = None
        self.__max_step = 0

    # Transform action from neural network into true action.
    def transform_action(self, temp_act):
        action = []
        for i in range(self.__action_size):
            # if action is continue
            if self.__action_type[i]:
                tmp_act = (temp_act[i] + 1) * (
                    (self.__action_up_bound[i] - self.__action_low_bound[i]) /
                    2.0) + self.__action_low_bound[i]
                action.append(tmp_act)
            else:
                sca = 2.0 / self.__action_sca[0]
                start = -1.0
                now_value = start + sca
                true_act = 0
                while now_value <= 1.0:
                    if temp_act[i] <= now_value:
                        break
                    else:
                        now_value += sca
                        true_act += 1
                if true_act >= self.__action_sca[i]:
                    true_act = self.__action_sca[i] - 1
                action.append(true_act)
        if self.__action_size == 1:
            action = action[0]
        return action

    # generate a new model
    def new_nnmodel(self, layers):
        # initialize NN model as policy
        self.__policy_model = NNModel()
        self.__policy_model.construct_nnmodel(layers)

        return

    # generate action from observation using neuron network policy
    def nn_policy_sample(self, observation):
        # action = []
        output = self.__policy_model.cal_output(observation)
        action = self.transform_action(output)
        return action

    # objective function of racos by summation of reward in a trajectory
    def sum_reward(self, solution):
        x = solution.get_x()
        sum_re = 0
        # reset stop step
        self.__stop_step = self.__max_step
        # reset nn model weight
        self.__policy_model.decode_w(x)
        # reset environment
        observation = self.__envir.reset()
        for i in range(self.__max_step):
            action = self.nn_policy_sample(observation)
            observation, reward, done, info = self.__envir.step(action)
            sum_re += reward
            if done:
                self.__stop_step = i
                break
        value = sum_re
        name = self.__envir_name
        # turn the direction for minimization
        if name == 'CartPole-v0' or name == 'MountainCar-v0' or name == 'Acrobot-v1' or name == 'HalfCheetah-v1' \
                or name == 'Humanoid-v1' or name == 'Swimmer-v1' or name == 'Ant-v1' or name == 'Hopper-v1' \
                or name == 'LunarLander-v2' or name == 'BipedalWalker-v2':
            value = -value
        return value

    def get_environment(self):
        return self.__envir

    def get_environment_name(self):
        return self.__envir_name

    def get_observation_size(self):
        return self.__obser_size

    def get_observation_low_bound(self, index):
        return self.__obser_low_bound[index]

    def get_observation_upbound(self, index):
        return self.__obser_up_bound[index]

    def get_action_size(self):
        return self.__action_size

    def get_action_type(self, index):
        return self.__action_type[index]

    def get_stop_step(self):
        return self.__stop_step

    def get_w_size(self):
        return self.__policy_model.get_w_size()

    def set_max_step(self, ms):
        self.__max_step = ms
        return
コード例 #12
0
ファイル: use_rnn.py プロジェクト: oserikov/nn_harmony_np
def main():
    if len(sys.argv) > 1 and any(["--help" in argv for argv in sys.argv[1:]]):
        help_and_exit()

    EPOCHS_NUM, HIDDEN_SIZE, HIDDEN_TYPE, VOCAB_FILE_FILENAME, PLOT_EPOCHS = initialize(
    )

    data, alphabet = load_data(VOCAB_FILE_FILENAME)
    training_data = [(entry[:-1], entry[1:]) for entry in data]

    model = NNModel(alphabet, HIDDEN_SIZE, activation=HIDDEN_TYPE)

    # region train the model
    for epoch_num, epoch_loss in model.train(training_data, EPOCHS_NUM):
        print('\t'.join(
            [f"epoch_num: {epoch_num}", f"epoch_loss: {epoch_loss}"]))

        # region plot loss
        if PLOT_EPOCHS:
            plt.plot([epoch_num], [epoch_loss], 'rx')
            plt.draw()
            plt.pause(0.01)
        # endregion
    # endregion

    hidden_unit_activation_for_char = [{} for unit_idx in range(HIDDEN_SIZE)]
    output_unit_activation_for_char = [{} for unit_idx in range(len(alphabet))]

    for char in alphabet:
        predicted_chars, units_activations, weights = model.sample_with_logging(
            char, 1)
        predicted_char = predicted_chars[0]
        hidden_activations = units_activations["hidden_layer"]
        output_activations = units_activations["output_layer"]

        hidden_units_activations = [
            h_u_act[0] for h_u_act in hidden_activations[0]
        ]
        output_units_activations = [
            o_u_act[0] for o_u_act in output_activations[0]
        ]

        for unit_idx, unit_activation in enumerate(hidden_units_activations):
            hidden_unit_activation_for_char[unit_idx][
                char] = hidden_units_activations[unit_idx]

        for unit_idx, unit_activation in enumerate(output_units_activations):
            output_unit_activation_for_char[unit_idx][
                char] = output_units_activations[unit_idx]

    # region log model state
    for unit_idx, unit_activations in enumerate(
            hidden_unit_activation_for_char):
        for char in alphabet:
            print(f"activation of HIDDEN unit {unit_idx} for char {char}" +
                  '\t' + str(hidden_unit_activation_for_char[unit_idx][char]))

    for unit_idx, unit_activations in enumerate(
            output_unit_activation_for_char):
        for char in alphabet:
            output_char = model.ix_to_char[unit_idx]
            print(
                f"activation of OUTPUT unit {unit_idx} (represents char {output_char}) for char {char}"
                + '\t' + str(output_unit_activation_for_char[unit_idx][char]))

    for hidden_idx, from_input_to_idxth_hidden in enumerate(model.W_ih):
        for char_idx, weight in enumerate(from_input_to_idxth_hidden):
            input_char = model.ix_to_char[char_idx]
            print(
                f"weight INPUT unit {char_idx} (represents char {input_char}) to HIDDEN unit {hidden_idx}"
                + '\t' + str(weight))

    for hidden_tgt_idx, from_hidden_to_idxth_hidden in enumerate(model.W_hh):
        for hidden_src_idx, weight in enumerate(from_hidden_to_idxth_hidden):
            print(
                f"weight HIDDEN unit {hidden_src_idx} to HIDDEN unit {hidden_tgt_idx}"
                + '\t' + str(weight))

    for output_idx, from_hidden_to_idxth_output in enumerate(model.W_ho):
        for hidden_idx, weight in enumerate(from_hidden_to_idxth_output):
            output_char = model.ix_to_char[output_idx]
            print(
                f"weight HIDDEN unit {hidden_idx} to OUTPUT unit {output_idx} (represents char {output_char})"
                + '\t' + str(weight))
コード例 #13
0
ファイル: testing.py プロジェクト: jtaenzer/LV_TechChallenge
def load_model():
    model = NNModel()
    model.load_model("./model_51_file_training.h5")
    print(model.model.summary())
    return model
コード例 #14
0
from nn_model import NNModel, ModelStateLogDTO
from experiment_datasets_creator import ExperimentCreator
from phonology_tool import PhonologyTool

MODEL_FILENAME = "../model_size_2_activation_sigmoid.pkl"
test_data_fn = "data/tur_swadesh.txt"
phonology_features_filename = "data/tur_phon_features.tsv"

model = NNModel.load_model(MODEL_FILENAME)

test_dataset = []
with open(test_data_fn, 'r', encoding="utf-8") as test_data_f:
    for line in test_data_f:
        if all(c in model.alphabet for c in line.strip()):
            test_dataset.append(line.strip())

phonologyTool = PhonologyTool(phonology_features_filename)
experimentCreator = ExperimentCreator(model, test_dataset, phonologyTool)

# front_harmony_dataset
front_harmony_dataset_fn = "front_harmony_dataset.tsv"
front_harmony_dataset = experimentCreator.make_dataset_pretty(
    experimentCreator.front_harmony_dataset())
experimentCreator.save_dataset_to_tsv(front_harmony_dataset,
                                      front_harmony_dataset_fn)

# vov_vs_cons_dataset
vov_vs_cons_dataset_fn = "vov_vs_cons_dataset.tsv"
vov_vs_cons_dataset = experimentCreator.make_dataset_pretty(
    experimentCreator.vov_vs_cons_dataset())
experimentCreator.save_dataset_to_tsv(vov_vs_cons_dataset,
コード例 #15
0
ファイル: robot_executor.py プロジェクト: f2276701/robot
class Robot:
    img_topic = "/camera/color/image_raw"
    joint_topic = "/joint_states"
    joint_service = "/goal_joint_space_path"
    gripper_service = "/goal_tool_control"
    node_name = "robot_executor"
    joint_name = ['joint1', 'joint2', 'joint3', 'joint4','gripper']

    def __init__(self, args, rate=1):
        self.node = rospy.init_node(self.node_name)
        self.joint_sub = JointSubscriber(self.joint_topic)
        self.img_sub = ImageSubscriber(self.img_topic)
        self.rate = rate

        self.gen_motion = GenMotion(self.joint_service, self.gripper_service, rate=rate) 
        self.model = NNModel(args)

        self.app = QApplication([])
        self.window = GoalWidget(self.model.goal_img)

        self.crop_size = ((36, 36 + 260), (250, 250 + 260))

    def online(self):
        output_log = []
        denorm_log = []
        state_log = []

        cur_joint, state = self.gen_motion.sub.get(), None
        raw_input("press key to start online prediction")
        for i in range(550):
            print("Step:", i)
            if i == 30:
                self.goal_change(cur_img)
            cur_img = self.img_sub.get()
            cur_img = cur_img[self.crop_size[0][0]:self.crop_size[0][1], self.crop_size[1][0]:self.crop_size[1][1], :]  
            output, state, denorm_output = self.model.on_predict(cur_joint, cur_img, state)
            self.gen_motion(denorm_output[0, :5].numpy().tolist())
            state_log.append(np.concatenate([s[0].detach().cpu().numpy() for s in state], -1))

            output_log.append(output[0].numpy())
            denorm_log.append(denorm_output[0].numpy())
            cur_joint = denorm_output[0, :5]
            time.sleep(1. / self.rate)

        output_log = np.stack(output_log)
        denorm_log = np.stack(denorm_log)
        state_log = np.stack(state_log)
        return output_log, state_log, denorm_log
        
    def offline(self, target_data):
        output_log = []
        denorm_log = []
        state_log = []

        #cur_joint, state = self.gen_motion.sub.get(), None
        cur_joint, state = target_data[0][:5], None
        raw_input("press key to start offline prediction")
        for i, target in enumerate(target_data):
            print("Step:", i)
            if i == 80:
                self.model.pem()
            img_feature = target[5:]
            output, state, denorm_output = self.model.off_predict(cur_joint, img_feature, state)
            self.gen_motion(denorm_output[0, :5].numpy().tolist())
            state_log.append(np.concatenate([s[0].detach().cpu().numpy() for s in state], -1))

            output_log.append(output[0].numpy())
            denorm_log.append(denorm_output[0].numpy())
            cur_joint = output[0, :5].tolist()
            time.sleep(1. / self.rate)

        output_log = np.stack(output_log)
        denorm_log = np.stack(denorm_log)
        state_log = np.stack(state_log)
        return output_log, state_log, denorm_log
        
    def goal_change(self, cur_img):
        print("===== goal changing =====")
        prev = self.model.goal
        goal_list, pb_list = self.model.gen_goal(cur_img[:, :, ::-1])
        goal_idx = self.window.set_goal(goal_list[0], goal_list[1])
        self.window.show()
        self.app.exec_()
        self.model.goal = pb_list[self.window.decision]
        print("{} -> {}".format(prev.numpy(), self.model.goal.numpy()))
コード例 #16
0
ファイル: main.py プロジェクト: Shanth1984/Mercari-
    train_df["target"] = np.log1p(train_df.price)
    train_df = preprocess(train_df)
    train_df, val_df = train_test_split(train_df,
                                        random_state=123,
                                        train_size=0.99)

    wbm = WordBatchModel()
    wbm.train(train_df)
    predsFM_val = wbm.predict(val_df)

    nnp = NNPreprocessor()
    train_df, WC = nnp.fit_transform(train_df)
    val_df = nnp.transform(val_df)

    nnm = NNModel(train_df=train_df,
                  word_count=WC,
                  batch_size=batch_size,
                  epochs=epochs)
    X_train = nnm.get_nn_data(train_df)
    Y_train = train_df.target.values.reshape(-1, 1)

    X_val = nnm.get_nn_data(val_df)
    Y_val = val_df.target.values.ravel()

    rnn_model = nnm.new_rnn_model(X_train)
    rnn_model.fit(X_train,
                  Y_train,
                  epochs=epochs,
                  batch_size=batch_size,
                  validation_data=(X_val, Y_val),
                  verbose=1)
    Y_val_preds_rnn = rnn_model.predict(X_val, batch_size=batch_size).ravel()
コード例 #17
0
def eval_experiments(path, iterations=20):

    import os
    from nn_model import NNModel
    experiments = {}
    for dr in os.listdir(path):
        full_path = path + dr
        dr_parts = dr.split('_')

        if len(dr_parts) != 8:
            continue
        [name, dataset, padding, classes, angles, fold, performance,
         date] = dr_parts

        experiment_id = '_'.join([name, classes, padding, angles])

        print(dr)
        for i in range(iterations):
            model = None
            try:
                model = NNModel(None,
                                None,
                                None,
                                mode='test',
                                model_state_path=full_path)
            except:
                continue

            scores = model.get_raw_eval()

            if experiment_id not in experiments:
                experiments[experiment_id] = {
                    'actual': [],
                    'original': [],
                    'predicted': [],
                    'actual_labels': scores['actual_labels'],
                    'original_labels': scores['original_labels'],
                    'class_map': scores['class_map'],
                    'accuracy': [],
                    'f1': [],
                    'precision': [],
                    'recall': [],
                }

            if scores['original'] is not None:
                experiments[experiment_id]['original'].extend(
                    scores['original'])

            experiments[experiment_id]['actual'].extend(scores['actual'])
            experiments[experiment_id]['predicted'].extend(scores['predicted'])
            experiments[experiment_id]['accuracy'].append(
                accuracy_score(scores['actual'], scores['predicted']))
            experiments[experiment_id]['f1'].append(
                f1_score(scores['actual'],
                         scores['predicted'],
                         average='macro'))
            experiments[experiment_id]['precision'].append(
                precision_score(scores['actual'],
                                scores['predicted'],
                                average='macro'))
            experiments[experiment_id]['recall'].append(
                recall_score(scores['actual'],
                             scores['predicted'],
                             average='macro'))

    print("\n\n\n=============== Final report ===============")
    for exp_id in experiments:
        print_experiment(exp_id, experiments[exp_id])
    print("\n\n\n======================================")

    return experiments
コード例 #18
0
 def __init__(self, name, acc, trans, noits, meats, u0, v0_prob, wk_prob,
              vk_prob, total_frame, param):
     super().__init__(name, acc, trans, noits, meats, u0, v0_prob, wk_prob,
                      vk_prob, total_frame)
     self.model = NNModel(param)
コード例 #19
0
# Using n_files/2 as the min_freq is a rule of thumb I determined empirically to keep the training time reasonable
txtdata = data_interp.simplify_text_data(txtdata, min_freq=n_files / 2)
# Set the number of words to keep based on the number of words that appear more often min_feq
vocab = data_interp.set_num_words(txtdata, min_freq=n_files / 2)
vocab_size = len(vocab) + 1
# Convert the data to sequences of integers with some maximum length
max_length, sequences = data_interp.training_data_to_padded_sequences(
    txtdata, max_len=15, shuffle_data=True)
# Break up the sequences into input (sequence of n words) and output (single word to test against)
input_data, output = sequences[:, :-1], sequences[:, -1]
output = to_categorical(output, num_classes=vocab_size)

# Save the tokenizer for later use, in case we randomized the training data
# If the training data was randomized we will need to know the words and word_index later for testing
tokenizer_json = data_interp.tokenizer.to_json()
with open("./tokenizer_%s_file_training.json" % n_files, "w",
          encoding="utf-8") as jsonf:
    jsonf.write(dumps(tokenizer_json, ensure_ascii=False))

# Prepare the model
model = NNModel()
# Input layer should have max_length - 1 neurons, output layer should have one neuron per word token
# Hidden layer size determined by the 2/3*(input layer + output layer) rule of thumb
model.prepare_model(max_length - 1,
                    vocab_size,
                    hidden_layer_size=int(
                        (vocab_size + max_length - 1) * 2 / 3))
# Fit on training data
model.fit_model(input_data, output)
# Save model, can be loaded later for testing without re-training
model.save_model("./model_%s_file_training.h5" % str(n_files))