コード例 #1
0
def run_epoch(session,
              model,
              eval_op=None,
              verbose=False,
              is_training=True,
              save_file=None):
    """Runs the model on the given data."""
    start_time = time.time()
    costs = 0.0
    iters = 0
    acc = 0.0
    state_fw, state_bw = session.run(
        [model.initial_state_fw, model.initial_state_bw])
    fetches = {
        "cost": model.cost,
        "final_state_fw": model.final_state_fw,
        "final_state_bw": model.final_state_bw,
        "accuracy": model.accuracy,
        "logits": model.logits,
        #"targets":model.targets,
        #"length":model.length
        #"loss_masked": model.loss_masked
    }
    if eval_op is not None:
        fetches["eval_op"] = eval_op
    for step in range(model.input.epoch_size):
        feed_dict = {}
        for i, (c, h) in enumerate(model.initial_state_fw):
            feed_dict[c] = state_fw[i].c
            feed_dict[h] = state_fw[i].h

        for i, (c, h) in enumerate(model.initial_state_bw):
            feed_dict[c] = state_bw[i].c
            feed_dict[h] = state_bw[i].h

        vals = session.run(fetches, feed_dict)
        if is_training == False:
            if save_file is not None:
                result = vals["logits"]
                #print(result)
                #for word in (result):
                #  save_file.write(str(word)+" ")
                #save_file.write("\n")
                predict_result.genPredict(array(result, dtype=int32),
                                          test_path=FLAGS.test_path)

        cost = vals["cost"]
        state_fw = vals["final_state_fw"]
        state_bw = vals["final_state_bw"]
        costs += cost
        iters += model.input.num_steps
        acc += vals["accuracy"]
        if verbose and step % (model.input.epoch_size // 10) == 10:
            print("%.3f Loss: %.3f Speed: %.0f wps Acc: %.3f" %
                  (step * 1.0 / model.input.epoch_size, np.exp(costs / iters),
                   iters * model.input.batch_size * max(1, FLAGS.num_gpus) /
                   (time.time() - start_time), acc /
                   (iters // model.input.num_steps)))

    return np.exp(costs / iters), acc / (iters // model.input.num_steps)
コード例 #2
0
ファイル: rnnlm.py プロジェクト: zedom1/Error-Detection
def run_epoch(session,
              model,
              eval_op=None,
              verbose=False,
              is_training=True,
              save_file=None):
    if is_training == False:
        result = session.run(model.logits)
        r1 = []
        for word in (result[0]):
            for backup in word:
                r1.append(backup)
                #save_file.write(str(backup)+" ")
            #save_file.write("\n")
        predict_result.genPredict(array(r1, dtype=float32),
                                  test_path=FLAGS.test_path)
        #np.savetxt(save_file,reshape(result,[-1,9175]),fmt="%.18e")
        return
    """Runs the model on the given data."""
    start_time = time.time()
    costs = 0.0
    iters = 0
    state = session.run(model.initial_state)

    fetches = {
        "cost": model.cost,
        "final_state": model.final_state,
    }
    if eval_op is not None:
        fetches["eval_op"] = eval_op
    for step in range(model.input.epoch_size):
        feed_dict = {}
        for i, (c, h) in enumerate(model.initial_state):
            feed_dict[c] = state[i].c
            feed_dict[h] = state[i].h

        vals = session.run(fetches, feed_dict)

        cost = vals["cost"]
        state = vals["final_state"]
        costs += cost
        iters += model.input.num_steps

        if verbose and step % (model.input.epoch_size // 10) == 10:
            print("%.3f perplexity: %.3f speed: %.0f wps" %
                  (step * 1.0 / model.input.epoch_size, np.exp(costs / iters),
                   iters * model.input.batch_size * max(1, FLAGS.num_gpus) /
                   (time.time() - start_time)))

    return np.exp(costs / iters)