Machine Learning Series-tensorflow-03-linear regression Linear Regression

Source: Internet
Author: User
Use tensorflow to implement linear regression of data

Import related libraries

import tensorflow as tfimport numpyimport matplotlib.pyplot as pltrng = numpy.random

Parameter settings

learning_rate = 0.01training_epochs = 1000display_step = 50

Training data

train_X = numpy.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 = numpy.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 Image Input

X = tf.placeholder("float")Y = tf.placeholder("float")

Set weight and offset

W = tf.Variable(rng.randn(), name="weight")b = tf.Variable(rng.randn(), name="bias")

Build a Linear Model

pred = tf.add(tf.multiply(X, W), b)

Mean Squared Error

cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)

Gradient Descent

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

Initialize variable

init = tf.global_variables_initializer()

Start Training

With TF. session () as sess: sess. run (init) # suitable for 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 the logs of each 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 finis Hed! ") 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') # drawing displays 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 ()
Result Display

EPOCH: 0050 cost = 0.183995649 W = 0.43250677 B =-0.5143978
EPOCH: 0100 cost = 0.171630666 W = 0.42162812 B =-0.43613702
EPOCH: 0150 cost = 0.160693780 W = 0.41139638 B =-0.36253116
EPOCH: 0200 cost = 0.151019916 W = 0.40177315 B =-0.2933027
EPOCH: 0250 cost = 0.142463341 W = 0.39272234 B =-0.22819161
EPOCH: 0300 cost = 0.134895071 W = 0.3842099 B =-0.16695316
EPOCH: 0350 cost = 0.128200993 W = 0.37620357 B =-0.10935676
EPOCH: 0400 cost = 0.122280121 W = 0.36867347 B =-0.055185713
EPOCH: 0450 cost = 0.117043234 W = 0.36159125 B =-0.004236537
EPOCH: 0500 cost = 0.112411365 W = 0.3549302 B = 0.04368245
EPOCH: 0550 cost = 0.108314596 W = 0.34866524 B = 0.08875148
EPOCH: 0600 cost = 0.104691163 W = 0.34277305 B = 0.13114017
EPOCH: 0650 cost = 0.101486407 W = 0.33723122 B = 0.17100765
EPOCH: 0700 cost = 0.098651998 W = 0.33201888 B = 0.20850417
EPOCH: 0750 cost = 0.096145160 W = 0.32711673 B = 0.24377018
EPOCH: 0800 cost = 0.093927994 W = 0.32250607 B = 0.27693948
EPOCH: 0850 cost = 0.091967128 W = 0.31816947 B = 0.308136
EPOCH: 0900 cost = 0.090232961 W = 0.31409115 B = 0.33747625
EPOCH: 0950 cost = 0.088699281 W = 0.31025505 B = 0.36507198
EPOCH: 1000 cost = 0.087342896 W = 0.30664718 B = 0.39102668
Optimization finished!
Training cost = 0.087342896 W = 0.30664718 B = 0.39102668

Refer:
Author: Aymeric damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/

Machine Learning Series-tensorflow-03-linear regression Linear Regression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.