线性回归-tensorflow(1)

Code

import numpy as np
import matplotlib.pyplot as plt
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
rng = np.random
#超参数
learning_rate = 0.01
training_epochs = 1000
display_step = 50
#训练数据
train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                         2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]
#tf图输入
X = tf.placeholder("float")
Y = tf.placeholder("float")
#初始化网络模型的权重
W = tf.Variable(rng.randn(), name = "weight")
b = tf.Variable(rng.randn(), name="bias")
#构造线性模型
pred = tf.add(tf.multiply(X, W), b)
#损失函数:均方差
cost = tf.reduce_sum(tf.pow(pred - Y, 2))/n_samples
#优化方式:梯度下降
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
#初始化所有图结点参数
init = tf.global_variables_initializer()
#开始训练
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        #Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c),"W=", sess.run(W), "b=", sess.run(b))
    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

    #Graphic display
    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()

Output

Epoch: 0050 cost= 0.178384900 W= 0.30815783 b= 0.3629712
Epoch: 0100 cost= 0.173098624 W= 0.30111572 b= 0.41429833
Epoch: 0150 cost= 0.168972984 W= 0.29489344 b= 0.4596497
Epoch: 0200 cost= 0.165753171 W= 0.28939557 b= 0.49972105
Epoch: 0250 cost= 0.163240537 W= 0.28453782 b= 0.535127
Epoch: 0300 cost= 0.161279723 W= 0.28024554 b= 0.56641155
Epoch: 0350 cost= 0.159749702 W= 0.27645317 b= 0.5940529
Epoch: 0400 cost= 0.158555999 W= 0.27310213 b= 0.6184765
Epoch: 0450 cost= 0.157624558 W= 0.27014133 b= 0.6400569
Epoch: 0500 cost= 0.156898007 W= 0.26752514 b= 0.65912473
Epoch: 0550 cost= 0.156331196 W= 0.26521355 b= 0.6759736
Epoch: 0600 cost= 0.155889139 W= 0.26317105 b= 0.69086033
Epoch: 0650 cost= 0.155544400 W= 0.26136637 b= 0.7040138
Epoch: 0700 cost= 0.155275598 W= 0.25977173 b= 0.7156369
Epoch: 0750 cost= 0.155066073 W= 0.25836283 b= 0.72590524
Epoch: 0800 cost= 0.154902726 W= 0.25711787 b= 0.73497933
Epoch: 0850 cost= 0.154775411 W= 0.2560179 b= 0.7429967
Epoch: 0900 cost= 0.154676273 W= 0.2550461 b= 0.7500795
Epoch: 0950 cost= 0.154599041 W= 0.2541873 b= 0.7563389
Epoch: 1000 cost= 0.154538885 W= 0.2534287 b= 0.76186824
Optimization Finished!
Training cost= 0.15453888 W= 0.2534287 b= 0.76186824
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值