def main():
    data = input_data.InputData()
    x = tf.placeholder(tf.float32, [None, SEQ_MAX_LEN, INPUT_DIV])
    t = tf.placeholder(tf.int32, [None])
    seqlen = tf.placeholder(tf.int32, [None])  # 可変長の入力データ(曲名)

    t_one_hot = tf.one_hot(t, depth=CLASS_NUM, dtype=tf.float32)

    def cell():
        return tf.contrib.rnn.BasicRNNCell(num_units=NODE_NUM,
                                           activation=tf.nn.tanh)  #中間層のセル

    cells = tf.contrib.rnn.MultiRNNCell([cell() for _ in range(NUM_LAYER)])
    outputs, states = tf.nn.dynamic_rnn(cell=cells,
                                        inputs=x,
                                        dtype=tf.float32,
                                        time_major=False)
    outputs = tf.transpose(outputs, perm=[1, 0, 2])

    w = tf.Variable(tf.random_normal([NODE_NUM, CLASS_NUM], stddev=0.01))
    b = tf.Variable(tf.zeros([CLASS_NUM]))
    logits = tf.matmul(outputs[-1], w) + b  # 出力層
    pred = tf.nn.softmax(logits)  # ソフトマックス

    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=t_one_hot,
                                                            logits=logits)
    loss = tf.reduce_mean(cross_entropy)  # 誤差関数
    train_step = tf.train.AdamOptimizer().minimize(loss)  # 学習アルゴリズム

    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(t_one_hot, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))  # 精度

    # 学習の実行
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    i = 0
    for _ in range(STEP_NUM):
        sings, labels = data.next_batch()

        length_div = [len(sing) for sing in sings]
        _, _loss, _accuracy = sess.run((train_step, loss, accuracy),
                                       feed_dict={
                                           x: sings,
                                           t: labels,
                                           seqlen: length_div
                                       })

        i += 1
        if i % 100 == 0:
            x_test, t_test = data.test_data()
            loss_test_, acc_test_, pred_ = sess.run([loss, accuracy, pred],
                                                    feed_dict={
                                                        x: x_test,
                                                        t: t_test
                                                    })
            print("[TRAIN] loss : %f, accuracy : %f" % (_loss, _accuracy))
            print("[TEST loss : %f, accuracy : %f" % (loss_test_, acc_test_))
            output_prediction(pred_)
    sess.close()
Example #2
0
 def __init__(self, sents, config, vocabulary):
     '''self.lengths = tf.placeholder(shape=(-1,), dtype=tf.int32)
     self.train_input = tf.placeholder(shape=(1, -1), dtype=tf.int32)
     self.validate_input = tf.placeholder(shape, dtype)
     self.test_input = tf.placeholder(shape, dtype)
     self.rnn = RnnLm(tf.contrib.data.Dataset.from_tensors(self.train_input),
                      tf.contrib.data.Dataset.from_tensors(self.validate_input),
                      tf.contrib.data.Dataset.from_tensors(self.test_input),
                      config,
                      vocabulary)'''
     self.train_input = input_data.InputData(vocabulary, sents=[[]])
     self.validate_input = input_data.InputData(vocabulary, sents=[[]])
     '''self.test_input = input_data.InputData(vocabulary, sents=sents)'''
     self.input_data = input_data.InputData(vocabulary)
     self.test_input = (tf.placeholder(dtype=tf.int64, name="seqlens"),
                        tf.placeholder(dtype=tf.int32, name="seqs"))
     self.rnn = RnnLm(self.train_input.dataset(),
                      self.validate_input.dataset(), self.test_input,
                      config, vocabulary)
     self.session = None
def main():
    data = input_data.InputData()

    x = tf.placeholder(tf.float32, shape=[None, 3, 9])
    y = tf.placeholder(tf.float32, shape=[None, 3])
    rate = tf.placeholder(tf.float32, shape=[])
    predict_op = predicrt(x, rate)
    loss_op = loss(predict_op, y)
    train_op = train(loss_op)
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_op)
        for i in range(MAX_STEP):
            train_data, actual_data = data.next_batch()
            _, _predict, _loss = sess.run([train_op, predict_op, loss_op],
                                          feed_dict={
                                              x: train_data,
                                              y: actual_data,
                                              rate: 0.5
                                          })
            if i % 100 == 0:
                print('step: %05d loss: %0.05f' % (i, _loss))
            if i % 1000 == 0:
                test_data, test_target = data.test_data()
                _loss, _predict = sess.run([loss_op, predict_op],
                                           feed_dict={
                                               x: test_data,
                                               y: test_target,
                                               rate: 1.0
                                           })
                sample_predict_index = random.choice(range(len(_predict)))
                sample_predict = _predict[sample_predict_index]
                actual_tmp = test_target[sample_predict_index]
                print(
                    'Test loss: %00.05f sample_avg_tmp: %00.02f sample_max_tmp: %00.02f sample_min_tmp: %00.02f actual_avg_tmp: %00.02f actual_max_tmp: %00.02f actual_min_tmp: %00.02f'
                    % (_loss, sample_predict[0], sample_predict[1],
                       sample_predict[2], actual_tmp[0], actual_tmp[1],
                       actual_tmp[2]))

        _predicts = sess.run(predict_op,
                             feed_dict={
                                 x: test_data,
                                 y: test_target,
                                 rate: 1.0
                             })
        actual_avg_tmps = [d[0] for d in test_target]
        actual_max_tmps = [d[1] for d in test_target]
        actual_min_tmps = [d[2] for d in test_target]
        predict_avg_tmps = [p[0] for p in _predicts]
        predict_max_tmps = [p[1] for p in _predicts]
        predict_min_tmps = [p[2] for p in _predicts]
        generate_graph(actual_avg_tmps, predict_avg_tmps, 'average')
        generate_graph(actual_max_tmps, predict_max_tmps, 'max')
        generate_graph(actual_min_tmps, predict_min_tmps, 'min')
Example #4
0
def read_ptb_with_voc(vocabulary, path=join("ptb", "data")):
    def read_sents(filename):
        with open(filename) as file:
            for line in file:
                yield line.lower().split() + ["</snt>"]

    file_reader = lambda filename: GenIt(read_sents, filename)
    return [
        input_data.InputData(vocabulary, sents=fr) for fr in [
            file_reader(join(path, "ptb.train.txt")),
            file_reader(join(path, "ptb.valid.txt")),
            file_reader(join(path, "ptb.test.txt"))
        ]
    ]
Example #5
0
    def __init__(self, train_set, validate_set, test_set, config, vocabulary, model_file=None):
        """Tworzy  trzy grafy obliczeniowe sieci z tymi samymi zmiennymi, taką samą arhitekturą, ale innymi źródłąmi
        danych wejściowych - czytające z train path, validate path i test path"""
        self.vocabulary = vocabulary
        self.config = config
        initializer = tf.random_uniform_initializer(-self.config.init_scale,
                                                    self.config.init_scale)
        train_set, validate_set, test_set = [self._normalize_input_type(obj)
                                             for obj in (train_set, validate_set, test_set)]
        self.train_set = train_set
        self.validate_set = validate_set
        self.test_set = test_set

        self._cells_created = 0

        self.summaries = {"train": [], "validate": [], "test": [], "production": []}

        # below all trainable variables are defined
        self.cell = self.multi_cell(config.size, config.depth)
        self.weights = tf.get_variable(shape=(self.config.size, self.vocabulary.size()), name="weights", initializer=initializer)
        self.bias = tf.get_variable(shape=(self.vocabulary.size(),), name="bias", initializer=initializer)

        with tf.variable_scope("net", reuse=False, initializer=initializer):
            self.train_data, self.train_iter = self.get_sentence(train_set)
            self.train_graph = self.build_compute_graph(self.train_data, "train")
        with tf.variable_scope("net", reuse=True, initializer=initializer):
            self.validate_data, self.validate_iter = self.get_sentence(validate_set)
            self.test_data, self.test_iter = self.get_sentence(test_set)
            self.validate_graph = self.build_compute_graph(self.validate_data, "validate")
            self.test_graph = self.build_compute_graph(self.test_data, "test")
            self.production_input_data = input_data.InputData(vocabulary)
            self.production_input_sequences = tf.placeholder(dtype=tf.int64, name="sequences")
            self.production_input_sequence_lengths = tf.placeholder(dtype=tf.int64, name="sequence_lengths")
            production_input_dataset = self._normalize_input_type((self.production_input_sequence_lengths,
                                                                   self.production_input_sequences))
            self.production_batch_size = tf.placeholder(dtype=tf.int64, shape=())
            self.production_data, self.production_iter = self.get_sentence(production_input_dataset,
                                                                           batch_size=self.production_batch_size)
            self.production_graph = self.build_compute_graph(self.production_data,
                                                             "production",
                                                             batch_size=self.production_batch_size)

        self._lr = tf.Variable(0.0, trainable=False)
        self._new_lr = tf.placeholder(
            tf.float32, shape=[], name="new_learning_rate")
        self._lr_update = tf.assign(self._lr, self._new_lr)
        self.end_of_sentence_id = 0

        self.production_session = None
Example #6
0
def train():
    with tf.Graph().as_default():
        tf.GraphKeys.VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES
        data = input_data.InputData()

        x = tf.placeholder(
            tf.float32,
            [None, FLAGS.width_size * FLAGS.height_size * FLAGS.depth_size])
        y = tf.placeholder(tf.float32, [None, 10])

        with tf.Session() as sess:
            output = convolution(x)
            loss_op = loss(output, y)
            train_op = train(loss_op)
            accuracy_op = accuracy(output, y)
            summary_writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph)
            for i, vals in enumerate(tf.trainable_variables()):
                tf.summary.histogram(vals.name, vals)
            summary_op = tf.summary.merge_all()
            saver = tf.train.Saver()
            load_checkpoint(sess, saver)
            for i in range(FLAGS.max_step):
                labels, images = data.next_batch()
                _ = sess.run((train_op), feed_dict={x: images, y: labels})
                if i % 10 == 0:
                    _loss, _accuracy = sess.run((loss_op, accuracy_op),
                                                feed_dict={
                                                    x: images,
                                                    y: labels
                                                })
                    print(
                        'global step: %04d, train loss: %01.7f, train accuracy %01.5f'
                        % (i, _loss, _accuracy))
                if i % 100 == 0 or i == FLAGS.max_step - 1:
                    summary_str = sess.run(summary_op,
                                           feed_dict={
                                               x: images,
                                               y: labels
                                           })
                    summary_writer.add_summary(summary_str, i)
                if i % 1000 == 0 or i == FLAGS.max_step - 1:
                    saver.save(sess, FLAGS.checkpoint_dir, global_step=i)
                    test_labels, test_images = data.test_data()
                    _accuracy = sess.run(accuracy_op,
                                         feed_dict={
                                             x: test_images,
                                             y: test_labels
                                         })
                    print('Test accuracy: %s' % _accuracy)
Example #7
0
def main(_):
    tf.logging.set_verbosity(tf.logging.INFO)
    data = input_data.InputData(train_data_path=flags.train_data_path,
                                eval_data_path=flags.eval_data_path,
                                model_dir=flags.model_dir,
                                min_count=flags.min_count,
                                max_seq_length=flags.max_seq_length,
                                batch_size=flags.batch_size,
                                eval_batch_size=flags.eval_batch_size,
                                epoch=flags.epoch,
                                shuffle=True,
                                num_parallel_calls=flags.num_parallel_calls)
    num_train_steps = int(data.num_train_samples / flags.batch_size *
                          flags.epoch)
    num_warmup_steps = int(flags.warmup_proportion * num_train_steps)
    estimator = build_estimator(flags, data.vocabulary_size, num_train_steps,
                                num_warmup_steps, data.freqs, data.vocab)
    if flags.do_train:
        tf.logging.info("***** Running training *****")
        tf.logging.info("  Num examples = %d", data.num_train_samples)
        tf.logging.info("  Batch size = %d", flags.batch_size)
        tf.logging.info("  Num train steps = %d", num_train_steps)
        tf.logging.info("  Num warmup steps = %d", num_warmup_steps)
        # estimator.train(input_fn=data.build_numpy_train_input_fn())
        estimator.train(input_fn=data.build_ds_train_input_fn())
    if flags.do_eval:
        tf.logging.info("***** Running evaluating *****")
        tf.logging.info("  Batch size = %d", flags.eval_batch_size)
        # estimator.evaluate(input_fn=data.build_numpy_eval_input_fn())
        estimator.evaluate(input_fn=data.build_ds_eval_input_fn())
    if flags.do_export:
        tf.logging.info("***** Running exporting *****")
        nce_weights = estimator.get_variable_value(
            'nce_layer_variables/nce_weights:0')
        nce_biases = estimator.get_variable_value(
            'nce_layer_variables/nce_biases:0')
        np.save(os.path.join(flags.model_dir, 'nce_weights.npy'), nce_weights)
        np.save(os.path.join(flags.model_dir, 'nce_biases.npy'), nce_biases)

        assets_extra = {}
        assets_extra['keys.dict'] = data.keys_path
        estimator.export_savedmodel(
            flags.export_model_dir,
            serving_input_receiver_fn=data.build_serving_input_fn(),
            assets_extra=assets_extra)
Example #8
0
# other stuff
from copy import copy, deepcopy
import random
# MPI
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
rep_ind_perm = np.arange(size)

if (rank == 0):
    print 'starting by getting the simulation data...'
    print '\n'

# load the input data
ID = ID.InputData()
sys_mod_nm = 'system_functions_' + str(ID.sysH)
sys_mod = __import__(sys_mod_nm)
MC_types = deepcopy(ID.MC_types)
OPT_types = deepcopy(ID.OPT_types)
TRJ_types = deepcopy(ID.TRJ_types)
# get the bins
bin_ctrs = np.load(ID.bin_ctrs_fnm)
bin_ctrs_CG = np.load(ID.bin_ctrs_CG_fnm)
if (ID.TRJ_type == TRJ_types[0]):  # TRJ_type == '1D'
    dtraj_AA = np.load(ID.dtraj_AA_fnm)
    dtraj_AA = dtraj_AA.astype(int)
    dtraj_AA = dtraj_AA.tolist()
    dtraj_CG = np.load(ID.dtraj_CG_fnm)
    dtraj_CG = dtraj_CG.astype(int)
    dtraj_CG = dtraj_CG.tolist()