线性回归拟合

该博客通过Python和TensorFlow展示了如何实现线性回归。作者创建了一个简单的线性模型y=2x,并添加了随机噪声。使用梯度下降法进行优化,训练了20个周期,展示了损失值的变化,并得到了权重和偏置的最终值。
摘要由CSDN通过智能技术生成

#!/usr/bin/env python2

-- coding: utf-8 --

“”"
Created on Thu Dec 20 10:20:55 2018

@author: prestonhuang
“”"

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

train_X = np.linspace(-1,1,100)
train_Y = 2*train_X + np.random.randn(train_X.shape) 0.3 # y=2x

plt.plot(train_X,train_Y,‘ro’,label = ‘Original data’)
plt.legend()
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)

int = tf.gl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值