吴恩达机器学习作业TensorFlow实现(ex1,线性回归)

# -*- coding: utf-8 -*-

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

plotdata = { "batchsize":[], "loss":[] }

def moving_average(a, w=10):
    if len(a) < w: 
        return a[:]    
    return [val if idx < w else sum(a[(idx-w):idx])/w for idx, val in enumerate(a)]
# =============================================================================
# 数据提取(需要将txt中,全部替换为空格)
# =============================================================================
data = np.loadtxt("G:\DeepLearning\吴恩达作业\ex1data1.txt")
train_X = data[:,0]
train_Y = data[:,1]

#显示模拟数据点
plt.rcParams['font.sans-serif']=['SimHei'] 
plt.plot(train_X, train_Y, 'ro', label='原始数据')
plt.legend()

plt.rcParams['figure.dpi'] = 100 #分辨率
plt.rcParams['savefig.dpi'] = 100 #图片像素

#plt.savefig('test.png', dpi=100)
plt.show()


# 创建模型
# 占位符
X = tf.placeholder("float")
Y = tf.placeholder("float")

## 模型参数
W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.zeros([1]), name="bias")

## 前向结构
z = tf.multiply(X, W)+ b
#
##反向优化
cost =tf.reduce_mean(tf.square(Y - z))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) #Gradient descent

## 初始化变量
init = tf.global_variables_initializer()

## 启动session
with tf.Session() as sess:
    sess.run(init)
    for step in range(20):
        sess.run(optimizer, feed_dict={X: train_X, Y:train_Y})  
        loss = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
        print("Step=%d,loss =%f,[W=%f,b=%f]" % (step+1,loss, 
              sess.run(W),sess.run(b)))
        
        plotdata["batchsize"].append(step)
        plotdata["loss"].append(loss)


    #图形显示
    plt.plot(train_X, train_Y, 'ro', label='Original data')
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    plt.legend()
    plt.show()

    plotdata["avgloss"] = moving_average(plotdata["loss"])
    plt.figure(1)
    plt.subplot(211)
    plt.plot(plotdata["batchsize"], plotdata["avgloss"], 'b--')
    plt.xlabel('Minibatch number')
    plt.ylabel('Loss')
    plt.title('Minibatch run vs. Training loss')
     
    plt.show()
    

 

你可能感兴趣的:(机器学习)