예제 #1
0
    def train(self):
        print "Loading the data sets...",
        train_data = Data(data_source=self.data_path,
                          alphabet=self.alphabet,
                          no_of_classes=self.no_of_classes,
                          l0=self.l0,
                          is_polarity=self.is_polarity)

        train_data.loadData()
        X_train, y_train = train_data.getAllData()
        print "Loadded"

        print "Training ..."
        self.model.fit(X_train,
                       y_train,
                       nb_epoch=5000,
                       batch_size=128,
                       validation_split=0.2,
                       callbacks=EarlyStopping)

        model_name = "cnn_model5.h5"
        if self.is_polarity:
            model_name = "cnn_model2.h5"
        self.model.save(model_name)

        print "Done!."
    def setUp(self):
        tf.reset_default_graph()

        self.data = Data("train", hparams_unit)
        self.hook = self.data.switch_dataset_hook
        features, labels = self.data.input_fn()
        self.model = Model(
            "train",
            features,
            labels,
            vocab_size=Tokenizer.get_tokenizer(hparams_unit).vocab_size,
            hparams=hparams_unit)

        # init ops
        self.model.predictions
        # self.model.loss
        self.model.train_op
        self.model.eval_metrics

        self.all_scopes = ["Encoder", "Decoder", "Classifier", "embedding"]
        self.loss_to_scope = [  # loss_name => scopes that should have a gradient
            ("loss", self.all_scopes), ("vae_loss", ["Encoder", "Decoder"]),
            ("generator_loss", ["Decoder"]),
            ("recon_loss", ["Encoder", "Decoder"]), ("kl_loss", ["Encoder"]),
            ("attr_preserve_loss", ["Decoder"]),
            ("disentanglement_loss", ["Decoder"]),
            ("classify_synth_loss", ["Classifier"])
        ]
        self.stage_changes_scopes = {
            "vae_uncond": ["Encoder", "Decoder", "embedding"],
            "classify": ["Classifier", "embedding"],
            "vae_cond_lab": ["Encoder", "Decoder", "embedding"],
            "classify_synth": ["Classifier", "embedding"],
        }
        self.fails = []
예제 #3
0
def main(config):
    # load vocabs
    vocab_words = load_vocab(config.words_filename)
    vocab_mor_tags = load_vocab(config.mor_tags_filename)
    vocab_tags = load_vocab(config.tags_filename)
    vocab_chars = load_vocab(config.chars_filename)
    vocab_lex_tags = load_vocab(config.lex_tags_filename)

    # get processing functions
    processing_word = get_processing_word(vocab_words,
                                          vocab_chars,
                                          lowercase=True,
                                          chars=config.chars)
    processing_mor_tag = get_processing_word(vocab_mor_tags, lowercase=False)
    processing_tag = get_processing_word(vocab_tags, lowercase=False)
    processing_lex_tag = get_processing_word(vocab_lex_tags, lowercase=False)

    # get pre trained embeddings
    embeddings = get_trimmed_glove_vectors(config.trimmed_filename)

    # create dataset
    dev = Data(config.dev_filename, processing_word, processing_mor_tag,
               processing_lex_tag, processing_tag, config.max_iter)
    test = Data(config.test_filename, processing_word, processing_mor_tag,
                processing_lex_tag, processing_tag, config.max_iter)
    train = Data(config.train_filename, processing_word, processing_mor_tag,
                 processing_lex_tag, processing_tag, config.max_iter)

    cnn_model = CnnLstmCrfModel(config,
                                embeddings,
                                ntags=len(vocab_tags),
                                nchars=len(vocab_chars))
    cnn_model.build()
    cnn_model.train(train, dev, vocab_tags)
    cnn_model.evaluate(test, vocab_tags)
예제 #4
0
def main(_):

    for k, v in FLAGS.__flags.items():
        print("{}={}".format(k, v))

    if not os.path.exists(FLAGS.prepro_dir):
        os.makedirs(FLAGS.prepro_dir)

    if FLAGS.prepro:
        train_data, train_label, test_data, test_label = data_utils.gen_data(
            train_size=100000, test_size=1000, seq_length=FLAGS.seq_length)
    else:
        train_data, train_label = cPickle.load(open("train.dat", 'rb'))
        test_data, test_label = cPickle.load(open("test.dat", 'rb'))

    print("training data size: {}".format(train_data.shape))
    print("testing data size: {}".format(test_data.shape))

    data = Data(train_data, train_label, test_data, test_label)

    model = Toy(data, FLAGS)

    model.build_model()

    model.train()
예제 #5
0
def build_data(config):
    """
    Procedure to build data

    Args:
        config: defines attributes needed in the function
    Returns:
        creates vocab files from the datasets
        creates a npz embedding file from trimmed glove vectors
    """
    processing_word = get_processing_word(lowercase=True)

    # Generators
    dev   = Data(config.dev_filename, processing_word)
    test  = Data(config.test_filename, processing_word)
    train = Data(config.train_filename, processing_word)

    # Build Word and Tag vocab
    vocab_words, vocab_mor_tags, vocab_tags = get_vocabs([train, dev, test])
    vocab_glove = get_glove_vocab(config.glove_filename)

    vocab = vocab_words & vocab_glove
    vocab.add(UNK)
    vocab.add(NUM)

    vocab_mor_tags.add(UNK)
    
    # Save vocab
    write_vocab(vocab, config.words_filename)
    write_vocab(vocab_mor_tags, config.mor_tags_filename)
    write_vocab(vocab_tags, config.tags_filename)

    # Trim GloVe Vectors
    vocab = load_vocab(config.words_filename)
    export_trimmed_glove_vectors(vocab, config.glove_filename, 
                                config.trimmed_filename, config.dim)

    # Build and save char vocab
    train = Data(config.train_filename)
    
    # should change this ~!!
    vocab_chars = get_char_vocab(train)
    write_vocab(vocab_chars, config.chars_filename)
예제 #6
0
def main(arguments):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-ut',
                        '--use_tree',
                        help="use tree feature or not  ",
                        action='store_true',
                        default=False)
    parser.add_argument('-ui',
                        '--use_ilp',
                        help="use ilp  or not  ",
                        action='store_true',
                        default=False)
    parser.add_argument('-lr',
                        '--learn_rate',
                        help=" ",
                        type=float,
                        default=0.001)
    parser.add_argument('-ep',
                        '--epochs',
                        type=int,
                        help="training epochs  ",
                        default=15)
    parser.add_argument('-hs',
                        '--hidden_size',
                        help="hidden layer size",
                        default=100)
    parser.add_argument('-ln',
                        '--num_layers',
                        help="stack lstm number",
                        default=3)
    parser.add_argument('-wes',
                        '--word_embed_size',
                        help="word vect size",
                        default=50)
    parser.add_argument('-bs', '--batch_size', help=" ", default=30)
    parser.add_argument('-mn',
                        '--model_name',
                        help="model saved path",
                        default='model')
    parser.add_argument('-md', '--mode', help="train or test", default='train')
    args = parser.parse_args(arguments)
    data = Data('./data/data_sample.bin', './data/vocab_sample.bin',
                './data/word_embed_weight_sample.bin', args.batch_size)
    if not os.path.exists('./output/'):
        os.makedirs('./output/')
    model = BiLstm(args, data, ckpt_path='./output/')
    if args.mode == 'train':
        model.train(data)
        sess = model.restore_last_session()
        model.predict(data, sess)
    if args.mode == 'test':
        sess = model.restore_last_session()
        model.predict(data, sess)
예제 #7
0
파일: main.py 프로젝트: zp1018/FedReID
def train():
    args = parser.parse_args()
    print(args)

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")

    set_random_seed(1)

    data = Data(args.datasets, args.data_dir, args.batch_size, args.erasing_p,
                args.color_jitter, args.train_all)
    data.preprocess()

    clients = {}
    for cid in data.client_list:
        clients[cid] = Client(cid, data, device, args.project_dir,
                              args.model_name, args.local_epoch, args.lr,
                              args.batch_size, args.drop_rate, args.stride)

    server = Server(clients, data, device, args.project_dir, args.model_name,
                    args.num_of_clients, args.lr, args.drop_rate, args.stride,
                    args.multiple_scale)

    dir_name = os.path.join(args.project_dir, 'model', args.model_name)
    if not os.path.isdir(dir_name):
        os.mkdir(dir_name)

    print("=====training start!========")
    rounds = 800
    for i in range(rounds):
        print('=' * 10)
        print("Round Number {}".format(i))
        print('=' * 10)
        server.train(i, args.cdw, use_cuda)
        save_path = os.path.join(dir_name, 'federated_model.pth')
        torch.save(server.federated_model.cpu().state_dict(), save_path)
        if (i + 1) % 10 == 0:
            server.test(use_cuda)
            if args.kd:
                server.knowledge_distillation(args.regularization)
                server.test(use_cuda)
        server.draw_curve()
예제 #8
0
파일: main.py 프로젝트: woshizhpy/GAN
def main(_):
    print("Parameters: ")
    log_writeln(outf, "Parameters: ")
    for k, v in FLAGS.__flags.items():
        print("{} = {}".format(k, v))
        log_writeln(outf, "{} = {}".format(k, v))
    if not os.path.exists("./prepro/"):
        os.makedirs("./prepro/")

    if FLAGS.prepro:
        img_feat, tags_idx, a_tags_idx, vocab_processor = data_utils.load_train_data(
            FLAGS.train_dir, FLAGS.tag_path, FLAGS.prepro_dir, FLAGS.vocab)
    else:
        img_feat = cPickle.load(
            open(os.path.join(FLAGS.prepro_dir, "img_feat.dat"), 'rb'))
        tags_idx = cPickle.load(
            open(os.path.join(FLAGS.prepro_dir, "tag_ids.dat"), 'rb'))
        a_tags_idx = cPickle.load(
            open(os.path.join(FLAGS.prepro_dir, "a_tag_ids.dat"), 'rb'))
        vocab_processor = VocabularyProcessor.restore(FLAGS.vocab)
    img_feat = np.array(img_feat, dtype='float32') / 127.5 - 1.
    test_tags_idx = data_utils.load_test(FLAGS.test_path, vocab_processor)

    log_writeln(outf, "Image feature shape: {}".format(img_feat.shape))
    log_writeln(outf, "Tags index shape: {}".format(tags_idx.shape))
    log_writeln(outf,
                "Attribute Tags index shape: {}".format(a_tags_idx.shape))
    log_writeln(outf,
                "Vocab size: {}".format(len(vocab_processor._reverse_mapping)))
    log_writeln(
        outf,
        "Vocab max length: {}".format(vocab_processor.max_document_length))

    data = Data(img_feat, tags_idx, a_tags_idx, test_tags_idx, FLAGS.z_dim,
                vocab_processor)

    Model = getattr(sys.modules[__name__], FLAGS.model)
    print(Model)
    log_writeln(outf, Model)
    model = Model(data, vocab_processor, FLAGS)

    model.build_model()

    model.train()
예제 #9
0
    def __init__(self, config=config_reader()):
        """
        :Desc: read model param
        :param config:
        """
        self.rnn_mode = config['rnn_mode']
        self.batch_size = config['batch_size'] - 2
        self.embedding_dim = config['embedding_dim']
        self.num_layers = config['num_layers']
        self.num_units = config['num_utils']
        self.learning_rate = config['learning_rate']
        self.max_epoch = config['max_epoch']
        self.keep_prob = config['keep_prob']

        self.data = Data()
        self.vocab = self.data.vocab
        self.chunk_size = self.data.chunk_size
        self.global_step = tf.Variable(0, trainable=False, name='global_step')
        self.increment_global_step_op = tf.assign(self.global_step,
                                                  self.global_step + 1)
def experiment_fn(run_config, hparams):
    model_class = get_model_class(run_config)
    estimator = tf.estimator.Estimator(model_fn=get_model_fn(model_class),
                                       params=hparams,
                                       config=run_config)

    train_data = Data("train", hparams)
    eval_data = Data("eval", hparams)

    return tf.contrib.learn.Experiment(
        estimator=estimator,
        train_input_fn=train_data.get_input_fn_for(model_class),
        eval_input_fn=eval_data.get_input_fn_for(model_class),
        train_steps=hparams.train_steps,
        eval_steps=hparams.eval_steps,
        min_eval_frequency=1,  # every time checkpoint is created
        # train_monitors=[train_data.switch_dataset_hook],
        # eval_hooks=[eval_data.switch_dataset_hook],
    )
예제 #11
0
import time
import tensorflow as tf
import config
import os
from data_utils import Data
from char_cnn import CharConvNet

if __name__ == '__main__':
    print('start...')
    tf.set_random_seed(0.18)
    exec(open('config.py').read())  # 加载配置文件
    print(config.model.th)
    print('end...')
    print('Loading data...')
    train_data = Data(max_length=config.max_length,
                      batch_size=config.batch_size,
                      no_of_classes=config.no_of_classes)
    train_data.build_model_dataset('train')
    dev_data = Data(max_length=config.max_length,
                    batch_size=config.batch_size,
                    no_of_classes=config.no_of_classes)
    dev_data.build_model_dataset('dev')
    '''epoch计算'''
    num_batches_per_epoch = int(
        train_data.get_length() / config.batch_size) + 1
    num_batch_dev = dev_data.get_length()
    print('Loaded')

    print('Training')

    with tf.Graph().as_default():
예제 #12
0
if __name__ == "__main__":
    # Load configurations from file
    config = json.load(open('config.json'))
    FLAGS.epochs = int(FLAGS.epochs)
    # import data from external APIs
    if FLAGS.imports == "Y":
        APIdataclass = APIdata()
        dps = APIdataclass.inaturalistAPI()
        APIdataclass.weatherAPI(dps)

    classes = np.genfromtxt("data/classes.txt", dtype=np.str, delimiter='\n')

    #################################################
    # Load training data
    training_data = Data(data_source=config['data']['training_data_source'],
                         num_of_classes=len(classes))
    training_data.load_data()
    training_inputs, training_labels = training_data.get_all_data()

    # Load validation data for testing
    validation_data = Data(
        data_source=config['data']['validation_data_source'],
        num_of_classes=len(classes))
    raw_test_data = validation_data.load_data()
    validation_inputs, validation_labels = validation_data.get_all_data()

    # Train with all data for a production model
    if FLAGS.useall == 'Y':
        training_inputs = np.concatenate((training_inputs, validation_inputs),
                                         axis=0)
        training_labels = np.concatenate((training_labels, validation_labels),
예제 #13
0
# Set Variables
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{}"
input_size = 400
num_of_classes = len(labels)

# Set paths of the sets
train_data_path = "./train_set.csv"
valid_data_path = "./validation_set.csv"
test_data_path = "./test_set.csv"

# Model's name to be saved
save_model_file = "textModel.h5"

# Load training data
training_data = Data(data_source=train_data_path,
                     alphabet=alphabet,
                     input_size=input_size,
                     num_of_classes=num_of_classes)
training_data.load_data()
training_inputs, training_labels, _ = training_data.get_all_data()

# Load validation data
validation_data = Data(data_source=valid_data_path,
                       alphabet=alphabet,
                       input_size=input_size,
                       num_of_classes=num_of_classes)
validation_data.load_data()
validation_inputs, validation_labels, _ = validation_data.get_all_data()

# Load test data
test_data = Data(data_source=test_data_path,
                 alphabet=alphabet,
예제 #14
0
optimizer = Adam()

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

print("Built")

print("Loading the data sets..."),

from data_utils import Data

train_data = Data(data_source=config.train_data_source,
                  alphabet=config.alphabet,
                  l0=config.l0,
                  batch_size=0,
                  no_of_classes=config.num_of_classes)

train_data.loadData()

X_train, y_train = train_data.getAllData()

# dev_data = Data(data_source = config.dev_data_source,
#                       alphabet = config.alphabet,
#                       l0 = config.l0,
#                       batch_size = 0,
#                       no_of_classes = config.num_of_classes)
#
#
# dev_data.loadData()
예제 #15
0
from data_utils import Data
from models.char_cnn_zhang import CharCNNZhang
from models.char_cnn_kim import CharCNNKim
from models.char_tcn import CharTCN

tf.flags.DEFINE_string("model", "char_cnn_zhang", "Specifies which model to use: char_cnn_zhang or char_cnn_kim")
FLAGS = tf.flags.FLAGS
FLAGS._parse_flags()

if __name__ == "__main__":
    # Load configurations
    config = json.load(open("config.json"))
    # Load training data
    training_data = Data(data_source=config["data"]["training_data_source"],
                         alphabet=config["data"]["alphabet"],
                         input_size=config["data"]["input_size"],
                         num_of_classes=config["data"]["num_of_classes"])
    training_data.load_data()
    training_inputs, training_labels = training_data.get_all_data()
    # Load validation data
    validation_data = Data(data_source=config["data"]["validation_data_source"],
                           alphabet=config["data"]["alphabet"],
                           input_size=config["data"]["input_size"],
                           num_of_classes=config["data"]["num_of_classes"])
    validation_data.load_data()
    validation_inputs, validation_labels = validation_data.get_all_data()

    # Load model configurations and build model
    if FLAGS.model == "kim":
        model = CharCNNKim(input_size=config["data"]["input_size"],
                           alphabet_size=config["data"]["alphabet_size"],
예제 #16
0
parser.add_argument(
    '--model',
    type=str,
    default='char_cnn_zhang',
    help='Specifies which model to use: char_cnn_zhang or char_cnn_kim')
FLAGS = parser.parse_args()

if __name__ == "__main__":
    # Load configurations
    config = json.load(open("config.json"))
    # Change the model name
    config['model'] = config[FLAGS.model]  # equal to config['char_cnn_hang']

    # Load training data
    training_data = Data(data_source=config["data"]["training_data_source"],
                         alphabet=config["data"]["alphabet"],
                         input_size=config["data"]["input_size"],
                         num_of_classes=config["data"]["num_of_classes"])
    training_data.load_data()
    training_inputs, training_labels = training_data.get_all_data()
    # Load validation data
    validation_data = Data(
        data_source=config["data"]["validation_data_source"],
        alphabet=config["data"]["alphabet"],
        input_size=config["data"]["input_size"],
        num_of_classes=config["data"]["num_of_classes"])
    validation_data.load_data()
    validation_inputs, validation_labels = validation_data.get_all_data()

    # Load model configurations and build model
    if FLAGS.model == "kim":
        model = CharCNNKim(
예제 #17
0
optimizer = Adam()

model.compile(optimizer=optimizer, loss='categorical_crossentropy')

	
print("Built")


print("Loading the data sets...")

from data_utils import Data

train_data = Data(data_source = config.train_data_source, 
                     alphabet = config.alphabet,
                     l0 = config.l0,
                     batch_size = 0,
                     no_of_classes = config.num_of_classes)

train_data.loadData()

X_train, y_train = train_data.getAllData()

dev_data = Data(data_source = config.dev_data_source,
                      alphabet = config.alphabet,
                      l0 = config.l0,
                      batch_size = 0,
                      no_of_classes = config.num_of_classes)


dev_data.loadData()
예제 #18
0
파일: train.py 프로젝트: hane1818/Test
def train():
    # Prepare data
    print("Prepare training data...")
    train_data = Data("training")
    train_data_loader = DataLoader(train_data,
                                   batch_size=args.batch_size,
                                   shuffle=True)
    print("Prepare validation data...")
    validation_data = Data("validation")
    validation_data_loader = DataLoader(validation_data,
                                        batch_size=args.batch_size,
                                        shuffle=True)

    # Build model
    print("Build model...")
    model = Generator()
    if torch.cuda.is_available() and args.use_gpu:
        model.cuda()

    loss_fn = RewardWeightedCrossEntropyLoss(
    ) if args.rl else nn.CrossEntropyLoss()
    #optimizer = SGD(model.parameters(), lr=args.learning_rate_G*10, momentum=0.9)
    optimizer = Adam(model.parameters(), lr=args.learning_rate_G)

    writer = SummaryWriter(args.train_dir)

    # Start training
    for epoch in range(args.train_epoches):
        print("Epoch %d: Start training" % epoch)
        train_losses = 0
        val_losses = 0
        for i_batch, sampled_batch in enumerate(train_data_loader):
            optimizer.zero_grad()
            _, doc, label, mlabel, rewards, weights, _ = sampled_batch
            probs, logits = model(doc)
            if args.rl:
                loss = loss_fn(
                    probs.contiguous().view(-1, args.target_label_size),
                    mlabel.view(-1), rewards.view(-1), weights.view(-1))
            else:
                loss = loss_fn(
                    probs.contiguous().view(-1, args.target_label_size),
                    label.view(-1))
            train_losses += loss.data * probs.size(0)
            loss.backward()
            nn.utils.clip_grad_norm_(model.parameters(), 0.5)
            optimizer.step()
        train_losses /= len(train_data)

        # Save model
        print("Epoch %d: Saving model" % epoch)
        torch.save(model.state_dict(),
                   os.path.join(args.train_dir, "epoch-%d.model" % epoch))

        # Validate model
        print("Epoch %d: Validate model performance" % epoch)
        for i_batch, sampled_batch in enumerate(validation_data_loader):
            docname, doc, label, mlabel, rewards, weights, _ = sampled_batch
            probs, logits = model(doc)
            if args.rl:
                val_losses += loss_fn(
                    probs.contiguous().view(-1, args.target_label_size),
                    mlabel.view(-1), rewards.view(-1),
                    weights.view(-1)).data * probs.size(0)
            else:
                val_losses += loss_fn(
                    probs.contiguous().view(-1, args.target_label_size),
                    label.view(-1)).data * probs.size(0)
            validation_data.write_prediction_summary(docname, logits,
                                                     "epoch-%d.model" % epoch)
        val_losses /= len(validation_data)

        # ROUGE Evaluation
        system_dir = os.path.join(
            args.train_dir,
            '.'.join(["epoch-%d.model" % epoch, "validation", "summary"]))
        model_dir = os.path.join(args.gold_summary_directory,
                                 "gold-%s-validation-orgcase" % args.data_mode)
        r1, r2, rl = calc_rouge_score(system_dir, model_dir)
        print("Epoch %d: ROUGE Score(1, 2, L) %.2f %.2f %.2f" %
              (epoch, r1 * 100, r2 * 100, rl * 100))

        # Write to Summary
        writer.add_scalar('training_loss', train_losses, epoch)
        writer.add_scalar('validation_loss', val_losses, epoch)
        writer.add_scalars('rouge_score', {
            'rouge-1': r1,
            'rouge-2': r2,
            'rouge-L': rl
        }, epoch)

    writer.export_scalars_to_json('./%s/all_scalars.json' % args.train_dir)
    writer.close()
예제 #19
0
def main(_):
    print("\nParameters: ")
    for k, v in sorted(FLAGS.__flags.items()):
        print("{} = {}".format(k, v))

    if not os.path.exists("./prepro/"):
        os.makedirs("./prepro/")

    if FLAGS.eval:
        print("Evaluation...")
        feats, test_id = data_utils.load_test_data(FLAGS.test_id,
                                                   FLAGS.test_dir)
        vocab_processor = VocabularyProcessor.restore(FLAGS.vocab)

        config = tf.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True

        with tf.Session(config=config) as sess:
            model = load_model(sess, FLAGS.checkpoint_file, vocab_processor)
            sentences = greedy_inference(sess, model, feats, vocab_processor)
            # sentences = beam_search(sess, model, feats, vocab_processor)
            ans = []
            for idx, sentence in enumerate(sentences):
                ans.append({"caption": sentence, "id": test_id[idx]})
            json.dump(ans, open(FLAGS.output, 'w'))

    else:
        if FLAGS.prepro:
            print("Start preprocessing data...")
            vocab_processor, train_dict = data_utils.load_text_data(
                train_lab=FLAGS.train_lab,
                prepro_train_p=FLAGS.prepro_train,
                vocab_path=FLAGS.vocab)
            print("Vocabulary size: {}".format(
                len(vocab_processor._reverse_mapping)))

            print("Start dumping word2vec matrix...")
            w2v_W = data_utils.build_w2v_matrix(vocab_processor,
                                                FLAGS.w2v_data,
                                                FLAGS.vector_file,
                                                FLAGS.embedding_dim)

        else:
            train_dict = cPickle.load(open(FLAGS.prepro_train, 'rb'))
            vocab_processor = VocabularyProcessor.restore(FLAGS.vocab)
            w2v_W = cPickle.load(open(FLAGS.w2v_data, 'rb'))

        print("Start generating training data...")
        feats, encoder_in_idx, decoder_in = data_utils.gen_train_data(
            FLAGS.train_dir, FLAGS.train_lab, train_dict)
        print("Start generating validation data...")
        v_encoder_in, truth_captions = data_utils.load_valid(
            FLAGS.valid_dir, FLAGS.valid_lab)

        t_encoder_in = None
        files = None
        if FLAGS.task_dir != None:
            t_encoder_in, files = data_utils.load_task(FLAGS.task_dir)

        print('feats size: {}, training size: {}'.format(
            len(feats), len(encoder_in_idx)))
        print(encoder_in_idx.shape, decoder_in.shape)
        print(v_encoder_in.shape, len(truth_captions))

        data = Data(feats, encoder_in_idx, decoder_in, v_encoder_in,
                    truth_captions, t_encoder_in, files)

        model = CapGenModel(data, w2v_W, vocab_processor)

        model.build_model()

        model.train()
예제 #20
0
def train():
    args = parser.parse_args()
    print(args)
    if args.clustering:
        clu = "clu"
    else:
        clu = "Nclu"

    if args.cdw:
        cdw = "cdw"
    else:
        cdw = "Ncdw"
    if args.kd:
        kd = "kd"
    else:
        kd = "Nkd"
    if args.regularization:
        reg = "reg"
    else:
        reg = "Nreg"

    kd_method = args.kd_method
    assert (kd_method == 'whole' or kd_method == 'cluster')
    if args.clustering:
        if args.clustering_method == "kmeans":
            cluster_description = "kmeans_{}".format(args.n_cluster)
        else:
            cluster_description = "finch_{}".format(args.max_distance)
    else:
        cluster_description = "No_cluster"

    cpk_dir = "checkpoints/{}_{}_{}_{}_{}_{}_{}".format(
        clu, cdw, kd, kd_method, reg, cluster_description,
        args.experiment_index)
    cpk_dir = os.path.join(args.project_dir, cpk_dir)
    if not os.path.isdir(cpk_dir):
        os.makedirs(cpk_dir)

    epoch = args.resume_epoch

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")

    set_random_seed(1)

    data = Data(args.datasets, args.data_dir, args.batch_size, args.erasing_p,
                args.color_jitter, args.train_all)
    data.preprocess()

    clients = {}
    for cid in data.client_list:
        clients[cid] = Client(cid, data, device, args.project_dir,
                              args.model_name, args.local_epoch, args.lr,
                              args.batch_size, args.drop_rate, args.stride,
                              args.clustering)

    server = Server(clients, data, device, args.project_dir, args.model_name,
                    args.num_of_clients, args.lr, args.drop_rate, args.stride,
                    args.multiple_scale, args.clustering,
                    args.clustering_method, args.max_distance, args.n_cluster)

    if epoch != 0:
        print("======= loading checkpoint, epoch: {}".format(epoch))
        path = os.path.join(cpk_dir, "{}.pth".format(epoch))
        cpk_epoch, server_state_dict, client_list, client_classifier, client_model = load_checkpoint(
            path)
        assert (epoch == cpk_epoch)
        server.federated_model.load_state_dict(server_state_dict)
        for i in range(len(client_list)):
            cid = client_list[i]
            clients[cid].classifier.load_state_dict(client_classifier[i])
            clients[cid].model.load_state_dict(client_model[i])
        print("all models loaded, training from {}".format(epoch))

    print("=====training start!========")
    rounds = 500
    rounds = rounds // args.local_epoch
    for i in range(epoch, rounds):
        save_checkpoint(server, clients, data.client_list, cpk_dir, i)
        print('=' * 10)
        print("Round Number {}".format(i))
        print('=' * 10)
        server.train(i, args.cdw, use_cuda)
        # if not args.clustering:
        #     save_path = os.path.join(dir_name, 'federated_model.pth')
        #     torch.save(server.federated_model.cpu().state_dict(), save_path)
        if (i + 1) % 10 == 0:
            server.test(use_cuda, use_fed=True)
            if args.kd:
                server.knowledge_distillation(args.regularization, kd_method)
                server.test(use_cuda, use_fed=True)
        server.draw_curve()
예제 #21
0
파일: filter.py 프로젝트: scampion/ChCNN
from keras.models import model_from_json

m = hashlib.md5()

with open('model.json', 'r') as f:
    model = model_from_json(f.read())
    model.load_weights("model.h5")

m.update(open('model.json', 'r').read().encode())
m.update(open('model.h5', 'rb').read())
md5 = m.hexdigest()
print(md5)

config = json.load(open("./config.json"))
validation_data = Data(data_source=config["data"]["validation_data_source"],
                       alphabet=config["data"]["alphabet"],
                       input_size=config["data"]["input_size"],
                       num_of_classes=config["data"]["num_of_classes"])

consumer = KafkaConsumer('realtime',  group_id='chcnn', bootstrap_servers=['localhost:9092'])
producer = KafkaProducer(bootstrap_servers='localhost:9092')

for message in consumer:
    s = time.time()
    data = validation_data.str_to_indexes(str(message.value))
    result = model.predict([[data]])
    print("score: %0.3f | msg: %s | time: %0.3fsec" % (float(result[0][6]), str(message.value)[:60], time.time() - s))
    producer.send('results', json.dumps({'msg': str(message.value),
                                          'model': {'name': 'chcnn',
                                                    'version': md5,
                                                    'result': float(result[0][6])}
                                          }).encode())
예제 #22
0
    k_tmp_vocab = cPickle.load(open(os.path.join(args.prepro_dir, "k_tmp_vocab_ids.dat"), 'rb'))
    vocab_processor = Vocab_Operator.restore(args.vocab)        

else:
    img_feat, tags_idx, a_tags_idx, vocab_processor, k_tmp_vocab = data_utils.load_train_data(args.train_dir,
    args.tag_path, args.prepro_dir, args.vocab)        


img_feat = np.array(img_feat, dtype='float32')/127.5 - 1.
test_tags_idx = data_utils.load_test(args.test_path, vocab_processor, k_tmp_vocab)

print("Image feature shape: {}".format(img_feat.shape))
print("Tags index shape: {}".format(tags_idx.shape))
print("Attribute Tags index shape: {}".format(a_tags_idx.shape))
print("Test Tags index shape: {}".format(test_tags_idx.shape))

data = Data(img_feat, tags_idx, a_tags_idx, test_tags_idx, args.z_dim, vocab_processor)



dcgan = dcgan.DCGAN(model_options, training_options, data, args.mode, args.resume, args.model_dir)

input_tensors, variables, loss, outputs, checks = dcgan.build_model()

if args.mode == 0: 
    dcgan.train(input_tensors, variables, loss, outputs, checks)
else:
    dcgan.test()


    
def plt_multiclass(logger, **args):
    result_dir = '../results/'
    logger.info(f'Results directory: {result_dir}')
    if not os.path.exists(result_dir):
        os.makedirs(result_dir)
        logger.debug(
            f'Results directory does not exist. Creating the directory')
    log_fname = logger.handlers[0].baseFilename.split('/')[-1]
    result_fname = log_fname.replace('_log_',
                                     '_results_').replace('.log', '.results')
    logger.debug(f'Results filepath: {result_dir}{result_fname}')
    result_fh = open(f'{result_dir}{result_fname}', 'w')

    print('---PROGRESSIVE ELM - MULTI-CLASS---')
    print('---PROGRESSIVE ELM - MULTI-CLASS---', file=result_fh)

    filename = args['filename']
    header = args['header']
    label_location = args['label']
    scale_type = args['scale']
    test_ratio = args['testratio']

    print(f'Dataset file: {filename}', file=result_fh)
    logger.debug(
        f'Dataset File: {filename}, Scaling Type: {scale_type}, Test ratio: {test_ratio}'
    )
    logger.debug(
        f'Header attribute in pandas read_csv: {header}, label location in csv: {label_location}'
    )

    # Hyper-parameters
    hidden_dim = args['neurons']
    N0 = args['initial']
    batch_size = args['batch']

    logger.debug(
        f'Hidden layer neurons: {hidden_dim}, Number of samples in initial block: {N0}, '
        f'Batch size for training: {batch_size}')

    # Load and Pre-process Data
    logger.info('Loading and preprocessing data...')
    data = Data()
    data.set_logger(logger)
    print(f'Loading data from file: {filename}')
    data.load_csv(fname=filename, header=header)
    data.get_feats_labels(label_column=label_location)
    data.scale_features(scale_type=scale_type)
    data.split_train_test(test_ratio=test_ratio)
    logger.info('Loading and preprocessing data done')

    # PLT-ELM Model
    logger.info('Creating PLT ELM model')
    try:
        plt = PLT(data.num_feats, hidden_dim)
    except:
        logger.error('---> !!! Error in creating PLT class object !!!')
        raise ValueError

    plt.set_logger(logger)

    data.set_initial_batch_size(N0)
    data.set_training_batch_size(batch_size)

    print('Begin Training...')
    logger.info('Begin Training...')
    for batch_id, X, y in data.fetch_train_data():
        if batch_id == 0:
            plt.initial_batch(X, y)
            continue
        plt.train(X, y)

    print('Training Complete')
    logger.info('Training Complete')

    print('Begin Testing...')
    logger.info('Begin Testing...')
    report, accuracy = plt.test(data.test_data['X'], data.test_data['y'])
    print('Testing Complete')
    logger.info('Testing Complete')

    print(f'Classification Report: \n{report}\nAccuracy: {accuracy}')
    print(f'Classification Report: \n{report}\nAccuracy: {accuracy}',
          file=result_fh)
    logger.info(f'\nClassification Report: \n{report}')
    logger.info(f'\nAccuracy: {accuracy}')
    pass
예제 #24
0
def predict_data(msg):
    input_data = msg
    text_data = Data(json_data=input_data['TextData'])
    input_data['PStatus'] = 1
    text_data.preprocess_data()
    # predict period (year and month)
    text_data.set_alphabet(config['data']['period_alphabet'])
    model_input = text_data.get_data()
    prediction = model_year.predict(model_input)
    input_data['PYear'], input_data['PStatus'] = from_categorical(prediction, 0.9, config['data']['init_year'], input_data['PStatus'])
    prediction = model_month.predict(model_input)
    input_data['PMonth'], input_data['PStatus'] = from_categorical(prediction, 0.9, 0, input_data['PStatus'])
    # predict types
    text_data.preprocess_type_text()
    text_data.set_alphabet(config['data']['type_alphabet'])
    model_input = text_data.get_data()
    if input_data['Tax'] == 0:  # prediction if wage
        input_data['TaxType'] = None
        prediction = model_pmnt_type_zp.predict(model_input)
        input_data['PmntType'], input_data['PStatus'] = from_percent(prediction, 0.9, input_data['PStatus'])
    else:  # prediction if tax
        prediction = model_tax_type.predict(model_input)
        input_data['TaxType'], input_data['PStatus'] = from_categorical(prediction, 0.9, 0, input_data['PStatus'])
        prediction = model_pmnt_type_tax.predict(model_input)
        input_data['PmntType'], input_data['PStatus'] = from_percent(prediction, 0.9, input_data['PStatus'])
    del input_data['TextData']
    del input_data['Tax']
    return input_data
예제 #25
0
optimizer = Adam()

model.compile(optimizer=optimizer, loss='categorical_crossentropy')
model.summary()
plot_model(model,'model.png',show_shapes=True)
print "Built"


print "Loading the data sets...",

from data_utils import Data

train_data = Data(data_source = config.train_data_source,
                  alphabet = config.alphabet,
                  l0 = config.l0,
                  batch_size = 0,
                  n_classes= config.num_of_classes)

train_data.loadData()

X_train, y_train = train_data.getAllData()

print "Loadded"


print "Training ..."

model.fit(X_train,
          y_train,
          batch_size=config.batch_size,
예제 #26
0
import time
import numpy as np
import tensorflow as tf
import sys
import os
from data_utils import Data
from char_cnn import CharConvNet


if __name__ == '__main__':
    execfile("config.py")

    print "Loading data ....",
    train_data = Data(data_source = config.train_data_source,
                      alphabet = config.alphabet,
                      l0 = config.l0,
                      batch_size = config.batch_size,
                      no_of_classes = config.no_of_classes)
    train_data.loadData()
    dev_data = Data(data_source = config.dev_data_source,
                      alphabet = config.alphabet,
                      l0 = config.l0,
                      batch_size = config.batch_size,
                      no_of_classes = config.no_of_classes)
    
    dev_data.loadData()

    num_batches_per_epoch = int(train_data.getLength() / config.batch_size) + 1
    num_batch_dev = dev_data.getLength()
    print "Loaded"
예제 #27
0
    return X_train_n, y_train_n


if __name__ == '__main__':
    np.random.seed(42)

    ###########################################################################
    # load data
    ###########################################################################

    # Load dataset(s)
    directories = ["training_setA"]
    # directories = ["training_setB"]
    # directories = ["training_setB", "training_setA"]
    # directories = ["training_setA"]
    data = Data()
    data.load_data(directories)
    # data.load_data(directories, top=1000)

    ###########################################################################
    # data pre-analysis
    ###########################################################################

    # Compute basic statistics
    data.basic_statistics()
    print("\n Statistics of DataSet A \n", data.stats)
    # visualizations
    # data.plot_fill_counts(show=True)
    # data.plot_proportion_sepsis(show=True)
    # data.plot_proportion_sepsis_sepsispatients(show=True)
    # data.plot_hospitalization_time(show=True)
예제 #28
0
def main():
    # data loading
    data = Data(params_logger=params_logger,
                train_logger=train_logger,
                embedding_path=Path.skpgrm_path,
                data_limit=None,
                w2v_limit=None)

    # classifier configuration and building
    classifier = Classifier(model_name='niko',
                            max_seqlen=data.max_seqlen,
                            vocab_size=data.vocab_size,
                            n_dummy=data.n_dummy,
                            pretrained_embedding=data.embedding_matrix,
                            params_logger=params_logger,
                            train_logger=train_logger)
    classifier.build_model()

    # training phase (model kept every 5 epochs)
    print("train.shape: ", data.train_x.shape)

    # if max_seqlen is too small, train_x has only 2dim shapes so it will raise error
    inputs = [data.train_x[:, i, :] for i in range(data.train_x.shape[1])]
    answers = data.y

    save_weight_path = os.path.join(save_model_path,
                                    "weights.{epoch:02d}-{val_loss:.2f}.hdf5")
    output_dict = classifier.train(inputs,
                                   answers,
                                   save_output=True,
                                   validation_split=data.validation_split,
                                   save_path=save_weight_path)
    data_dict = data.retrieve_data()

    # test phase
    # TODO: make code clean (wrap these process up as one method in Data class)
    test_stories = [data.test_x[:, i, :] for i in range(data.test_x.shape[1])]
    test_inputs_with_1 = test_stories + [data.test_e1]
    test_inputs_with_2 = test_stories + [data.test_e2]
    answer_with_1 = classifier.test(
        test_inputs_with_1, batchsize=32)  # bsize for test does not matter
    answer_with_2 = classifier.test(test_inputs_with_2, batchsize=32)

    # test accuracy
    acc = classifier.calculate_accuracy(answer_with_1,
                                        answer_with_2,
                                        gt=data.test_answers)
    train_logger.info("test acc: {}".format(acc / float(data.test_x.shape[0])))

    # model and output saving
    save_intermed_output_path = os.path.join(save_model_path,
                                             "output_dict.pkl")
    save_data_path = os.path.join(save_model_path, "data.pkl")

    with open(save_intermed_output_path, 'wb') as w:
        pickle.dump(output_dict, w, protocol=4)
        train_logger.info(
            "output_dict saved: {}".format(save_intermed_output_path))
    with open(save_data_path, 'wb') as w:
        pickle.dump(data_dict, w, protocol=4)
        train_logger.info("data_dict saved: {}".format(save_data_path))
예제 #29
0
import sys
import os
import config
from data_utils import Data
from char_cnn import CharConvNet

learning_rate = 0.001
if __name__ == '__main__':
    print 'start...'
    execfile("config.py")
    print config.model.th
    print 'end...'
    print "Loading data ....",
    train_data = Data(data_source=config.train_data_source,
                      alphabet=config.alphabet,
                      l0=config.l0,
                      batch_size=config.batch_size,
                      no_of_classes=config.no_of_classes)
    train_data.loadData()
    dev_data = Data(data_source=config.dev_data_source,
                    alphabet=config.alphabet,
                    l0=config.l0,
                    batch_size=config.batch_size,
                    no_of_classes=config.no_of_classes)

    dev_data.loadData()

    num_batches_per_epoch = int(train_data.getLength() / config.batch_size) + 1
    num_batch_dev = dev_data.getLength()
    print "Loaded"
예제 #30
0
파일: main.py 프로젝트: mhpereir/PHYS490HW3
 input_file_path  = args.input_path
 params_file_path = args.params_path
 output_file_path = args.output_path
 
 verbosity        = args.v
 
 with open(params_file_path) as paramfile:
     param_file = json.load(paramfile)
 
 n         = 4                               # number of spins in a chains
 N         = int(param_file['batch_size'])
 eta       = param_file['eta']
 n_epochs  = int(param_file['n_epoch'])
 n_epoch_v = int(param_file['n_epoch_v'])
 
 data = Data(input_file_path)
 bm   = BM(eta, n)
 
 loss = torch.nn.KLDivLoss(reduction='batchmean')
     
 p_in_array    = torch.from_numpy(np.array([data.p_in[key] for key in data.p_in.keys()]))
 exp_val_data  = bm.expected_val(data.input_data, len(data.input_data))
 
 train_loss = []
 fig,ax = plt.subplots()
 for epoch in range(1,n_epochs+1):
     bm.gen_model_data(N)
     bm.probs()
     
     if len(bm.p_model) < len(data.p_in):
         for key in data.p_in.keys():
예제 #31
0
import json
import numpy as np
import pickle

from data_utils import Data, DataGenerator
from model import CharCNN
from sklearn.model_selection import train_test_split

if __name__ == "__main__":
    # Load configurations
    config = json.load(open("config.json"))

    data_preprocessor = Data(data_source=config["data"]["data_source"],
                         alphabet=config["data"]["alphabet"],
                         n_classes=config["data"]["n_classes"])
    # input_size, alphabet_size, num_examples = data_preprocessor.load_data()
    input_size = 5058
    alphabet_size = 69
    # num_examples = 5996996
    num_examples = 10000
    data_preprocessor.generate_all_data(save_reviews=config["data"]["save_reviews"],
                                        save_ratings=config["data"]["save_ratings"])

    # Define training and validation splits
    partition = np.arange(num_examples)
    train_indices, valid_indices = train_test_split(partition,
                                                         test_size=0.05,
                                                         random_state=42,
                                                         shuffle=True)
    # Parameters
    params = {'dim': (input_size,),
예제 #32
0
def main(_):

    print("Parameter:")
    for k, v in FLAGS.__flags.items():
        print("{} = {}".format(k, v))

    if not os.path.exists("./prepro"):
        os.makedirs("./prepro")

    if FLAGS.eval:
        print("Evaluation...")
        threshold = 0.5
        Aspects = ['服務', '環境', '價格', '交通', '餐廳']
        vocab_processor = VocabularyProcessor.restore(FLAGS.vocab)
        test_dict = data_utils.load_test(vocab_processor, FLAGS.test_data)
        graph = tf.Graph()
        config = tf.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        f = open('out.txt', 'w')
        r_words, r_words_dict = data_utils.load_new_aspect('new_aspect.txt')
        print(r_words)
        print(r_words_dict)
        with graph.as_default(), tf.Session(config=config) as sess:

            model = load_model(graph, sess, FLAGS.checkpoint_file)

            for k, v in sorted(test_dict.items(), key=lambda x: x[0]):
                attns, preds = sess.run(
                    [model["attn_w"], model["pred"]],
                    feed_dict={model["seq_in"]: v["test_x"]})
                t_asp = {'服務': 0, '環境': 0, '價格': 0, '交通': 0, '餐廳': 0}
                t_asp_s = {'服務': 0, '環境': 0, '價格': 0, '交通': 0, '餐廳': 0}
                for idx, p in enumerate(preds):
                    label = 1 if p == 1 else -1

                    for word in r_words:
                        if v['parsed_sents'][idx].find(word) >= 0:
                            t_asp_s[r_words_dict[word]] = 1
                            t_asp[r_words_dict[word]] = label

                    # for a_idx, a in enumerate(attns[idx]):
                    # 	if a >= threshold and a > t_asp_s[Aspects[a_idx]]:
                    # 		t_asp_s[Aspects[a_idx]] = a
                    # 		t_asp[Aspects[a_idx]] = label
                test_dict[k]["aspect"] = t_asp
                f.write(test_dict[k]['raw_context'] + '\n')
                f.write(str(t_asp))
                f.write("\n")

        ans = []
        with open(FLAGS.query, 'r') as f:
            for idx, row in enumerate(csv.reader(f)):
                if idx != 0:
                    ans.append(test_dict[int(row[1])]["aspect"][row[2]])
        print(len(ans))
        with open(FLAGS.output, 'w') as f:
            f.write("Id,Label\n")
            for idx, p in enumerate(ans):
                f.write("{},{}\n".format(idx + 1, p))

    else:
        if FLAGS.prepro:
            print("Start preprocessing data...")
            train_x, labels, vocab_processor, g_train_x, (
                g_sent_dist, g_pos_label) = data_utils.load_data(
                    FLAGS.polarity_data, FLAGS.aspect_data, FLAGS.prepro_train,
                    FLAGS.prepro_labels, FLAGS.vocab)
            (use_Pdist, use_Ndist, neg_label, pos_label, sent_Pdist,
             sent_Ndist, polarity, aspect_idx) = labels
            print("Start loading pre-train word embedding...")
            w2v_W = data_utils.build_w2v_matrix(vocab_processor,
                                                FLAGS.w2v_data,
                                                FLAGS.vector_file,
                                                FLAGS.embedding_size)
        else:
            train_x = cPickle.load(open(FLAGS.prepro_train, 'rb'))
            labels = cPickle.load(open(FLAGS.prepro_labels, 'rb'))
            g_train_x = cPickle.load(open("./prepro/g_train.dat", 'rb'))
            (g_sent_dist,
             g_pos_label) = cPickle.load(open("./prepro/g_label.dat", 'rb'))
            (use_Pdist, use_Ndist, neg_label, pos_label, sent_Pdist,
             sent_Ndist, polarity, aspect_idx) = labels
            vocab_processor = VocabularyProcessor.restore(FLAGS.vocab)
            w2v_W = cPickle.load(open(FLAGS.w2v_data, 'rb'))

        print(len(vocab_processor._reverse_mapping))
        print(len(train_x))
        print(len(use_Pdist))
        print(len(w2v_W))

        data = Data(train_x, (use_Pdist, use_Ndist, neg_label, pos_label,
                              sent_Pdist, sent_Ndist, polarity), aspect_idx,
                    FLAGS.dev_size, g_train_x, (g_sent_dist, g_pos_label))

        model = Model(data, w2v_W, vocab_processor)

        model.build_model()

        model.train()
예제 #33
0
파일: train.py 프로젝트: hane1818/Test
def test():
    # Prepare data
    print("Prepare test data...")
    test_data = Data("test")
    test_data_loader = DataLoader(test_data,
                                  batch_size=args.batch_size,
                                  shuffle=True)

    # Build model
    print("Build model...")
    model = Generator()
    if torch.cuda.is_available() and args.use_gpu:
        model.cuda()

    print("Load model...")
    model.load_state_dict(
        torch.load(
            os.path.join(args.train_dir,
                         "epoch-%d.model" % args.model_to_load)))
    model.eval()

    loss_fn = RewardWeightedCrossEntropyLoss(
    ) if args.rl else nn.CrossEntropyLoss()

    #writer = SummaryWriter(args.train_dir)

    # Start training
    test_losses = 0
    # Test model
    print("Test model performance")
    for i_batch, sampled_batch in enumerate(test_data_loader):
        docname, doc, label, mlabel, rewards, weights, _ = sampled_batch
        probs, logits = model(doc)
        if args.rl:
            test_losses += loss_fn(
                probs.contiguous().view(-1, args.target_label_size),
                mlabel.view(-1), rewards.view(-1),
                weights.view(-1)).data * probs.size(0)
        else:
            test_losses += loss_fn(
                probs.contiguous().view(-1, args.target_label_size),
                label.view(-1)).data * probs.size(0)
        if args.attn and args.coverage:
            test_losses -= torch.sum(torch.mean(model.covloss,
                                                dim=1)).squeeze()
        test_data.write_prediction_summary(
            docname, logits, "epoch-%d.model" % args.model_to_load)
    test_losses /= len(test_data)

    # ROUGE Evaluation
    system_dir = os.path.join(
        args.train_dir,
        '.'.join(["epoch-%d.model" % args.model_to_load, "test", "summary"]))
    model_dir = os.path.join(args.gold_summary_directory,
                             "gold-%s-test-orgcase" % args.data_mode)
    r1, r2, rl = calc_rouge_score(system_dir, model_dir)
    print("ROUGE Score(1, 2, L) %.2f %.2f %.2f" %
          (r1 * 100, r2 * 100, rl * 100))

    # Draw attention weight
    for i in range(len(test_data)):
        attn_weight = model.all_attn_weights[i].detach()  #.numpy()
        label, weight = mlabel[i], weights[i]
        logit = F.softmax(probs, dim=2)[i][:, 1].squeeze().detach().numpy()

        result = torch.masked_select(attn_weight, weight.eq(1)).view(
            args.max_doc_length, -1).transpose(0, 1)
        length = result.size(0)
        result = torch.masked_select(result, weight.eq(1)).view(
            length, length).transpose(0, 1).numpy()

        fig, ax = plt.subplots()
        im, cbar = heatmap(result,
                           list(range(1, length + 1)),
                           list(range(1, length + 1)),
                           ax=ax,
                           cmap="YlGn")

        # We want to show all ticks...
        ax.set_xticks(np.arange(length))
        ax.set_yticks(np.arange(length))
        # ... and label them with the respective list entries
        ax.set_xticklabels(list(range(1, length + 1)))
        ax.set_yticklabels([
            "%d(%.2f)" % (idx, l)
            for idx, l in zip(range(1, length + 1), logit)
        ])

        # Rotate the tick labels and set their alignment.
        plt.setp(ax.get_xticklabels(),
                 rotation=0,
                 ha="right",
                 rotation_mode="anchor")

        fig.tight_layout()
        plt.savefig(os.path.join(args.train_dir, "Figure_%d" % i))
    """# Write to Summary
class ModelTest(tf.test.TestCase):
    def setUp(self):
        tf.reset_default_graph()

        self.data = Data("train", hparams_unit)
        self.hook = self.data.switch_dataset_hook
        features, labels = self.data.input_fn()
        self.model = Model(
            "train",
            features,
            labels,
            vocab_size=Tokenizer.get_tokenizer(hparams_unit).vocab_size,
            hparams=hparams_unit)

        # init ops
        self.model.predictions
        # self.model.loss
        self.model.train_op
        self.model.eval_metrics

        self.all_scopes = ["Encoder", "Decoder", "Classifier", "embedding"]
        self.loss_to_scope = [  # loss_name => scopes that should have a gradient
            ("loss", self.all_scopes), ("vae_loss", ["Encoder", "Decoder"]),
            ("generator_loss", ["Decoder"]),
            ("recon_loss", ["Encoder", "Decoder"]), ("kl_loss", ["Encoder"]),
            ("attr_preserve_loss", ["Decoder"]),
            ("disentanglement_loss", ["Decoder"]),
            ("classify_synth_loss", ["Classifier"])
        ]
        self.stage_changes_scopes = {
            "vae_uncond": ["Encoder", "Decoder", "embedding"],
            "classify": ["Classifier", "embedding"],
            "vae_cond_lab": ["Encoder", "Decoder", "embedding"],
            "classify_synth": ["Classifier", "embedding"],
        }
        self.fails = []

    @with_session
    def test_gradient_static(self, sess):
        for loss_name, scopes in self.loss_to_scope:
            self._assert_grad_only_in_scope(loss_name, scopes)
        self._maybe_fail()

    @with_session
    def test_gradient_runtime(self, sess):
        for task in hparams_unit.train_stage_names:
            self.hook.switch_to(task, sess)
            self._assert_correct_params_change(task, sess)
        self._maybe_fail()

    def _maybe_fail(self):
        if len(self.fails) > 0:
            msg = "\nFailed %i sub-tests:\n  " % len(self.fails)
            msg += "\n  ".join(self.fails)
            self.fail(msg)

    def _assert_grad_only_in_scope(self, loss_name, scopes):
        should_be_none = self._scopes_except(scopes)
        grads, zero_grad_vars = self._gradients(
            loss_name,
            var_list=self.model.vars_in_scopes(scopes),
        )

        def fail(msg, v):
            self.fails.append(" ".join([loss_name, msg, v.name]))

        for v in zero_grad_vars:
            scope = v.name.split("/")[0]
            if scope not in should_be_none:
                fail("SHOULD have gradient for var", v)

        for _, v in grads:
            scope = v.name.split("/")[0]
            if scope in should_be_none:
                fail("should NOT have gradient for var", v)

    def _assert_correct_params_change(self, task, sess):
        scopes = self.stage_changes_scopes[task]

        changeable = lambda: self._get_vals(scopes)
        not_changeable = lambda: self._get_vals(self._scopes_except(scopes))

        should_change = changeable()
        should_not_change = not_changeable()
        print("should_change ", len(should_change))
        print("should_not_change ", len(should_not_change))

        # print("should change")
        # for v in should_change:
        #     print(v[1])
        #
        # print("should not change")
        # for v in should_not_change:
        #     print(v[1])
        #
        # print("all vars")
        # for v in self._get_vars(None):
        #     print(v.name)

        self.assertEqual(
            len(should_change) + len(should_not_change),
            len(self._get_vars(None)))

        sess.run(self.model.train_op)
        changed = []

        for old, new in zip(should_change, changeable()):
            val_old, name = old
            val_new, _ = new
            if np.array_equal(val_old, val_new):
                self.fails.append("%s %s SHOULD change during %s" %
                                  (name, str(val_old.shape), task))
            else:
                changed.append(name)

        for old, new in zip(should_not_change, not_changeable()):
            val_old, name = old
            val_new, _ = new
            if not np.array_equal(val_old, val_new):
                self.fails.append("%s %s should NOT change during %s" %
                                  (name, str(val_old.shape), task))
                changed.append(name)
                # if task == "classify" and name == "Encoder/z/z_log_var/bias:0":
                #     print("old_val", val_old)
                #     print("new_val", val_new)

        print(task)
        print("  should change")
        for v in should_change:
            print("    " + str(v[1]))
        print("  changed")
        for v in changed:
            print("    " + v)

    def _scopes_except(self, scopes):
        return set(self.all_scopes) - set(scopes)

    def _get_vars(self, scope):
        return tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)

    def _get_vals(self, scopes):
        return [(v.eval(), v.name) for scope in scopes
                for v in self._get_vars(scope)]

    def _gradients(self, loss_name, var_list=None):
        loss = getattr(self.model, loss_name)
        grads = self.model.optimizer.compute_gradients(loss, var_list=var_list)
        ret_grads, zero_grad_vars = [], []
        for grad, var in grads:
            if grad is None:
                zero_grad_vars.append(var)
            else:
                ret_grads.append((grad, var))
        return ret_grads, zero_grad_vars