Пример #1
0
def create_vocab():

    DBHelperMethod.connect_to_db()
    dataset = DBHelperMethod.load_data_set()
    chars = dp.load_nn_input_dataset_string(dataset[:, [0, 6]])
    vocab, vocab_inv = dp.build_vocab(chars)
    return vocab, vocab_inv, chars, dataset
Пример #2
0
def convert_input_to_vocab(input):
    sequence_list = numpy.array(input)
    vocabulary, vocabulary_inv = dp.build_vocab(sequence_list)
    sentences = list(chain(*sequence_list))

    padded_input = dp.build_input_data2(sentences, vocabulary)
    return numpy.array(padded_input), vocabulary, vocabulary_inv
Пример #3
0
def main(_):
    FLAGS = tf.app.flags.FLAGS
    pp = pprint.PrettyPrinter()
    FLAGS._parse_flags()
    pp.pprint(FLAGS.__flags)

    # Load Data
    X_train, Q_train, Y_train = data_helper.load_data('train')
    X_test, Q_test, Y_test = data_helper.load_data('valid')

    vocab_size = np.max(X_train) + 1
    print('[?] Vocabulary Size:', vocab_size)

    # Create directories
    if not os.path.exists(FLAGS.ckpt_dir):
        os.makedirs(FLAGS.ckpt_dir)

    timestamp = datetime.now().strftime('%c')
    FLAGS.log_dir = os.path.join(FLAGS.log_dir, timestamp)
    if not os.path.exists(FLAGS.log_dir):
        os.makedirs(FLAGS.log_dir)

    # Train Model
    with tf.Session(config=tf.ConfigProto(
            log_device_placement=False,
            allow_soft_placement=True)) as sess, tf.device('/gpu:0'):
        model = AlternatingAttention(FLAGS.batch_size,
                                     vocab_size,
                                     FLAGS.encoding_dim,
                                     FLAGS.embedding_dim,
                                     FLAGS.num_glimpses,
                                     session=sess)

        if FLAGS.trace:  # Trace model for debugging
            train.trace(FLAGS, sess, model, (X_train, Q_train, Y_train))
            return

        saver = tf.train.Saver()

        if FLAGS.restore_file is not None:
            print('[?] Loading variables from checkpoint %s' %
                  FLAGS.restore_file)
            saver.restore(sess, FLAGS.restore_file)

        # Run evaluation
        if FLAGS.evaluate:
            if not FLAGS.restore_file:
                print('Need to specify a restore_file checkpoint to evaluate')
            else:
                test_data = data_helper.load_data('test')
                word2idx, _, _ = data_helper.build_vocab()
                test.run(FLAGS, sess, model, test_data, word2idx)
        else:
            train.run(FLAGS, sess, model, (X_train, Q_train, Y_train),
                      (X_test, Q_test, Y_test), saver)
Пример #4
0
def main(_):
    FLAGS = tf.app.flags.FLAGS
    pp = pprint.PrettyPrinter()
    FLAGS._parse_flags()
    pp.pprint(FLAGS.__flags)

    # Load Data
    X_train, Q_train, Y_train = data_helper.load_data('train')
    X_test, Q_test, Y_test = data_helper.load_data('valid')

    vocab_size = np.max(X_train) + 1
    print('[?] Vocabulary Size:', vocab_size)

    # Create directories
    if not os.path.exists(FLAGS.ckpt_dir):
        os.makedirs(FLAGS.ckpt_dir)

    timestamp = datetime.now().strftime('%c')
    FLAGS.log_dir = os.path.join(FLAGS.log_dir, timestamp)
    if not os.path.exists(FLAGS.log_dir):
        os.makedirs(FLAGS.log_dir)

    # Train Model
    with tf.Session(config=tf.ConfigProto(log_device_placement=False, allow_soft_placement=True)) as sess, tf.device('/gpu:0'):
        model = AlternatingAttention(FLAGS.batch_size, vocab_size, FLAGS.encoding_dim, FLAGS.embedding_dim, FLAGS.num_glimpses, session=sess)

        if FLAGS.trace: # Trace model for debugging
            train.trace(FLAGS, sess, model, (X_train, Q_train, Y_train))
            return

        saver = tf.train.Saver()

        if FLAGS.restore_file is not None:
            print('[?] Loading variables from checkpoint %s' % FLAGS.restore_file)
            saver.restore(sess, FLAGS.restore_file)

        # Run evaluation
        if FLAGS.evaluate:
            if not FLAGS.restore_file:
                print('Need to specify a restore_file checkpoint to evaluate')
            else:
                test_data = data_helper.load_data('test')
                word2idx, _, _ = data_helper.build_vocab()
                test.run(FLAGS, sess, model, test_data, word2idx)
        else:
            train.run(FLAGS, sess, model,
                    (X_train, Q_train, Y_train),
                    (X_test, Q_test, Y_test),
                    saver)
Пример #5
0
def load_data(data_path):
    """
    Loads and preprocessed data for the MR dataset.
    Returns input vectors, labels, vocabulary, and inverse vocabulary.
    """
    # Load and preprocess data
    sentences, labels,label_inv = load_data_and_labels(data_path)
    vocabulary, vocabulary_inv =data_helper.build_vocab(sentences)
    x, y = build_input_data(sentences, labels, vocabulary)
    print('before adding noise train case number in every category')
    count_category(y,label_inv)
    x,y=noise_input_data(x,y,vocabulary)
    print('after adding noise train case number in every category')
    count_category(y,label_inv)
    return [x, y, vocabulary, vocabulary_inv,label_inv]
Пример #6
0
def load_testing_data():
    dp.establish_db_connection()
    training_dataset = DBHelperMethod.load_dataset_by_type("testing")

    x = dp.load_nn_input_dataset_string_space_only(training_dataset[:, [0, 6]])
    y = dp.load_nn_labels_dataset_string(training_dataset[:, [0, 1]])
    vocab, vocab_inv = dp.build_vocab(x)

    sent_num, sen_len = dp.load_nn_seq_lengths(training_dataset[:, [3]])

    input_sentences = sqp.create_window_of_chars(list(x), window_size)
    input_sentences = dp.build_input_data_without_flattening(
        input_sentences, vocab)
    input_sentences = numpy.array(input_sentences[:-1])

    output_labels = sqp.create_window_of_chars(list(y), window_size)
    output_labels = numpy.array(output_labels[:-1])

    return input_sentences, output_labels, vocab, vocab_inv
Пример #7
0
def predict(config, model):
    model_path = os.path.join(config.save_path, "model.ckpt")
    model.load_state_dict(torch.load(model_path))
    model.eval()
    char2index, _ = build_vocab()
    index2label = {index: label for index, label in enumerate(config.labels)}

    with torch.no_grad():
        while True:
            input_line = input("input your sentence >>>")
            input_index = [char2index[char] for char in input_line]
            input_index = torch.tensor([input_index]).to(config.device)
            output = model(input_index)
            output_arg = torch.argmax(output, dim=1)
            output_arg = output_arg.numpy().tolist()
            output = output.numpy().tolist()

            print("target label: [{label}] with props: {props}".format(
                label=index2label[output_arg[0]], props=output[0]))
def load_test_data(test_file, labels):

    df = pd.read_csv(test_file)
    select = ['Descript']

    df = df.dropna(axis=0, how='any', subset=select)
    test_examples = df[select[0]].apply(
        lambda x: data_helper.clean_str(x).split(' ')).tolist()
    vocabulary, vocabulary_inv = data_helper.build_vocab(test_examples)
    x = np.array([[vocabulary[word] for word in sentence]
                  for sentence in test_examples])
    num_labels = len(labels)
    one_hot = np.zeros((num_labels, num_labels), int)
    np.fill_diagonal(one_hot, 1)
    label_dict = dict(zip(labels, one_hot))

    y_ = None
    if 'Category' in df.columns:
        select.append('Category')
        y_ = df[select[1]].apply(lambda x: label_dict[x]).tolist()

    not_select = list(set(df.columns) - set(select))
    df = df.drop(not_select, axis=1)
    return x, y_, df
Пример #9
0
        train_summary_writer.flush()

        timestr = datetime.datetime.now().isoformat()
        logging.info("%s, the %i step, train cost is:%f and the train accuracy is %6.7f"%(timestr, global_steps, cost, accuracy))
        if(global_steps % FLAGS.evaluate_every == 0):
            valid_accuracy = evaluate(valid_model,session,valid_data,global_steps)
            logging.info("%s, the valid accuracy is %f"%(timestr, valid_accuracy))

        global_steps += 1

    return global_steps
#---------------------------- run epoch end -------------------------------------


#------------------------------------load data -------------------------------
word2idx, idx2word = build_vocab(FLAGS.word_file)
label2idx, idx2label = load_label(FLAGS.label_file)
train_x, train_y, train_mask = load_data(FLAGS.train_file, word2idx, label2idx, FLAGS.sequence_len)
logging.info("load train data finish")
train_data, valid_data = create_valid(zip(train_x, train_y, train_mask))
num_classes = len(label2idx)
embedding = load_embedding(FLAGS.embedding_size, filename=FLAGS.embedding_file)
test_x, test_y, test_mask = load_data(FLAGS.test_file, word2idx, label2idx, FLAGS.sequence_len)
logging.info("load test data finish")
#----------------------------------- load data end ----------------------

#----------------------------------- execute train ---------------------------------------
with tf.Graph().as_default():
    with tf.device("/cpu:0"):
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_options)
        session_conf = tf.ConfigProto(allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement, gpu_options=gpu_options)
Пример #10
0
                        " the real selectd set from the The sampled pools")
tf.flags.DEFINE_integer("gan_k", 10, "he number of samples of gan")
# Misc Parameters
tf.flags.DEFINE_boolean("allow_soft_placement", True,
                        "Allow device soft device placement")
tf.flags.DEFINE_boolean("log_device_placement", False,
                        "Log placement of ops on devices")

FLAGS = tf.flags.FLAGS
FLAGS.flag_values_dict()

timeStamp = time.strftime("%Y%m%d%H%M%S", time.localtime(int(time.time())))

print(("Loading data..."))

vocab = data_helper.build_vocab()
embeddings = data_helper.load_vectors(vocab)
alist = data_helper.read_alist("WebAP")
raw = data_helper.read_raw("WebAP")
qs = data_helper.qname("WebAP/qeo-train.txt")
number = data_helper.qid("WebAP/qeo-train.txt")

numbertest = data_helper.qid("WebAP/qeo-test.txt")
qstest = data_helper.qname("WebAP/qeo-test.txt")
# 加载了几个测试集
test_List = data_helper.loadTestSet(dataset="WebAP", filename="term-test")
train_List = data_helper.loadTestSet(dataset="WebAP", filename="term-train")

print("Load done...")
log_gan_train_ndcg = 'WebAP/log/log_gan_train_ndcg'
log_gan_test_ndcg = 'WebAP/log/log_gan_test_ndcg'
Пример #11
0
def test_build_vocab():
  file_path_1 = "./data/source.txt"
  file_path_2 = "./data/target.txt"
  output_path = "./data/vocab.txt"
  build_vocab(file_path_list=[file_path_1, file_path_2],
              output_path=output_path)
Пример #12
0
def train_cnn():
    global my_min  # by odg

    # 오리지널 파일로부터 입력과 출력값을 배열로 만들어내기. 또한 CNN의 설정값 또한 파일로서 읽기
    """Step 0: load sentences, labels, and training parameters"""
    # 파라미터로 받은 파일을 로딩해서 문장배열(x_raw)과 각 문장들의 분류값배열(y_raw)을 얻어낸다. x는 뉴럴넷의 input이로, y는 output이다.
    train_file = sys.argv[1]
    # CNN네트워크의 각종 세부 설정값(hyper parameter)들을 로딩한다. 이 안에는 num_epochs, batch_size, num_filters등의 값들이 들어있다.
    x_raw, y_raw, df, labels = data_helper.load_data_and_labels(
        train_file)  # @
    # x_raw는  데이터셋, y_raw는 label의 One-hot vector, df는 라벨 포함 데이터셋, labels는 라벨 들어가있음.

    parameter_file = sys.argv[2]
    params = json.loads(open(parameter_file).read())

    model_dir = sys.argv[3]  # 모델 폴더 이름
    max_document_length = 0
    minimum_frequency = 5  # 단어장에 넣을 단어의 최소한의 빈도수(해당 빈도수 이상 있어야 단어장에 등록)
    list_max_final_scores = []  # final_scores 들 중 가장 큰 값만 저장한 리스트 by odg

    if (model_dir == "new"):
        timestamp = str(int(time.time()))
        model_name = "./trained_model_" + timestamp
        # 학습내용을 기록할 디렉토리 정의
        out_dir = os.path.abspath(os.path.join(os.path.curdir,
                                               model_name))  # !새롭게 생기는 폴더.
        checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints"))
        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)

        vectorize_list = list(mytoken.tokenizer(x_raw))

        for i in vectorize_list:
            if max_document_length < len(i):
                max_document_length = len(i)

        word2Vec = Word2Vec(vectorize_list,
                            size=params['embedding_dim'] -
                            params['num_of_class'],
                            window=3,
                            min_count=minimum_frequency,
                            workers=4)
        word2Vec.save(model_name + "/word2Vec.vec")  # @
        fastText = FastText(vectorize_list,
                            size=params['embedding_dim'] -
                            params['num_of_class'],
                            window=3,
                            min_count=minimum_frequency,
                            workers=4)
        fastText.save(model_name + "/fastText.vec")  # @

        vocab_dict, _ = data_helper.build_vocab(max_document_length,
                                                word2Vec.wv.index2word,
                                                params['num_of_class'], True)
        # 학습내용중 Tensorflow 내부의 변수상태가 저장됨 (예:AdamOptimizer)

    else:
        out_dir = os.path.abspath(os.path.join(model_dir))
        checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints"))
        vocab_dict, max_document_length = data_helper.build_vocab(
            0, None, None, False)
        model_name = model_dir
    checkpoint_prefix = os.path.join(checkpoint_dir, "model")

    # 전체 문장을 동일한 크기로 맞추고, 단어마다 ID를 부여해서, ID로 이루어진 문장을 만들기
    """Step 1: pad each sentence to the same length and map each word to an id"""
    # 문장배열의 크기를 pad값을 사용해서 같은 크기로 맞추어 주고, 문장안의 단어들을 ID로 매핑시켜주는 작업을 통해 학습 문장을 숫자 매트릭스형태로 만들어 학습이 가능한 상태로 만든다.
    logging.info('가장 긴 길이의 문장: {}'.format(max_document_length))  # 21

    vocab_processor = learn.preprocessing.VocabularyProcessor(
        max_document_length=max_document_length,
        vocabulary=vocab_dict,
        tokenizer_fn=mytoken.tokenizer)  # 데이터셋의 단어들에 대해 인덱스를 붙여주는... #!

    x = np.array(list(vocab_processor.transform(x_raw)))  # !
    vocab_dictionary = vocab_processor.vocabulary_._mapping  # !
    y = np.array(y_raw)  # y는 라벨에 대한 One-hot vector

    # 데이터셋을 학습용과 테스트용으로 나누기
    """Step 2: split the original dataset into train and test sets"""
    x_, x_test, y_, y_test = train_test_split(x,
                                              y,
                                              test_size=0.1,
                                              random_state=42)
    # 학습 문장과 결과값을 학습과 테스트 두개의 그룹으로 나눈다.(10%만 검증용으로 사용한다)
    # 데이터셋을 임의로 배치하고, 학습 데이터를 학습용과 검증용으로 다시 분류하기.
    """Step 3: shuffle the train set and split the train set into train and dev sets"""
    # 학습용 문장 배열(x_)의 순서를 그때마다 다르게 하기 위해 random방식으로 배열의 순서를 바꾸는 과정이다.
    # 학습데이터를 다시 두개의 그룹으로 나누는 것은 학습과 검증을 나눔으로서 overfitting을 줄이고 학습의 효과를 확인하기 쉽게 하기 위해서이다. 전체 데이터셋 구성은 다음과 같다. https://blog.naver.com/2feelus/221005831312 에서 확인.
    shuffle_indices = np.random.permutation(np.arange(len(y_)))  # 인덱스를 섞어줌
    x_shuffled = x_[
        shuffle_indices]  # 데이터셋에서 랜덤으로 셔플된 문장 인덱스 가져옴 ex) [5 69 0 ... 0]
    y_shuffled = y_[shuffle_indices]  # 해당 데이터셋에 대한 라벨 One-hot vector를 가져옴
    x_train, x_dev, y_train, y_dev = train_test_split(x_shuffled,
                                                      y_shuffled,
                                                      test_size=0.1)

    # 카테고리 라벨을 파일로 저장하여, 예측시에 활용할수 있도록 하기
    # 전체 카테고리 라벨들이 label.json 파일의 내용으로 저장. 실제 예측시에 이파일에 저장된 카테고리 순서에 따라 예측값을 얻어낸다.

    with open('./labels.json', 'w', encoding='utf-8-sig') as outfile:  # by odg
        json.dump(labels, outfile, indent=4, ensure_ascii=False)  # by odg

    logging.info('x_train: {}, x_dev: {}, x_test: {}'.format(
        len(x_train), len(x_dev), len(x_test)))
    logging.info('y_train: {}, y_dev: {}, y_test: {}'.format(
        len(y_train), len(y_dev), len(y_test)))

    # 텐서 플로우 그래프생성이후 CNN 객체를 생성하기
    """Step 5: build a graph and cnn object"""
    # 텐서플로우에서 머신러닝의 데이터 흐름을 표현하는 그래프를 새로 생성한다. 그래프는 여러가지 머신러닝용 계산 명령 객체들을 포함하고 있다.
    graph = tf.Graph()
    # 파이선의 Context Manager 개념을 사용하여 기존의 기본 그래프 객체를 위에서 선언한 graph 객체로 대체하여 내부 블럭에 적용한다.
    # 멀티프로세스로 돌아가는 환경에서 이러한 방식을 사용하여 쓰레드에서 각각의 그래프 객체를 사용하도록 한다.
    with graph.as_default():
        # 세션을 새로 생성한다. 세션의 설정옵션으로 GPU를 특정하지 않기(allow_soft_placement=True),
        # 연산이 어느디바이스로 설정되었는지 보여주여주지 않기(log_device_placement=False)
        session_conf = tf.ConfigProto(allow_soft_placement=True,
                                      log_device_placement=False)
        sess = tf.InteractiveSession(config=session_conf)  # !!
        # 세션또한 Context manager를 사용하여 세션의 열고 닫는 처리를 자동으로 해준다.
        with sess.as_default():
            # CNN객체를 생성한다. 파라미터 = 문장의 최대길이(sequence_length):912, 분류 카테고리수(num_classes):11,
            # 사전에 등록된 단어수(vocab_size):52943, 워드임베딩 사이즈(embedding_size):50,
            # CNN필터(커널)의 크기는 3x3,4x4,5x5 , 필터의 갯수는 총 32 개,
            # 오버피팅 방지를 위한 가중치 영향력 감소 수치(l2_reg_lambda):0.0
            cnn = TextCNN(
                sequence_length=x_train.shape[1],  # 들어온 문장의 최대 길이
                num_classes=y_train.shape[1],  # 라벨의 개수 (= One-hot vector의 길이)
                vocab_size=len(vocab_processor.vocabulary_),  # 단어의 수
                embedding_size=params['embedding_dim'],
                filter_sizes=list(map(int, params['filter_sizes'].split(","))),
                num_filters=params['num_filters'],
                l2_reg_lambda=params['l2_reg_lambda'],
                vec_dir=model_name  # @
            )
            global_step = tf.Variable(0, name="global_step",
                                      trainable=False)  # !!원본 바꾼것
            # Cost function으로 Adam Optimizer사용
            optimizer = tf.train.AdamOptimizer(1e-3)
            # cnn의 loss(오차) 값을 파리미터로 받아 점진하강.
            grads_and_vars = optimizer.compute_gradients(cnn.loss)
            # 학습에 사용할 함수 정의(session.run에서 사용됨).
            tf.summary.scalar("cnn_loss", cnn.loss)  # @@@
            tf.summary.scalar("cnn_accuracy", cnn.accuracy)  # @@@

            train_op = optimizer.apply_gradients(grads_and_vars,
                                                 global_step=global_step)

            # saver를 사용해 학습 내용을 저장
            saver = tf.train.Saver()

            # One training step: train the model with one batch
            # train_step 은 모델을 학습하는 하나의 묶음(batch)이다. 만약 batch size가 50이라면 50번의 Traning과 그에 따른 50번의 Test가 실행되게 된다.
            def train_step(x_batch, y_batch):
                # 입력/예측 출력값을 넣어줌으로서 학습/평가를 할수 있도록 한다.
                feed_dict = {
                    cnn.input_x: x_batch,
                    cnn.input_y: y_batch,
                    cnn.dropout_keep_prob: params['dropout_keep_prob']
                }  # Overfitting을 줄이기 위해,  Dropout(신경망 노드 탈락시키기) 확률을 지정.
                # 위에서 설정한 값들을 사용해 학습을 시작한다.
                _, step, loss, acc = sess.run(
                    [train_op, global_step, cnn.loss, cnn.accuracy], feed_dict)

            # One evaluation step: evaluate the model with one batch
            # dev_step 은 학습 결과 묶음(batch)를 평가(Evaluation)하는 메소드이다.
            def dev_step(x_batch, y_batch):
                # 평가시에는 dropout은 사용하지 않는다.(dropout_keep_prob:1.0 => off)
                feed_dict = {
                    cnn.input_x: x_batch,
                    cnn.input_y: y_batch,
                    cnn.dropout_keep_prob: 1.0
                }
                # 평가시에는 학습용 train_op 파라미터는 넣지 않는다.
                step, loss, acc, num_correct, summary, scores, final_scores = sess.run(
                    [
                        global_step, cnn.loss, cnn.accuracy, cnn.num_correct,
                        merged, cnn.scores, cnn.final_scores
                    ], feed_dict)  # @@@

                for j in final_scores:  # 각 final_scores에서 최대값 얻어오기
                    max_final_scores = max(j)
                    list_max_final_scores.append(
                        max_final_scores)  # 각 최대값을 리스트에 추가

                min_final_scores = min(list_max_final_scores)  # 리스트에서 가장 작은 값
                writer.add_summary(summary, step)  # @@@

                return num_correct, min_final_scores

            # 사용된 단어들을 ID에 매핑시켜 차후 예측시에 사용한다.(학습시에는 사용하지 않음)
            vocab_processor.save(os.path.join(out_dir, "vocab.pickle"))
            # 텐서플로우에서 사용하는 변수들을 초기화

            ckpt = tf.train.get_checkpoint_state(
                model_dir +
                "/checkpoints")  # checkpoint 얻는다.(모델의 Variable값을 얻어옴) #!!
            if ckpt and tf.train.checkpoint_exists(
                    ckpt.model_checkpoint_path):  # 모델 checkpoint가 존재하면 #!!
                print("다음 파일에서 모델을 읽는 중 입니다..",
                      ckpt.model_checkpoint_path)  # !!
                saver.restore(sess, ckpt.model_checkpoint_path
                              )  # checkpoint파일에서 모델의 변수값을 얻어온다. #!!
            else:  # 모델 checkpoint가 존재하지 않는다면 #!!
                print("새로운 모델을 생성하는 중 입니다.")  # !!
                sess.run(tf.global_variables_initializer())  # !!

            # Training starts here
            # 학습의 총 배치 갯수를 세팅한다. batch_iter 함수는 generator형식으로 작성되어있어서, 아래처럼 초기화를 해놓으면, for문안에서 배치단위로 값을 돌려주게 되어있다.
            # 한번에 학습단위묶음은 37개(batch_size=37). 학습데이터는 전체 학습에 한번 씩만 사용할것이다. (num_epochs=1)
            train_batches = data_helper.batch_iter(list(zip(x_train, y_train)),
                                                   params['batch_size'],
                                                   params['num_epochs'])
            # 최고의 정확성을 저장하기 위한 변수
            best_accuracy, best_at_step = 0, 0

            merged = tf.summary.merge_all()  # @@@
            writer = tf.summary.FileWriter("./logs", graph=graph)  # @@@
            """Step 6: train the cnn model with x_train and y_train (batch by batch)"""
            for train_batch in train_batches:
                # zip을 사용하여 배치렬로 x(입력)과 y(기대출력)값을 각각 뽑아낸다.
                x_train_batch, y_train_batch = zip(
                    *train_batch
                )  # *는 unpack 하는거. https://stackoverflow.com/questions/2921847/what-does-the-star-operator-mean 참고.
                # 배치단위로 학습 진행
                train_step(x_train_batch, y_train_batch)
                current_step = tf.train.global_step(sess, global_step)
                # 현재 학습 회차가 evaluate 할 순서이면 evaluate를 한x_dev다. 기본은 200번 마다.
                """Step 6.1: evaluate the model with x_dev and y_dev (batch by batch)"""
                if current_step % params['evaluate_every'] == 0:
                    # 개발용 데이터를 배치단위로 가져온다.
                    dev_batches = data_helper.batch_iter(
                        list(zip(x_dev, y_dev)), params['batch_size'], 1)
                    total_dev_correct = 0
                    for dev_batch in dev_batches:
                        x_dev_batch, y_dev_batch = zip(*dev_batch)
                        # 학습된 모델에 개발용 배치 데이터를 넣어서 예측 성공 갯수를 누적한다.
                        num_dev_correct, _ = dev_step(x_dev_batch, y_dev_batch)
                        total_dev_correct += num_dev_correct
                    # 모델의 정확성을 화면에 출력한다.
                    dev_accuracy = float(total_dev_correct) / len(y_dev)
                    logging.critical(
                        'Accuracy on dev set: {}'.format(dev_accuracy))

                    # 가장 예측 확률이 좋게 나온 모델을 저장한다. 기준은 dev_accuracy가 가장 좋게 나온 step의 모델이다.
                    """Step 6.2: save the model if it is the best based on accuracy on dev set"""
                    if dev_accuracy >= best_accuracy:
                        best_accuracy, best_at_step = dev_accuracy, current_step
                        path = saver.save(sess,
                                          checkpoint_prefix,
                                          global_step=current_step)  # !!
                        tf.Print(path, [path], "This is saver : ")
                        logging.critical('Saved model at {} at step {}'.format(
                            path, best_at_step))
                        logging.critical(
                            'Best accuracy is {} at step {}'.format(
                                best_accuracy, best_at_step))

            # 학습데이터와 Test데이터는 9:1로 나누었다.
            # Test데이터는 학습에 사용되지 않은 데이터로서, 학습된 모델이 객관성을 가지는지 확인하기 위한 용도이다
            """Step 7: predict x_test (batch by batch)"""
            test_batches = data_helper.batch_iter(list(zip(x_test, y_test)),
                                                  params['batch_size'], 1)
            total_test_correct = 0
            for test_batch in test_batches:
                x_test_batch, y_test_batch = zip(*test_batch)
                num_test_correct, min_final_scores = dev_step(
                    x_test_batch, y_test_batch)
                my_min = min_final_scores  # by odg
                total_test_correct += num_test_correct

            f = open(checkpoint_dir + "_min.txt", "w")
            f.write(str(my_min))
            f.close()

            test_accuracy = float(total_test_correct) / len(y_test)

            logging.critical('테스트셋 Accuracy {}, best 모델 {}'.format(
                test_accuracy,
                path))  # 자꾸 여기서 오류남. 이건 그냥 log 띄우는거라 없어도 될거같은데..
            logging.critical('트레이닝 완료')
Пример #13
0
import tensorflow as tf
import data_helper

encoder_sentence_length = 10
decoder_sentence_length = 10

print 'Building vocab ...'
all_input_sentences = data_helper.get_input_sentence()
all_target_sentences = data_helper.get_target_sentence()
encoder_vocab, encoder_reverse_vocab, encoder_vocab_size = data_helper.build_vocab(
    all_input_sentences)
decoder_vocab, decoder_reverse_vocab, decoder_vocab_size = data_helper.build_vocab(
    all_target_sentences, is_target=True)

max_epoch = 2000
layer_num = 2
hidden_size = 30
embedding_size = 30

encoder_inputs = tf.placeholder(tf.int32, [None, encoder_sentence_length],
                                name='encoder_inputs')
encoder_sequence_length = tf.placeholder(tf.int32, [None],
                                         name='encoder_sequence_length')
decoder_inputs = tf.placeholder(tf.int32, [None, decoder_sentence_length + 1],
                                name='decoder_inputs')

with tf.variable_scope('embedding'):
    encoder_embedding = tf.get_variable(
        'encoder_embedding',
        initializer=tf.random_uniform([encoder_vocab_size + 1,
                                       embedding_size]))
Пример #14
0
        total_busi_num += busi_num
        total_busi_correct_num += busi_correct_num
        total_other_num += other_num
        total_other_correct_num += other_correct_num

    accuracy=float(total_correct_num)/total_num
    busi_acc = float(total_busi_correct_num) / total_busi_num
    other_acc = float(total_other_correct_num) / total_other_num
    logger.info("validation success")

    return accuracy, busi_acc, other_acc
#------------------------------ evaluate model end -------------------------------------

#------------------------------------load data -------------------------------
label2id, id2label = load_label(FLAGS.label_file)
id2word, word2id = build_vocab(FLAGS.word_file)
embeddings = load_embedding(FLAGS.embedding_file)
logger.info("load label, word, embedding finished")
train_valid_x, train_valid_y, class_weight = load_data(FLAGS.train_file, word2id, label2id, FLAGS.num_unroll_steps)

# cal label weight
class_weight_mean = np.mean(class_weight.values())
label_weight = {}
for label, weight in class_weight.items():
    label_weight[label2id.get(label)] = 1. / (weight / class_weight_mean)
label_weight = np.array([label_weight[ix] for ix in sorted(label_weight.keys())], dtype=np.float32)

# split data
train_data, valid_data = split_train_by_ratio(zip(train_valid_x, train_valid_y), 0.01)
train_x, train_y = zip(*train_data)
valid_x, valid_y = zip(*valid_data)
Пример #15
0
入口函数,进行train,test和predict
主要负责:
  1. 参数的组装
  2. 数据的处理
  3. train等入口函数的处理
"""
import argparse
from data_helper import create_iter, build_vocab
from train_eval import train
from model import Config, TextRnn
from predict import predict

parser = argparse.ArgumentParser(description='Lstm Text Classification')
parser.add_argument('--operation',
                    default="train",
                    type=str,
                    help='train test or predict')
args = parser.parse_args()

char2index, index2char = build_vocab()
config = Config(len(char2index))
train_iter, eval_iter = create_iter(config)
model = TextRnn(config)

operation = args.operation
if operation == "train":
    print("*****[ begin to train ]*****")
    train(config, model, train_iter, eval_iter)
elif operation == "predict":
    print("*****[ begin to predict ]*****")
    predict(config, model)
Пример #16
0
        metrics.classification_report(y_test_cls,
                                      y_pred_cls,
                                      target_names=categories))

    # 混淆矩阵
    print("Confusion Matrix...")
    cm = metrics.confusion_matrix(y_test_cls, y_pred_cls)
    print(cm)

    time_dif = get_time_dif(start_time)
    print("Time usage:", time_dif)


if __name__ == '__main__':
    config = RNNConfig()  # 获取配置参数
    if not os.path.exists(vocab_dir):  # 如果不存在词汇表,重建
        build_vocab(train_dir, vocab_dir, config.vocab_size)
    categories, cat_to_id = read_category()
    words, word_to_id = read_vocab(vocab_dir)
    config.vocab_size = len(words)
    # print(word_to_id)
    # print(words)
    if not os.path.exists(vector_word_npz):
        export_word2vec_vectors(word_to_id, words_embeding, vector_word_npz)
    config.pre_trianing = get_training_word2vec_vectors(vector_word_npz)
    model = TextRNN(config)
    option = ''
    if option == 'train':
        train()
    else:
        test()
Пример #17
0
tf.flags.DEFINE_integer("checkpoint_every", 100,
                        "Save model after this many steps (default: 100)")
# Misc Parameters
tf.flags.DEFINE_boolean("allow_soft_placement", True,
                        "Allow device soft device placement")
tf.flags.DEFINE_boolean("log_device_placement", False,
                        "Log placement of ops on devices")

FLAGS = tf.flags.FLAGS
FLAGS.flag_values_dict()

timeStamp = time.strftime("%Y%m%d%H%M%S", time.localtime(int(time.time())))

print(("Loading data..."))

vocab = data_helper.build_vocab()  # 加载vocab,需修改参数 QA Web 默认是QA
embeddings = data_helper.load_vectors(vocab)  #需修改参数 QA Web
alist = data_helper.read_alist("WebAP")  # 加载所有例子里的回答
qs = data_helper.qname("WebAP/qeo-train.txt")
number = data_helper.qid("WebAP/qeo-train.txt")
numbertest = data_helper.qid("WebAP/qeo-test.txt")
qstest = data_helper.qname("WebAP/qeo-test.txt")
test1List = data_helper.loadTestSet(dataset="WebAP", filename="term-test")
test2List = data_helper.loadTestSet(dataset="WebAP", filename="term-train")

print("Load done...")
precision = 'WebAP/log/test1.dns' + timeStamp
precision1 = 'WebAP/logtrain/train.dns' + timeStamp
loss_precision = 'WebAP/log/qterm.gan_loss' + timeStamp

from functools import wraps