티스토리 뷰
가지고 있는 데이터를 사용 해서 학습을 시켜 보겠습니다.
기존 예제들은 데이터의 포멧도 잘 맞춰져 있어서 파일을 읽어오고
배치로 입력하고 하는 과정이 간단하게 되어 있습니다.
하지만 막상 실제 데이터를 사용하려니
데이터를 선정하고 데이터를 가공하고 데이터를 입력하는 것이 어려운 것 같습니다.
오늘은 데이터를 읽어 오는 것을 해 보겠습니다.
1. 데이터 준비
저는 2GB 정도 되는 데이터를 가지고 있습니다.
Mysql에 저장이 되어 있고 이것을 CSV 파일로 Import 하였습니다.
2GB 정도 되니 더이상 엑셀로 열리지 않습니다.
데이터를 10000개만 추출하여 파일을 만들고 시험 해 보았습니다.
데이터는 아래와 같이
주황색 부분에 Raw Data가 12개, 초록색 부분에 Raw Data를 가공하여 만든 24개의 Data, 빨간색 부분이 Label 입니다.
2. 데이터 읽어오기
import tensorflow as tf
filename_queue = tf.train.string_input_producer(["sampledata10000__.csv"])
reader = tf.TextLineReader(skip_header_lines=1)
key, 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],[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),
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, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31, col32, col33, col34, col35, col36, col37 = 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, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31, col32, col33, col34, col35, col36, col37 ])
with tf.Session() as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(10000):
# Retrieve a single instance:
example, label = sess.run([features, col37])
print(example)
coord.request_stop()
coord.join(threads)
print(example) 을 하여 example에 데이터가 저장된 것을 확인 할 수 있습니다.
에러가 발생했던 부분은 2가지 인데
첫번째로
reader = tf.TextLineReader() 이렇게 사용하면 데이터를 첫번째 줄 부터 가져온다
그런데 첫번째 줄에는 데이터 Column 이름이 string으로 들어있어서
데이터 형식이 맞지 않아 에러가 발생한다
reader = tf.TextLineReader(skip_header_lines=1)
이렇게 써 주면 첫번째 줄을 넘어가면서 해결되었습니다.
두번째로
예제에는 아래와 같이 사용했는데
record_defaults = [ [1],[1],[1],[1],[1] ]
사용하는 데이터의 포멧이 float 일 경우 에러가 발생합니다.
record_defaults = [tf.constant([1], dtype=tf.float32), tf.constant([1], dtype=tf.float32)] 이런 식으로
사용 데이터에 맞게 자료형을 명시해 주면 해결됩니다.
'Tensorflow Step By Step' 카테고리의 다른 글
Tensorflow 1 layer Regression (0) | 2017.02.25 |
---|---|
Tensorflow CSV File Read 2 (0) | 2017.02.22 |
Tensorboard 사용하기 2 (0) | 2017.02.12 |
Tensorboard 사용하기 1 (5) | 2017.02.12 |
Tensorflow Tutorial (MNIST for beginer) (0) | 2017.01.27 |
- Total
- Today
- Yesterday