我對tensorflow相當陌生,並且成功地完成了必要的MNIST教程。在張量流中使用csv訓練數據RNN
我想用一組CSV數據訓練一個簡單的RNN。數據是33個特徵,最後是一個二進制輸出變量(如此34列)。
我已經實現了一次讀入一行的csv閱讀器。我正在嘗試讀取該行並將其傳遞到我的張量流圖中。我覺得「TensorFlow-way」開始變得更加清晰,但也有一些基本的缺失 - 特別是因爲它涉及到將數據流式傳輸到您的模型中。
我已經包含了我在下面做的一個例子。大部分的代碼已被剝離爲清楚起見,但重要的部分仍然是:
import tensorflow as tf
import sys
import datapipe as datapipe
learning_rate = 0.001
n_features = 33
n_hidden = 100 # number of features in the hidden layer - I just made this up
n_classes = 2 # 0 or 1 - a binary classification
x = tf.placeholder('float', [None, 1, n_features])
y = tf.placeholder('float', [None, n_classes])
transform = tf.transpose(x)
with tf.session() as sess:
sess.run(tf.initialize_all_variables())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
datapipe = datapipe.Datapipe(filename='training.csv', features=33, epochs=100)
while not coord.should_stop():
nextline = datapipe.nextline()
# I basically want to run "transform" with the nextline of the csv file
stuff = sess.run(transform, feed_dict={ x: nextline })
coord.request_stop()
coord.join(threads)
而且datapipe
是:
import tensorflow as tf
class Datapipe:
def __init__(self, filename=None, features=None, epochs=100):
self.filename = filename
self.features = features
self.epochs = epochs
self.defaults = []
for i in range(self.features):
self.defaults.append([]) # require all fields to be present
def nextline(self):
file_queue = tf.train.string_input_producer([self.filename], num_epochs=self.epochs, shuffle=False)
reader = tf.TextLineReader()
key, csv_str = reader.read(file_queue)
return tf.pack(tf.decode_csv(csv_str, record_defaults=self.defaults))
當我運行這個例子,我得到的錯誤:
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.
謝謝你的幫助!
編輯
我的問題基本上是:我怎麼能養活文件數據(如CSV)成tensorflow模式?(教程是沒有幫助)
編輯2016年12月9日
基於由塞爾吉答案,我現在這樣做:
with open('../data/training2.csv') as f:
reader = csv.reader(f)
for line in reader:
arr = np.array(line)
x = arr[0:len(arr)-1:1]
y = arr[len(arr)-1:len(arr):1]
sess.run(transform, feed_dict={ x: x, y: y })
聽起來不錯。我想使用本地張量流csv閱讀器方法,因爲我也想用本地方式批量處理數據(例如'tf.train.batch')。有沒有辦法將tensorflow對象變成一個numpy數組? – Zach