线性拟合?叶子的长宽:

# Linear Regression: TensorFlow Way
#----------------------------------
#
# This function shows how to use TensorFlow to
# solve linear regression.
# y = Ax + b
#
# We will use the iris data, specifically:
#  y = Sepal Length
#  x = Petal Widthimport matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn import datasets
from tensorflow.python.framework import ops
ops.reset_default_graph()# Create graph
sess = tf.Session()# Load the data
# iris.data = [(Sepal Length, Sepal Width, Petal Length, Petal Width)]
iris = datasets.load_iris()
x_vals = np.array([x[3] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])# Declare batch size
batch_size = 25# Initialize placeholders
x_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)# Create variables for linear regression
A = tf.Variable(tf.random_normal(shape=[1,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))# Declare model operations
model_output = tf.add(tf.matmul(x_data, A), b)# Declare loss function (L2 loss)
loss = tf.reduce_mean(tf.square(y_target - model_output))# Declare optimizer
my_opt = tf.train.GradientDescentOptimizer(0.05)
train_step = my_opt.minimize(loss)# Initialize variables
init = tf.global_variables_initializer()
sess.run(init)# Training loop
loss_vec = []
for i in range(100):rand_index = np.random.choice(len(x_vals), size=batch_size)rand_x = np.transpose([x_vals[rand_index]])rand_y = np.transpose([y_vals[rand_index]])sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})loss_vec.append(temp_loss)if (i+1)%25==0:print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)) + ' b = ' + str(sess.run(b)))print('Loss = ' + str(temp_loss))# Get the optimal coefficients
[slope] = sess.run(A)
[y_intercept] = sess.run(b)# Get best fit line
best_fit = []
for i in x_vals:best_fit.append(slope*i+y_intercept)# Plot the result
plt.plot(x_vals, y_vals, 'o', label='Data Points')
plt.plot(x_vals, best_fit, 'r-', label='Best fit line', linewidth=3)
plt.legend(loc='upper left')
plt.title('Sepal Length vs Pedal Width')
plt.xlabel('Pedal Width')
plt.ylabel('Sepal Length')
plt.show()# Plot loss over time
plt.plot(loss_vec, 'k-')
plt.title('L2 Loss per Generation')
plt.xlabel('Generation')
plt.ylabel('L2 Loss')
plt.show()

转载于:https://www.cnblogs.com/bonelee/p/8996380.html

tensorflow 线性回归 iris相关推荐

  1. tensorflow 线性回归

    https://blog.csdn.net/zhangpengzp/article/details/81384154 学习tensorflow,希望与大家共同进步,下面讲述的是如何利用tensorfl ...

  2. tensorflow线性回归--拟合iris花瓣数据

    思路:用线性回归拟合鸢尾花花瓣长度和宽度之间的关系:y = Ax + b,其中 y 时花瓣长度,x是花瓣宽度. 建议有一点 tensorflow 基础再往下看. 下面是代码具体讲解. 先放结果吧 代码 ...

  3. 机器学习算法 09-02 TensorFlow核心概念 TensorFlow基础代码、TensorFlow线性回归解析解和BGD求法

    目录 1 核心概念 2 代码流程 3 基础代码: 3.1 tf的版本 定义常量 理解tensor 了解session 3. 2   指定设备.  Variable 初始化 .  with块创建sess ...

  4. [tensorflow] 线性回归模型实现

    在这一篇博客中大概讲一下用tensorflow如何实现一个简单的线性回归模型,其中就可能涉及到一些tensorflow的基本概念和操作,然后因为我只是入门了点tensorflow,所以我只能对部分代码 ...

  5. tensorflow实现iris分类

    内容来自MOOC<人工智能实践:Tensorflow笔记2> 八股搭建网络:我觉得是指按照固定模式搭建神经网络,八股只是呆板的意思,并不是按照八个步骤搭建.我们抨击的应该是八股取士制度,而 ...

  6. python 网页樱花动态图_python,tensorflow线性回归Django网页显示Gif动态图

    1.工程组成 2.urls.py """Django_machine_learning_linear_regression URL Configuration The ` ...

  7. python网页动图_python,tensorflow线性回归Django网页显示Gif动态图

    1.工程组成 2.urls.py """Django_machine_learning_linear_regression URL Configuration The ` ...

  8. tensorflow线性回归基础函数

    以下先使用tensorflow 的矩阵乘积,注意不是内积,然后使用基础函数求平方 .平方和.均值,这是使用tensorflow 使用线性回归分析的基础,基础好了,才能走出下一步,要不然怎么数据分析呢, ...

  9. tensorflow Elastic Net回归,拟合 iris 数据

    引言: 之前写过一篇 tensorflow线性回归–拟合iris花瓣数据.今天的Elastic Net Regression和它差不多,只不过是损失函数变了一下.我对Elastic Net 回归的理解 ...

最新文章

  1. 为JS和C#类加一个扩展方法吧:P
  2. retina 负样本回归增强loss
  3. WebClient DownloadFile 用法
  4. mysql可以打开dbt么_dbt 基本试用
  5. JZOJ 5473. 【NOIP2017提高组正式赛】小凯的疑惑
  6. 上海市新能源汽车数据平台引入阿里云Lindorm数据库,成本下降20%
  7. java开发一个完整的秒杀与竞价网上商城
  8. nrm : 无法加载文件 C:\Users\hc\AppData\Roaming\npm\nrm.ps1 ,因为在此系统上禁止运行脚本。
  9. 留言系统 php 防攻击,php 留言板防刷新
  10. 拼多多11.11:无定金、不预售,“好牌子、好实惠”底价直降
  11. centos7 开机启动文件路径_centos7定时运行python脚本
  12. 【cl】本地安装maven的jar包报错Artifact is already in the local repository
  13. atitit.项目设计模式---ioc attilax总结v4 q11
  14. python线程,进程,队列和缓存
  15. java fastjson解析json_fastjson解析json数据 Java类
  16. 新版UI千月影视盒子双端源码
  17. 冯康 计算机组装与维护,计算机组装与维护_毕业论文.doc
  18. win10系统cpu内核或逻辑核心缺少缺少,解决办法
  19. 羊车门问题。有三扇关闭的门,一扇门背后面停着汽车,其余门后面是山羊,只有主持人知道每扇门后面是什么。参赛者可以选择一扇门,在开启它之前,主持人会开启另一扇门,露出门后的山羊,然后允许参赛者更换自己的选
  20. 《Adobe After Effects CS6中文版经典教程》——2.9 渲染合成图像

热门文章

  1. Linux USB 驱动开发(一)—— USB设备基础概念
  2. 量子计算机模拟其他系统,一种量子计算机的模拟控制方法、系统及相关组件技术方案...
  3. 在java中开发图形用户_2016年计算机二级考试《JAVA》习题:编写图形用户界面
  4. 翟萍python程序设计_Python程序设计(高等学校计算机教育规划教材)
  5. lin通讯从节点同步间隔场_低成本总线技术——LIN总线协议规范介绍
  6. linux进不了容器配置目录,linux – 在Docker容器中使用bcrypt的ELF头文件或安装问题...
  7. mysql带where条件导出数据表以及部分错误解析
  8. mysql存储数据,varchar类型中的数据变成了科学计数法?
  9. php基于laravel框架的批量插入操作
  10. 软件测试——等价类划分