>

custome 사전 정의 함수 trainDNN 를 사용하여 RNN 및 LSTM 모델을 실행하고 있습니다.

import tensorflow as tf
from tensorflow.contrib.layers import fully_connected
import h5py
import time
from sklearn.utils import shuffle
def trainDNN(path, n_days, n_features, n_neurons, 
            train_sequences, train_lengths, train_y,
            test_sequences, test_y, test_lengths,
            lstm=False, n_epochs=50, batch_size=256,
            learning_rate=0.0003, TRAIN_REC=8, TEST_REC=8):
    # we're doing binary classification
    n_outputs = 2
    # this is the initial learning rate
    # adam optimzer decays the learning rate automatically
#     learning_rate = 0.0001
    #learning rate decay is determined by epsilon
    epsilon = 0.001
    # setup the graph
    tf.reset_default_graph()
    # inputs to the network
    X = tf.placeholder(tf.float32, [None, n_days, n_features])
    y = tf.placeholder(tf.int32, [None])
    seq_length = tf.placeholder(tf.int32, [None])
    # the network itself
    cell = tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons) if lstm else tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)
    outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32, sequence_length=seq_length)
    logits = fully_connected(states[-1] if lstm else states, n_outputs)
    # the training process (minimize loss) including the training operatin itself
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)
    loss = tf.reduce_mean(xentropy)
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, epsilon=epsilon)
    training_op = optimizer.minimize(loss)
    # hold onto the accuracy for the logwriter
    correct = tf.nn.in_top_k(logits, y, 1)
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
    # this saves the network for later querying
    # currently only saves after all epochs are complete
    # but we could for example save checkpoints on a
    # regular basis
    saver = tf.train.Saver()
    # this is where we save the log files for tensorboard
    now = int(time.time())
    name = 'lstm' if lstm else 'rnn'
    root_logdir = path+"tensorflow_logs/{}/{}-{}/".format(name.upper(), name, now)
    train_logdir = "{}train".format(root_logdir)
    eval_logdir = "{}eval".format(root_logdir)
    print('train_logdir', train_logdir)
    print('eval_logdir', eval_logdir)
    # scalars that are written to the log files
    loss_summary = tf.summary.scalar('loss', loss)
    acc_summary = tf.summary.scalar('accuracy', accuracy)
    # summary operation and writer for the training data
    train_summary_op = tf.summary.merge([loss_summary, acc_summary])
    train_writer = tf.summary.FileWriter(train_logdir, tf.get_default_graph())
    # summary operation and writer for the validation data
    eval_summary_op = tf.summary.merge([loss_summary, acc_summary])
    eval_writer = tf.summary.FileWriter(eval_logdir, tf.get_default_graph())
    # initialize variables
    init = tf.global_variables_initializer()
    n_batches = len(train_sequences) // batch_size
    print(n_batches, 'batches of size', batch_size, n_epochs, 'epochs,', n_neurons, 'neurons')
    with tf.Session() as sess:
        # actually run the initialization
        init.run()
        start_time = time.time()
        for epoch in range(n_epochs):
            # at the beginning of each epoch, shuffle the training data
            train_sequences, train_y, train_lengths = shuffle(train_sequences, train_y, train_lengths)
            for iteration in range(n_batches):
                # extract the batch of training data for this iteration
                start = iteration*batch_size
                end = start+batch_size
                X_batch = train_sequences[start:end]
                y_batch = train_y[start:end]
                y_batch = y_batch.ravel()
                seq_length_batch = train_lengths[start:end]
                # every TRAIN_REC steps, save a summary of training accuracy & loss
                if iteration % TRAIN_REC == 0:
                    train_summary_str = train_summary_op.eval(
                        feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
                    )
                    step = epoch * n_batches + iteration
                    train_writer.add_summary(train_summary_str, step)
                    # without this flush, tensorboard isn't always current
                    train_writer.flush()
                # every TEST_REC steps, save a summary of validation accuracy & loss
                # TODO: this runs all validation data at once. if validation is
                # sufficiently large, this will fail. better would be to either
                # pick a random subset of validation data, or even better, run
                # validation in multiple batches and save the validation accuracy 
                # & loss based on the aggregation of all of the validation batches.
                if iteration % TEST_REC == 0:
                    summary_str = eval_summary_op.eval(
                        feed_dict = {X: test_sequences, y: test_y.ravel(), seq_length: test_lengths}
                    )
                    step = epoch * n_batches + iteration
                    eval_writer.add_summary(summary_str, step)
                    # without this flush, tensorboard isn't always current
                    eval_writer.flush()
                # run training.
                # this is where the network learns.
                sess.run(
                    training_op,
                    feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
                )
            # after every epoch, calculate the accuracy of the last seen training batch 
            acc_train = accuracy.eval(
                feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
            )
            # after each epoch, calculate the accuracy of the test data
            acc_test = accuracy.eval(
                feed_dict = {X: test_sequences, y: test_y.ravel(), seq_length: test_lengths}
            )
            # print the training & validation accuracy to the console
            print(epoch, time.strftime('%m/%d %H:%M:%S'), "Accuracy train:", acc_train, "test:", acc_test)

        # save the model (for more training or inference) after all
        # training is complete
        save_path = saver.save(sess, root_logdir+"model_final.ckpt")
        # close the writers
        train_writer.close()
        eval_writer.close()    
        log(["{}-{} model score".format(name.upper(), now), percent(acc_test)])

위의 함수는 RNN 및 LSTM 모델을 시간 시퀀스 데이터에 대해 훈련시키고 이진 분류 점수를 출력합니다. 훈련 및 시험 점수가 인쇄되지만 RUC 및 LSTM의 이진 분류를 위해 AUC를 계산하고 ROC 곡선을 생성하는 방법을 알아 내려고 노력 중입니다.

업데이트 :

다음 스크립트를 사용하여 로짓과 예측을 평가했습니다.

n_epochs = 2
batch_size = 2000
n_batches = len(train_sequences) // batch_size
print(n_batches)
with tf.Session() as sess:
    init.run()
    #sess.run( tf.local_variables_initializer() )
    for epoch in range(n_epochs):
        train_sequences, train_y, train_lengths = shuffle(train_sequences, train_y, train_lengths)
        for iteration in range(n_batches):
            start = iteration*batch_size
            end = start+batch_size
            X_batch = train_sequences[start:end]
            y_batch = train_y[start:end]
            seq_length_batch = train_lengths[start:end]
            if iteration % 20 == 0:
                train_summary_str = train_summary_op.eval(
                    feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
                )
                step = epoch * n_batches + iteration
            if iteration % 200 == 0:
                summary_str = eval_summary_op.eval(
                    feed_dict = {X: test_sequences, y: test_y, seq_length: test_lengths}
                )
                step = epoch * n_batches + iteration
            sess.run(
                training_op,
                feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
            )
        acc_train = accuracy.eval(
            feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch}
        )
        acc_test = accuracy.eval(
            feed_dict = {X: test_sequences, y: test_y, seq_length: test_lengths}
        )
        probs = logits.eval(feed_dict = {X: test_sequences, y: test_y, seq_length: test_lengths})
        predictions = correct.eval(feed_dict = {logits:probs, y: test_y})
        print(epoch, "Train accuracy:", acc_train, "Test accuracy:", acc_test)# "Manual score:", score)

이것은 기본적으로 테스트 케이스 수와 동일한 행과 2 개의 이진 클래스 각각에 대한 확률을 포함하는 2 개의 열이있는 행렬 인 prob를 반환합니다. 예측 개체에는 예측이 올바른지 여부가 포함됩니다. ReLU 함수 확률 점수는 더 이상 양수 및 음수 예측에 대한 기본 0.5 컷오프를 기반으로하지 않기 때문에 시그 모이 드 함수 점수만큼 직관적이지 않기 때문에 회의적입니다. 오히려 예측은 어떤 클래스가 더 많은 확률을 갖는지에 따라 결정됩니다. ReLu 출력에서 ​​ROC 곡선을 생성 할 수 있습니까?

  • 답변 # 1

    tf.metrics.auc() 를 사용할 수 있습니다  이 목적을 위해. one-hot 인코딩 된 레이블과 이에 대한 예측이 필요하며 update_op 를 실행해야합니다.  여러 sess.run() 에 AUC를 축적하려고하면 반환합니다.  명령은 아래의 별도 섹션을 참조하십시오.

    코드에서 y_one_hot 를 만듭니다.   tf.one_hot() 와 함께 , 그리고 당신은 accuracy 직후 에이 모든 것을 넣을 것입니다  아마도 :

    y_one_hot = tf.one_hot( y, n_outputs )
    auc, auc_update_op = tf.metrics.auc( y_one_hot, logits )
    
    

    훈련 루프를 시작하기 전에, auc가 생성 한 지역 변수를 초기화해야합니다. 아마도 init.run() 직후에 :

    sess.run( tf.initialize_local_variables() )
    
    

    정확성을 실행할 때 auc 도 실행해야합니다.   accuracy 와 함께  와이즈 비즈   sess.run() 대신  이것처럼 (예상치 않은) :

    .eval()
    
    
    <시간> 여러 배치에 대한 누적

    # after every epoch, calculate the accuracy of the last seen training batch acc_train, auc_val = sess.run( [ accuracy, auc ], feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch} ) # after each epoch, calculate the accuracy of the test data acc_test, auc_val = sess.run( [ accuracy, auc ], feed_dict = {X: test_sequences, y: test_y.ravel(), seq_length: test_lengths} ) 의 누적 기능을 사용하려면  새 계산을 시작하려면 누적을 재설정해야합니다. 이를 위해서는 생성 된 로컬 변수를 수집해야합니다. 다음과 같이 auc를 만드십시오 :

    tf.metrics.auc()
    
    

    적산이 끝나면 auc의 내부 변수를 다음과 같이 재설정하십시오 :

    with tf.variable_scope( "AUC" ):
        auc, auc_update_op = tf.metrics.auc( predictions=y_pred, labels=y_true, curve = 'ROC' )
    auc_variables = [ v for v in tf.local_variables() if v.name.startswith( "AUC" ) ]
    auc_reset_op = tf.initialize_variables( auc_variables )
    
    

    또한 session.run( auc_reset_op ) 를 실행해야합니다.   auc_update_op 를 실행할 때마다  이렇게 :

    sess.run()
    
    
    # after every epoch, calculate the accuracy of the last seen training batch acc_train, auc_val, _ = sess.run( [ accuracy, auc, auc_update_op ], feed_dict = {X: X_batch, y: y_batch, seq_length: seq_length_batch} ) session.run( auc_reset_op ) # maybe you want to do this here... # after each epoch, calculate the accuracy of the test data acc_test, auc_val, _ = sess.run( [ accuracy, auc, auc_update_op ], feed_dict = {X: test_sequences, y: test_y.ravel(), seq_length: test_lengths} )

  • 이전 jmsserializerbundle - symfony 42와 함께 JMSSerializer를 사용하는 방법
  • 다음 java - 내 코드는 스레드 안전합니까? 그렇지 않은 경우 스레드 안전성을 얻는 방법은 무엇입니까?