2017年10月13日 星期五

tensorflow regression I



這個例子是說...

我們先自己創造出一個資料群


y_true = (0.5* x_data)+5 + noise

斜率是 0.5...  常數是  5..  noise 是製造成一些分布...

接下來我們用tensorflow的方式來找出  M  和  B



m = tf.Variable(0.81)
b = tf.Variable(0.17)

定義m 和 b..  用 tf.Variable

xph = tf.placeholder(tf.float32,[batch_size])
yph = tf.placeholder(tf.float32,[batch_size])

定義輸入的資料.. 用 tf.placeholder

y_model = m*xph + b

定義model

error = tf.reduce_sum(tf.square(yph-y_model))

定義 error

optimizr = tf.train.GradientDescentOptimizer(learning_rate = 0.001)

定義參數化的方式

train = optimizr.minimize(error)

定義train

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)    ->  先初始化變數
    batches = 1000 
    for i in range(batches):
        rand_ind = np.random.randint(len(x_data),size=batch_size)
        # 這邊是說不要全部的資料都 feed進去...  用亂數的方式取 1000個資料出來
        feed = {xph:x_data[rand_ind],yph:y_true[rand_ind]}
        sess.run(train,feed_dict=feed)   ->  run  train
    model_m,model_b = sess.run([m,b])   ->    get model的值出來


最後值會在  model_m 和 model_b

model_m -> 0.48660713

model_b -> 4.8790665

y_hat = x_data * model_m + model_b
my_data.sample(n=250).plot(kind='scatter',x='X Data',y='Y')
plt.plot(x_data,y_hat,'r')

y_hat 就是那條紅色...   用眼睛看起來...這條線可以代表這群資料







程式碼如下:


# coding: utf-8

# In[56]:


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')


# In[57]:


import tensorflow as tf


# In[58]:


x_data = np.linspace(0.0,10.0,1000000)


# In[59]:


noise = np.random.randn(len(x_data))


# In[60]:


x_data


# In[61]:


noise.shape


# In[62]:


# y = mx +b
# b = 5
y_true = (0.5* x_data)+5 + noise


# In[63]:


x_df = pd.DataFrame(data=x_data,columns=['X Data'])


# In[64]:


y_df = pd.DataFrame(data=y_true,columns=['Y'])


# In[65]:


y_df.head()


# In[66]:


my_data = pd.concat([x_df,y_df],axis=1)


# In[67]:


my_data.head()


# In[68]:


my_data.sample(n=250).plot(kind='scatter',x='X Data',y='Y')


# In[69]:


batch_size = 8


# In[70]:


np.random.randn(2)


# In[71]:


m = tf.Variable(0.81)


# In[72]:


b = tf.Variable(0.17)


# In[73]:


xph = tf.placeholder(tf.float32,[batch_size])


# In[74]:


yph = tf.placeholder(tf.float32,[batch_size])


# In[75]:


y_model = m*xph + b


# In[76]:


error = tf.reduce_sum(tf.square(yph-y_model))


# In[77]:


optimizr = tf.train.GradientDescentOptimizer(learning_rate = 0.001)


# In[78]:


train = optimizr.minimize(error)


# In[79]:


init = tf.global_variables_initializer()


# In[84]:


with tf.Session() as sess:
    sess.run(init)
    batches = 1000
    for i in range(batches):
        rand_ind = np.random.randint(len(x_data),size=batch_size)
        feed = {xph:x_data[rand_ind],yph:y_true[rand_ind]}
        sess.run(train,feed_dict=feed)
    model_m,model_b = sess.run([m,b])


# In[85]:


model_m


# In[86]:


model_b


# In[88]:


y_hat = x_data *model_m + model_b


# In[92]:


my_data.sample(250).plot(kind='scatter',x='X Data',y='Y')
plt.plot(x_data,y_hat,'r')


# In[ ]:




沒有留言:

張貼留言