用TFTS读取时间序列数据

在训练模型之前,需要将事件序列数据读入成为Tensor的形式。

TFTS库中提供了两个方便的读取器:

NumpyReader----用于从numpy数组中读入数据;

CSVReader----后者用于从CSV文件中读入数据。

x = np.array(range(1000))
noise = np.random.uniform(-0.2, 0.2, 1000)
y = np.sin(np.pi * x / 100) + x / 200. + noise

data = {
    tf.contrib.timeseries.TrainEvalFeatures.TIMES: x,
    tf.contrib.timeseries.TrainEvalFeatures.VALUES: y,
}

reader = NumpyReader(data)

#首先把x和y变成Python中的字典(变量data)。
#直接写成“data={‘times':x,‘values ’:y}”也是可以的。

用上面的代码得到reader后,该reader下会有一个read_full()方法,它的返回值是时间序列对应的tensor,可以用下面的代码进行实验:

with tf.Session() as sess:
    full_data = reader.read_full()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    print(sess.run(full_data))
    coord.request_stop()

猜你喜欢

转载自blog.csdn.net/DeepOscar/article/details/81124538