def main(): parser = argparse.ArgumentParser() parser.add_argument('--save', '-s', type=str, default='', help='save name to test') args = parser.parse_args() save = args.save # Prepare logger # - create logger with 'spam_application' logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) # - create file handler which logs even debug messages fh = logging.FileHandler('test_result/{}.log'.format(save.split('/')[1]), mode='w') fh.setLevel(logging.INFO) # - create formatter and add it to the handlers formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) # - add the handlers to the logger logger.addHandler(fh) # Parse save name hp_dict = parse_hyparams(save.split('/')[1]) # Prepare datasets train, dev, test = reader.read(hp_dict['x'], hp_dict['y']) # Prepare linearizer and converter global l, c # Model setup if hp_dict['r'] == 0: l = linearizer.linearize_tree c = converter.convert params = { 'n_embed': hp_dict['x'] + 3, 'd': hp_dict['d'], 'k': hp_dict['k'] } if hp_dict['m'] == 'n': model = RNN(**params) elif hp_dict['m'] == 't': model = RNTN(**params) elif hp_dict['m'] == 'd': model = RNTNd(**params) elif hp_dict['m'] == 'c': model = RNTNc(**params) elif hp_dict['r'] == 1: l = linearizer.linearize_tree_relational c = converter.convert_relational params = { 'n_embed': hp_dict['x'] * 2, 'd': hp_dict['d'], 'k': hp_dict['k'] } if hp_dict['m'] == 'rn': model = RNNr(**params) elif hp_dict['m'] == 'rt': params['mp'] = hp_dict['v'] model = RNTNr(**params) elif hp_dict['m'] == 'rd': params['mp'] = hp_dict['v'] model = RNTNrd(**params) elif hp_dict['m'] == 'rc': params['mp'] = hp_dict['v'] model = RNTNrc(**params) elif hp_dict['m'] == 'rs': model = RNTNrs(**params, p=hp_dict['p'], q=hp_dict['q']) print(save) serializers.load_hdf5(save, model) model = L.Classifier(model) model.to_cpu() # Dev logger.info('---dev---') accuracy = get_accuracy(model, dev) logger.info('dev: {}'.format(accuracy)) # Test logger.info('---test---') for length, a_bin in enumerate(test[1:]): accuracy = get_accuracy(model, a_bin) logger.info('test {} : {}'.format(length + 1, accuracy))
def main(): """ parser = argparse.ArgumentParser() parser.add_argument('--save', '-s', type=str, default='', help='save name to test') args = parser.parse_args() """ # Parse save file name path = './trained_model_12-11' saves = os.listdir(path) for save in saves: # Prepare logger # - create logger with 'spam_application' logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) # - create file handler which logs even debug messages fh = logging.FileHandler('test_result/{}.log'.format(save), mode='w') fh.setLevel(logging.INFO) # - create formatter and add it to the handlers formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) # - add the handlers to the logger logger.addHandler(fh) # Parse save name hp_dict = parse_hyparams(save) # Prepare datasets train, dev, test = reader.read(hp_dict['x'], hp_dict['y']) # Prepare linearizer and converter global l, c # Model setup if hp_dict['r'] == 0: l = linearizer.linearize_tree c = converter.convert params = {'n_embed': hp_dict['x'] + 3, 'd': hp_dict['d'], 'k': hp_dict['k']} if hp_dict['m'] == 'n': model = RNN(**params) elif hp_dict['m'] == 't': model = RNTN(**params) elif hp_dict['m'] == 'd': model = RNTNd(**params) elif hp_dict['m'] == 'c': model = RNTNc(**params) elif hp_dict['r'] == 1: l = linearizer.linearize_tree_relational c = converter.convert_relational params = {'n_embed': hp_dict['x'] * 2, 'd': hp_dict['d'], 'k': hp_dict['k']} if hp_dict['m'] == 'rn': model = RNNr(**params) elif hp_dict['m'] == 'rt': model = RNTNr(**params) elif hp_dict['m'] == 'rd': model = RNTNrd(**params) elif hp_dict['m'] == 'rc': model = RNTNrc(**params) elif hp_dict['m'] == 'rs': model = RNTNrs(**params, p=hp_dict['p'], q=hp_dict['q']) print(save) serializers.load_hdf5(path + '/' + save, model) model = L.Classifier(model) model.to_cpu() m = hp_dict['m'] w = hp_dict['w'] i = hp_dict['i'] - 1 p = hp_dict['p'] # Dev logger.info('---dev---') accuracy = get_accuracy(model, dev) logger.info('dev: {}'.format(accuracy)) if m == 'rs': results[p][w][0][i] = accuracy else: results[m][w][0][i] = accuracy # Test logger.info('---test---') for length, a_bin in enumerate(test[1:]): accuracy = get_accuracy(model, a_bin) logger.info('test {} : {}'.format(length + 1, accuracy)) if hp_dict['m'] == 'rs': results[p][w][length + 1][i] = accuracy else: results[m][w][length + 1][i] = accuracy logger.removeHandler(fh) logger.setLevel(logging.INFO) fh = logging.FileHandler('test_result/final.log', mode='w') fh.setLevel(logging.INFO) fh.setFormatter(formatter) logger.addHandler(fh) for m in results.keys(): logger.info('') logger.info('m: {}'.format(m)) for w in results[m].keys(): logger.info('w: {}---------'.format(w)) for j, accuracies in enumerate(results[m][w]): print(accuracies) std = np.std(accuracies) mean = np.mean(accuracies) if j == 0: logger.info('dev: {} ({})'.format(mean, std)) else: logger.info('test {} : {} ({})'.format(j, mean, std)) with open('final.p', 'wb') as f: pickle.dump(results, f)