from tensorflow.contrib.timeseries.python.timeseries import NumpyReader

x = np.array(range(1000))
noise = np.random.uniform(-0.2, 0.2, 1000)
y = np.sin(np.pi * x / 100) + x / 200. + noise
plt.plot(x, y)
plt.savefig('timeseries_y.jpg')

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

reader = NumpyReader(data)
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()

train_input_fn = tf.contrib.timeseries.RandomWindowInputFn(reader,
                                                           batch_size=2,
                                                           window_size=10)

with tf.Session() as sess:
    batch_data = train_input_fn.create_batch()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    one_batch = sess.run(batch_data[0])
    coord.request_stop()
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.contrib.timeseries.python.timeseries import NumpyReader,CSVReader
#接着,利用np.sin生成一个实验用的时间序列数据,改时间序列数据实际上是在正弦曲线上加入了上升的趋势和一些随机的噪声
x=np.array(range(1000))
noise=np.random.uniform(-0.2,0.2,1000)
y=np.sin(np.pi*x/100)+x/200+noise
plt.plot(x,y)
plt.savefig('timeseries_y.jpg')
data={#以numpy的形式读入
    tf.contrib.timeseries.TrainEvalFeatures.TIMES:x,
    tf.contrib.timeseries.TrainEvalFeatures.VALUES:y
    }
reader=NumpyReader(data)
with tf.Session() as sess:
    full_data=reader.read_full()#返回时间序列对应的tensor
    #调用read_full()方法会生成读取队列
    #调用tf.trian.start_queue_runners启动队列才能正常读取
    coord=tf.train.Coordinator()
    threads=tf.train.start_queue_runners(sess=sess, coord=coord)
    print(sess.run(full_data))
    coord.request_stop()
#建立batch数据集
train_input_fn=tf.contrib.timeseries.RandomWindowInputFn(reader,batch_size=2,window_size=10)
with tf.Session() as sess:
    batch_data=train_input_fn.create_batch()
    coord=tf.train.Coordinator()
    threads=tf.train.start_queue_runners(sess=sess, coord=coord)
    one_batch=sess.run(batch_data[0])
    coord.request_stop()
print(one_batch)
x = np.array(range(1000))
noise = np.random.uniform(-0.2, 0.2, 1000)
y = np.sin(np.pi * x / 100) + x / 200. + noise
plt.plot(x, y)
plt.savefig('timeseries_y.jpg')

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

reader = NumpyReader(data)

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()

train_input_fn = tf.contrib.timeseries.RandomWindowInputFn(
    reader, batch_size=2, window_size=10)

with tf.Session() as sess:
    batch_data = train_input_fn.create_batch()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    one_batch = sess.run(batch_data[0])
    coord.request_stop()
Exemple #4
0
y = np.sin(np.pi * x / 100) + x / 200. + noise
plt.plot(x, y)
plt.savefig('timeserise_y.jpg')

#读取数据
#step1
data = {
    tf.contrib.timeseries.TrainEvalFeatures.TIMES: x,
    tf.contrib.timeseries.TrainEvalFeatrues.VALUES: y,
}
#上述也可以直接写为:data={times:x,values:y}
#step2
reader = NumpyReader(data)
#step3
with tf.Session() as sess:
    full_data = reader.read_full()  #step4
    coord = tf.train.Coordinator()  #step5:创建“管理线程”的类实例
    threads = tf.train.start_queue_runners(sess=sess,
                                           coord=coord)  #step6:启动队列才能正常进行读取
    print(sess.run(
        full_data))  #不能直接print(sess.run(reader.read_full())),需要启动线程后才可以
    coord.request_stop()  #任务执行结束后,停止线程

#reader即为train_data_set,接下来创建train_batch
#step1
train_input_fn = tf.contrib.timeseries.RandomWindowInputFn(
    reader, batch_size=2,
    window_size=10)  #batch大小为2条timeseries,一个timeseries共有window_size个时间对
#step2
with tf.Session() as sess:
    batch_data = train_input_fn.creat_batch()  #对trainset创建batch
Exemple #5
0
y = np.sin(np.pi * x / 100) + x / 200. + noise
plt.plot(x, y)
plt.savefig('1_timeseries_y.jpg')

# 2. 将 x 和 y 保存到data字典里
data = {
    tf.contrib.timeseries.TrainEvalFeatures.TIMES: x,
    tf.contrib.timeseries.TrainEvalFeatures.VALUES: y,
}

# 3. 利用 NumpyReader 将其读取为一个 reader
reader = NumpyReader(data)

# 4. 真正读取 reader 的值
with tf.Session() as sess:
    full_data = reader.read_full(
    )  # 用read_full 读取 reader 得到一个时间序列对应的 tensor, 需要 start_queue_runners 启动队列才可以用 run()获取值
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    print(sess.run(full_data))
    coord.request_stop()

# 5. 建立读取batch数据的对象
train_input_fn = tf.contrib.timeseries.RandomWindowInputFn(
    reader, batch_size=2, window_size=10)  # 一个batch里有2个序列,每个序列长度为10

# 6. 真正读取、create batch数据
with tf.Session() as sess:
    batch_data = train_input_fn.create_batch()
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    one_batch = sess.run(batch_data[0])