Demo entry 6662012

ㄴㅇ

   

Submitted by anonymous on Nov 23, 2017 at 22:31
Language: Python. Code size: 7.5 kB.

# -*- coding: utf-8 -*-

from __future__ import print_function

import tensorflow as tf
import numpy as np
import csv
import argparse
from sklearn.preprocessing import MinMaxScaler

parser = argparse.ArgumentParser()
parser.add_argument("--batch_size", type=int, default=2, help="batch size")
parser.add_argument("--H1", type=int, default=16, help="# of neurons in hidden layer1")
parser.add_argument("--H2", type=int, default=16, help="# of neurons in hidden layer1")

FLAGS = parser.parse_args()

#=================================hyper-parameter setting====================================#
# Dataset property
#num_examples = 12936 # for all data
num_examples = 4000 # without 0 label(non-labeled)
validation_examples = 465 # validation datasets
validation_epochs = 3 # validation epochs
val_batch_size = 465 # batch size for validataion
validation_steps = int(validation_examples/val_batch_size) # validation steps

# Parameters
starter_learning_rate = 0.1 # initial learning rate
epochs_decaying_lr = 20 # the number of epochs with decaying learning rate
lr_decaying = 0.1 # learning rate decaying rate
batch_size = FLAGS.batch_size # batch size for training
training_epochs = 200 # maximum epochs to train
display_step = int(num_examples/batch_size) # 1epoch

global_step = tf.contrib.framework.get_or_create_global_step() # step variable
learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, epochs_decaying_lr*num_examples/batch_size, lr_decaying, staircase=True) # learning rate policy

# Network Parameters
n_hidden_1 = FLAGS.H1 # 1st layer number of neurons
n_hidden_2 = FLAGS.H2 # 2nd layer number of neurons
n_input = 50 # ADNI data input
n_classes = 3 # ADNI total classes

print("================parameter setting==================")
print("Batch size = %d" % FLAGS.batch_size)
print("H1 = %d" % FLAGS.H1)
print("H2 = %d" % FLAGS.H2)
print("===================================================")
#============================================================================================#

def DataMinMaxScaler(filename):
  #post_data = np.empty(shape=(,), dtype=np.float32)
  pre_data = np.loadtxt(filename + ".csv", delimiter=",", dtype=np.float32, skiprows=1)
  #print(type(pre_data[0:-1]))
  # MinMax Scaling using sklearn
  scaler = MinMaxScaler(feature_range=(0,5))
  pre_data[:,0:-1] = scaler.fit_transform(pre_data[:,0:-1])
  #print(pre_data[:,-1])
  #for c in range(pre_data.shape[1]):
  #  print(c)
  #  scaler.fit(pre_data[:,c])
  #  pre_data[:,c] = scaler.transform(pre_data[:,c])
  #  print(pre_data[:,c])

  with open(filename + "_post.csv","wb") as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    for row in range(pre_data.shape[0]):
        writer.writerow(pre_data[row])
  #print(post_data)
  #print(pre_data)
  #f.write(pre_data)
  #f.close()


# Import ADNI data (shuffle batch)
def input_reading(filename, batch_size):
  filename_queue = tf.train.string_input_producer([filename], shuffle=False)
  reader = tf.TextLineReader(skip_header_lines=1)
  key, value = reader.read(filename_queue)

  record_defaults=[[0.]]*51

  data = tf.decode_csv(value, record_defaults=record_defaults)


  min_after_dequeue = 100000
  capacity = min_after_dequeue + 3 * batch_size

  feature_batch, label_batch = tf.train.shuffle_batch([data[0:-1], data[-1:]], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue, num_threads=4)
  return feature_batch, tf.cast(tf.reshape(label_batch,[batch_size]),tf.int32)

# tf Graph input
#X = tf.placeholder("float", [None, n_input]) # for placeholder features
#Y = tf.placeholder("float", [None, n_classes]) # for placeholder labels
DataMinMaxScaler("andl_tw_NA0_0labelout_training")
DataMinMaxScaler("andl_tw_NA0_0labelout_validation")

X, Y = input_reading('andl_tw_NA0_0labelout_training_post.csv', batch_size)
val_X, val_Y = input_reading('andl_tw_NA0_0labelout_validation_post.csv', val_batch_size)

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

# Create model
def multilayer_perceptron(X):
    # Hidden fully connected layer with 256 neurons
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(X, weights['h1']), biases['b1']))
    # Hidden fully connected layer with 256 neurons
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']))
    # Output fully connected layer with a neuron for each class
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer


# Construct model
logits = multilayer_perceptron(X)
val_logits = multilayer_perceptron(val_X)

# Define loss and optimizer
val_pred = tf.nn.softmax(val_logits)
cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
    logits=logits, labels=Y))
cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
val_correct_prediction = tf.equal(tf.argmax(val_pred, 1), tf.cast(val_Y,tf.int64))

# Calculate accuracy
val_accuracy_op = tf.reduce_mean(tf.cast(val_correct_prediction, "float"))
val_accuracy_summary = tf.summary.scalar('accuracy',val_accuracy_op)

tf.add_to_collection('losses', cross_entropy_mean)
loss_op = tf.add_n(tf.get_collection('losses'), name='total_loss')
loss_summary= tf.summary.scalar('loss',loss_op)
summary = tf.summary.merge_all()

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op, global_step=global_step)
hooks = [tf.train.StopAtStepHook(last_step=training_epochs*num_examples/batch_size)]

init = tf.global_variables_initializer()
# Session
with tf.train.MonitoredTrainingSession(save_summaries_steps=10, hooks=hooks) as sess:
    sess.run(init)
    summary_writer = tf.summary.FileWriter('/tmp/TFlog', sess.graph)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    epoch = 0
    # Training cycle
    while not sess.should_stop():
        avg_cost = 0.
        avg_acc_train = 0.
        total_batch = int(num_examples/batch_size)
        # Loop over all batches
        for i in range(total_batch):
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c, step = sess.run([train_op, loss_op, global_step])
            # Compute average loss
            avg_cost += c / total_batch
            #avg_acc += a / total_batch
        # Display logs per epoch step
        if step % display_step == 0:
            epoch += 1
            print("Epoch:", '%04d' % epoch, "trainig loss={:.9f}".format(avg_cost))
            if epoch % validation_epochs == 0:
                avg_acc = 0.
                for j in range(validation_steps):
                    a = sess.run([val_accuracy_op])
                    avg_acc += a[0] / validation_steps
                print("validation accuracy=", '%.3f' % avg_acc)
            summary_str = sess.run(summary)
            summary_writer.add_summary(summary_str, epoch)

    print("Optimization Finished!")
    coord.request_stop()
    coord.join(threads)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).