人工智能AI培训_TensorFlow 实现线性回归

  人工智能AI培训_TensorFlow 实现线性回归


3405684-749486673404015c.png
TensorFlow

  1.实验介绍

  1.1.关于本实验

  本实验为TensorFlow线性回归的一个实际案例即房价预测。

  1.2.实验目的

  理解线性回归。

  理解如何利用TensorFlow做预测。

  1.3.实验介绍

  本实验通过一个预测房屋价格的实例来讲解利用线性回归预测房屋价格,以及在tensorflow中如何实现。平时常用的房价预测数据集为波士顿房价数据集,本实验采用的是北京的房价数据集,更贴近国人的生活。

  1.4.实验步骤

  步骤 1步骤1登陆华为云。

  步骤 2点击右上方的控制台。

  步骤 3选择弹性云服务器,网页中会显示该弹性云的可进行的操作,选择远程登录。即登录到弹性云服务器。

  步骤 4输入指令ll,查看当前目录下的文件。

  步骤 5输入命令vi house_price.py,创建新的Python脚本。

  步骤 6输入命令i,进入编辑模式开始编辑,输入脚本内容。

  步骤 7输入命令 :wq!,保存并退出。

  步骤 8输入命令cat house_price.py查看代码。

  步骤 9运行测试。输入命令:python3 house_price.py。

  2.实验过程

  2.1.设置编码说明

  # coding:utf-8

  2.2.导入模块

  #载入此项目所需的库

  from __future__ import print_function, division

  import tensorflow as tf

  import pandas as pd

  import numpy as np

  import matplotlib.pyplot as plt

  import seaborn

  2.3.导入数据

  该实验数据来源为:https://github.com/cunxi1992/boston_housing中的bj_housing2.csv文件。

  读取数据:

  train = pd.read_csv("bj_housing2.csv")

  2.4.定义参数

  train = train[train['Area'] < 12000]

  train_X = train['Area'].values.reshape(-1, 1)

  train_Y = train['Value'].values.reshape(-1, 1)

  n_samples = train_X.shape[0]

  # 定义参数,设置学习率

  learning_rate = 2

  # 设置训练次数

  training_epochs = 1000

  # 设置多少次显示一次

  display_step = 50

  2.5.定义占位符

  # 定义X,Y占位符

  X = tf.placeholder(tf.float32)

  Y = tf.placeholder(tf.float32)

  # 使用Variable定义的学习参数

  W = tf.Variable(np.random.randn(), name="weight", dtype=tf.float32)

  b = tf.Variable(np.random.randn(), name="bias", dtype=tf.float32)

  2.6.构建正向传播结构

  # 构建正向传播结构

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

  #损失函数

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

  # 使用梯度下降优化器

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

  2.7.初始化

  # 激活Init

  init = tf.global_variables_initializer()

  # 启动session,初始化变量

  with tf.Session() as sess:

  sess.run(init)

  2.8.启动循环

  #启动循环开始训练

  for epoch in range(training_epochs):

  for (x, y) in zip(train_X, train_Y):

  sess.run(optimizer, feed_dict={X: x, Y: y})

  #显示训练中的详细信息

  if (epoch + 1) % display_step == 0:

  c = sess.run(cost, feed_dict={X: train_X, Y: train_Y})

  print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.3f}".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')

  2.9.展示训练结果

  #展示训练结果

  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()

  2.10.实验结果

  输出结果:

  Epoch: 0050 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0100 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0150 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0200 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0250 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0300 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0350 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0400 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0450 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0500 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0550 cost= 16308.796 W= 1.8889627 b= 155.08276

  Epoch: 0600 cost= 16308.796 W= 1.8889627 b= 155.08276

  ……

  3.实例描述

  本实验利用网上已有的北京房价数据集预测了北京的房价,实现了TensorFlow的线性回归应用。

转载于:https://www.jianshu.com/p/b4e3a8d0c33e

猜你喜欢

转载自blog.csdn.net/weixin_34162401/article/details/91206698