1.生成模拟数据
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
#from sklearn.utils import shuffle
def GenerateData(batchsize=100):
train_X=np.linspace(-1,1,batchsize)
train_Y=2*train_X+np.random.randn(*train_X.shape)*0.3
#yield shuffle(train_X,train_Y)#数据打乱处理
yield train_X,train_Y
2.定义占位符:
Xinput=tf.placeholder('float',(None))
Yinput=tf.placeholder('float',(None))
3.建立会话 获取数据
rain_epochs=20
with tf.Session() as sess:
for epoch in range(train_epochs):
for x,y in GenerateData():
xv,yv=sess.run([Xinput,Yinput],feed_dict={Xinput:x,Yinput:y})
print(epoch,' x.shape:',np.shape(xv),' x[:3]:',xv[:3])
0 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
1 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
2 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
3 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
4 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
5 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
6 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
7 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
8 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
9 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
10 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
11 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
12 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
13 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
14 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
15 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
16 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
17 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
18 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
19 x.shape: (100,) x[:3]: [-1. -0.97979796 -0.959596 ]
4.数据可视化
train_data=list(GenerateData())[0]
plt.plot(train_data[0],train_data[1],'ro',label='Original data')
plt.legend()
plt.show()