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
def load_model(): model = NNModel() model.load_model("./model_51_file_training.h5") print(model.model.summary()) return model
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,