티스토리 뷰
CSV 파일을 이용해서 데이터를 가져오고
간단한 1 layer regression 을 해 보겠습니다.
지난 포스트에서 사용했던 데이터를 이용해서
모델을 만들어 보겠습니다.
입력 데이터는 19개 이고 출력 데이터는 0~1 의 실수를 갖는 모델 입니다.
아래 그림과 같이 19개의 weight와 1개의 Bias가 필요 합니다.
수학 공식으로 나타내면
y = w1*x1+b1 +w2*x2+b1+ w3*x3+b1 + ... + w17*x17+b1 + w18*x18+b1 _w19*x19+b1
제 데이터는 200만개 정도로
200만개 중에서 랜덤으로 10000개씩 Batch 처리 해서 데이터를 불러와 학습하고
오차율을 계산 해 보겠습니다.
import tensorflow as tf
with tf.name_scope("input") as scope:
x = tf.placeholder(tf.float32, [None, 19])
with tf.name_scope("weight") as scope:
#W = tf.Variable(tf.zeros([19, 1]))
W = tf.Variable(tf.random_normal([19,1], stddev=0.35))
with tf.name_scope("bias") as scope:
b = tf.Variable(tf.random_normal([1], stddev=0.35))
with tf.name_scope("layer1") as scope:
y = tf.add(tf.matmul(x, W), b)
with tf.name_scope("y_") as scope:
y_ = tf.placeholder(tf.float32, [None, 1])
with tf.name_scope("cost") as scope:
squared_deltas1 = tf.square(y_ - y)
squared_deltas = tf.sqrt(squared_deltas1)
cost = tf.reduce_mean(squared_deltas)
cost_sum = tf.summary.scalar("cost",cost)
with tf.name_scope("train") as scope:
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
def read_my_file_format(filename_queue):
reader = tf.TextLineReader(skip_header_lines=1)
_, value = reader.read(filename_queue)
record_defaults = [[1], [1], [1], [1], [1],[1], [1], [1], [1], [1],[1], [1], [1], [1], [1],[1], [1], [1], [1], [1]]
record_defaults = [tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32),
tf.constant([1], dtype=tf.float32)]
col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15 ,col16, col17, col18, col19, col20 = tf.decode_csv(value, record_defaults=record_defaults)
features = tf.pack([col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15 ,col16, col17, col18, col19])
label = tf.pack([col20])
return features, label
def input_pipeline(batch_size, num_epochs=None):
min_after_dequeue = 10000
capacity = min_after_dequeue + 3 * batch_size
filename_queue = tf.train.string_input_producer(["sampledata1999_2008_5.csv"], num_epochs=num_epochs, shuffle=True)
example, label = read_my_file_format(filename_queue)
example_batch, label_batch = tf.train.shuffle_batch([example, label],
batch_size=batch_size,
capacity=capacity,
min_after_dequeue=min_after_dequeue)
return example_batch, label_batch
examples, labels = input_pipeline(10000,1)
i = 0;
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
#init_op = tf.global_variables_initializer()
sess = tf.Session()
merged = tf.summary.merge_all()
trainwriter =tf.summary.FileWriter("./board/custom", sess.graph)
# Initialize the variables (like the epoch counter).
sess.run(init_op)
print(W.eval(session = sess))
# Start input enqueue threads.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
try:
while not coord.should_stop():
i = i + 1
example_batch, label_batch = sess.run([examples, labels])
sess.run(train_step, feed_dict={x: example_batch, y_: label_batch})
if i % 1 == 0:
summary = sess.run(merged, feed_dict={x: example_batch, y_: label_batch})
trainwriter.add_summary(summary,i)
print(cost.eval(feed_dict={x: example_batch, y_: label_batch}, session = sess))
except tf.errors.OutOfRangeError:
print('Done training -- epoch limit reached')
finally:
# When done, ask the threads to stop.
coord.request_stop()
# Wait for threads to finish.
coord.join(threads)
sess.close()
코드는 크게 6가지 부분으로 나뉠 수 있습니다.
1. 값들이 저장될 공간을 할당
2. 값들의 연결 (Layer)를 정의
3. 비용 함수를 생성
4. 비용을 감소시키는 쪽으로 w,b를 조정하는 함수
5. 학습을 시행
6. 학습 결과를 시험
코드별로 살펴보겠습니다,.
1. 값들이 저장될 공간을 할당
with tf.name_scope("input") as scope:
x = tf.placeholder(tf.float32, [None, 19])
with tf.name_scope("weight") as scope:
#W = tf.Variable(tf.zeros([19, 1]))
W = tf.Variable(tf.random_normal([19,1], stddev=0.35))
with tf.name_scope("bias") as scope:
b = tf.Variable(tf.random_normal([1], stddev=0.35))
with tf.name_scope("y_") as scope:
y_ = tf.placeholder(tf.float32, [None, 1])
2. 값들의 연결 (Layer)를 정의
with tf.name_scope("layer1") as scope:
y = tf.add(tf.matmul(x, W), b)
3. 비용 함수를 생성
with tf.name_scope("cost") as scope:
squared_deltas1 = tf.square(y_ - y)
squared_deltas = tf.sqrt(squared_deltas1)
cost = tf.reduce_mean(squared_deltas)
cost_sum = tf.summary.scalar("cost",cost)
4. 비용을 감소시키는 쪽으로 w,b를 조정하는 함수
with tf.name_scope("train") as scope:
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
5. 학습을 시행
example_batch, label_batch = sess.run([examples, labels])
sess.run(train_step, feed_dict={x: example_batch, y_: label_batch})
6. 학습 결과를 시험
print(cost.eval(feed_dict={x: example_batch, y_: label_batch}, session = sess))
이 예제대로 시행하면 Tensorboard도 같이 만들어 집니다.
Cost가 점점 줄어드는 쪽으로 잘 학습되고 있는지 확인 할 수 있습니다.
오차가 30% 수준에서 더 줄어들지 않았지만
그래도 첫 학습에 성공 했습니다.
'Tensorflow Step By Step' 카테고리의 다른 글
Tensorflow multi layer Regression (0) | 2017.02.26 |
---|---|
Tensorflow CSV File Read 2 (0) | 2017.02.22 |
Tensorflow CSV File Read 1 (0) | 2017.02.16 |
Tensorboard 사용하기 2 (0) | 2017.02.12 |
Tensorboard 사용하기 1 (5) | 2017.02.12 |
- Total
- Today
- Yesterday