def count_polY_accuracy(model, num_batchs, sess, save_dir): symbol = Symbol(hp.vocab_path, hp.poly_dict_path) with open(hp.poly_dict_path, 'r') as f: POLY_DICT = json.load(f) test_poly_dict = {} for key, value in POLY_DICT.items(): POLY_DICT[key] = sorted(value) test_poly_dict[key] = np.zeros((len(value), len(value))) for i in range(num_batchs): model_inputs, model_pre, mask, poly_mask, model_correct_pred, model_acc, model_outputs, model_targets, model_target_seq \ = sess.run([model.inputs, model.pred, model.mask, model.poly_mask, model.correct_pred, model.accuracy, model.outputs, model.targets, model.target_seq]) print("model_pred", model_pre) print("model_accruacy", model_acc) print("model_outputs", model_outputs) print("model_mask", mask) print("model_poly_mask", poly_mask) print("model_targets", model_targets) print("model_target_seq", model_target_seq) print("model_correct_pred", model_correct_pred) for pred_poly_seq, ta, model_input in zip(model_pre, model_target_seq, model_inputs): pred_poly = symbol.sequence_to_label(pred_poly_seq) target_poly = symbol.sequence_to_label(ta) word_value = symbol.input_to_word_value(model_input) print(pred_poly) print(target_poly) print(word_value) if len(pred_poly_seq) != len(ta): print("length not equal") print("-----------------") continue for p, t, w in zip(pred_poly, target_poly, word_value): if t == "-": continue else: i = POLY_DICT[w].index(p) j = POLY_DICT[w].index(t) test_poly_dict[w][i, j] += 1 print(w) print(test_poly_dict[w]) print("Model Accuracy: {}".format(model_acc)) poly_accuracy_out = open(os.path.join(save_dir, "accuracy.out"), "w") poly_count = open(os.path.join(save_dir, "poly_count.out"), "w") for key, value in test_poly_dict.items(): accuracy = np.trace(value) / np.sum(value) poly_accuracy_out.writelines("{}\t{}\t{}\t{}\n".format( key, int(np.sum(value)), int(np.trace(value)), accuracy)) poly_count.writelines(key + "\n") for i, poly_i in enumerate(POLY_DICT[key]): for j, poly_j in enumerate(POLY_DICT[key]): poly_count.write("{}->{}:{}\n".format(poly_i, poly_j, int(value[i, j])))
def test_symbol(vocab_path, data_path, poly_dict_path): symbol = Symbol(vocab_path, poly_dict_path) with open(data_path, "r") as json_file: data = json.load(json_file) metadata = list(zip(data["features"], data["labels"])) for meta in metadata: input_data = np.asarray(symbol.feature_to_sequence(meta[0]), dtype=np.float32) target_data = np.asarray(symbol.label_to_sequence(meta[1]), dtype=np.float32) poly_mask = symbol.poly_mask(meta[0]) print(meta[0]) print(meta[1]) print("input_data" + "=" * 50) print(input_data) print("target_data" + "=" * 50) print(target_data) print("poly_mask" + "=" * 50) print(poly_mask) print("sequence to feature value " + "=" * 50) print(symbol.input_to_word_value(input_data))