我对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