# -*- coding: utf-8 -*-
"""
Created on Mon Oct 15 17:38:39 2018 @author: zhen
""" import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler n_epochs = 10000
learning_rate = 0.01 housing = fetch_california_housing(data_home="C:/Users/zhen/.spyder-py3/data", download_if_missing=True)
m, n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]
# 归一化
scaler= StandardScaler().fit(housing_data_plus_bias)
scaled_housing_data_plus_bias = scaler.transform(housing_data_plus_bias)
# 创建常量
x = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name='x')
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name='y')
# 创建随机数
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0), name='theta')
# 矩阵乘
y_pred = tf.matmul(x, theta, name="predictions") error = y_pred - y
# 求平均值
mse = tf.reduce_mean(tf.square(error), name="mse")
"""
# 求梯度
gradients = tf.gradients(mse, [theta])[0]
# 赋值
training_op = tf.assign(theta, theta - learning_rate * gradients)
"""
# 梯度下降
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(mse) init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init) for epoch in range(n_epochs):
if epoch % 100 == 0:
print("Epoch", epoch, "MSE = ", mse.eval())
sess.run(training_op) best_theta = theta.eval()
print(best_theta)

结果:

Epoch 0 MSE =  9.128207
Epoch 100 MSE = 4.893214
Epoch 200 MSE = 4.8329406
Epoch 300 MSE = 4.824335
Epoch 400 MSE = 4.8187895
Epoch 500 MSE = 4.814753
Epoch 600 MSE = 4.811796
Epoch 700 MSE = 4.8096204
Epoch 800 MSE = 4.808017
Epoch 900 MSE = 4.806835
Epoch 1000 MSE = 4.805955
Epoch 1100 MSE = 4.805301
Epoch 1200 MSE = 4.8048124
Epoch 1300 MSE = 4.804449
Epoch 1400 MSE = 4.804172
Epoch 1500 MSE = 4.803962
Epoch 1600 MSE = 4.8038034
Epoch 1700 MSE = 4.803686
Epoch 1800 MSE = 4.8035927
Epoch 1900 MSE = 4.80352
Epoch 2000 MSE = 4.8034678
Epoch 2100 MSE = 4.803425
Epoch 2200 MSE = 4.8033857
Epoch 2300 MSE = 4.803362
Epoch 2400 MSE = 4.803341
Epoch 2500 MSE = 4.8033247
Epoch 2600 MSE = 4.80331
Epoch 2700 MSE = 4.8033013
Epoch 2800 MSE = 4.8032923
Epoch 2900 MSE = 4.8032856
Epoch 3000 MSE = 4.8032804
Epoch 3100 MSE = 4.803273
Epoch 3200 MSE = 4.803271
Epoch 3300 MSE = 4.8032694
Epoch 3400 MSE = 4.803267
Epoch 3500 MSE = 4.8032637
Epoch 3600 MSE = 4.8032603
Epoch 3700 MSE = 4.803259
Epoch 3800 MSE = 4.803259
Epoch 3900 MSE = 4.8032584
Epoch 4000 MSE = 4.8032575
Epoch 4100 MSE = 4.8032575
Epoch 4200 MSE = 4.803256
Epoch 4300 MSE = 4.803255
Epoch 4400 MSE = 4.803256
Epoch 4500 MSE = 4.803256
Epoch 4600 MSE = 4.803253
Epoch 4700 MSE = 4.8032565
Epoch 4800 MSE = 4.803258
Epoch 4900 MSE = 4.8032556
Epoch 5000 MSE = 4.803256
Epoch 5100 MSE = 4.8032537
Epoch 5200 MSE = 4.8032565
Epoch 5300 MSE = 4.803255
Epoch 5400 MSE = 4.8032546
Epoch 5500 MSE = 4.803254
Epoch 5600 MSE = 4.8032537
Epoch 5700 MSE = 4.8032517
Epoch 5800 MSE = 4.8032527
Epoch 5900 MSE = 4.8032537
Epoch 6000 MSE = 4.803254
Epoch 6100 MSE = 4.8032546
Epoch 6200 MSE = 4.803255
Epoch 6300 MSE = 4.8032546
Epoch 6400 MSE = 4.803253
Epoch 6500 MSE = 4.803253
Epoch 6600 MSE = 4.803253
Epoch 6700 MSE = 4.8032517
Epoch 6800 MSE = 4.803252
Epoch 6900 MSE = 4.8032517
Epoch 7000 MSE = 4.803252
Epoch 7100 MSE = 4.8032537
Epoch 7200 MSE = 4.8032537
Epoch 7300 MSE = 4.803253
Epoch 7400 MSE = 4.803253
Epoch 7500 MSE = 4.803253
Epoch 7600 MSE = 4.803254
Epoch 7700 MSE = 4.8032546
Epoch 7800 MSE = 4.8032556
Epoch 7900 MSE = 4.803256
Epoch 8000 MSE = 4.8032565
Epoch 8100 MSE = 4.8032565
Epoch 8200 MSE = 4.8032565
Epoch 8300 MSE = 4.8032556
Epoch 8400 MSE = 4.8032565
Epoch 8500 MSE = 4.8032575
Epoch 8600 MSE = 4.8032565
Epoch 8700 MSE = 4.803256
Epoch 8800 MSE = 4.803256
Epoch 8900 MSE = 4.8032556
Epoch 9000 MSE = 4.803255
Epoch 9100 MSE = 4.8032546
Epoch 9200 MSE = 4.803254
Epoch 9300 MSE = 4.8032546
Epoch 9400 MSE = 4.8032546
Epoch 9500 MSE = 4.803255
Epoch 9600 MSE = 4.803255
Epoch 9700 MSE = 4.803255
Epoch 9800 MSE = 4.803255
Epoch 9900 MSE = 4.803255
[[ 0.43350863]
[ 0.8296331 ]
[ 0.11875448]
[-0.26555073]
[ 0.3057157 ]
[-0.00450223]
[-0.03932685]
[-0.8998542 ]
[-0.87051094]]

结果样例:

TensorFlow实现梯度下降的更多相关文章

  1. Tensorflow梯度下降应用

    import tensorflow as tfimport numpy as np #使用numpy生成随机点x_data = np.random.rand(100)y_data = x_data*0 ...

  2. Python之TensorFlow的变量收集、自定义命令参数、矩阵运算、梯度下降-4

    一.TensorFlow为什么要存在变量收集的过程,主要目的就是把训练过程中的数据,比如loss.权重.偏置等数据通过图形展示的方式呈现在开发者的眼前. 自定义参数:自定义参数,主要是通过Python ...

  3. Tensorflow细节-P84-梯度下降与批量梯度下降

    1.批量梯度下降 批量梯度下降法是最原始的形式,它是指在每一次迭代时使用所有样本来进行梯度的更新.从数学上理解如下: 对应的目标函数(代价函数)即为: (1)对目标函数求偏导: (2)每次迭代对参数进 ...

  4. 采用梯度下降优化器(Gradient Descent optimizer)结合禁忌搜索(Tabu Search)求解矩阵的全部特征值和特征向量

    [前言] 对于矩阵(Matrix)的特征值(Eigens)求解,采用数值分析(Number Analysis)的方法有一些,我熟知的是针对实对称矩阵(Real Symmetric Matrix)的特征 ...

  5. 『TensorFlow』梯度优化相关

    tf.trainable_variables可以得到整个模型中所有trainable=True的Variable,也是自由处理梯度的基础 基础梯度操作方法: tf.gradients 用来计算导数.该 ...

  6. 梯度下降与pytorch

    记得在tensorflow的入门里,介绍梯度下降算法的有效性时使用的例子求一个二次曲线的最小值. 这里使用pytorch复现如下: 1.手动计算导数,按照梯度下降计算 import torch #使用 ...

  7. TensorFlow的梯度裁剪

    在较深的网络,如多层CNN或者非常长的RNN,由于求导的链式法则,有可能会出现梯度消失(Gradient Vanishing)或梯度爆炸(Gradient Exploding )的问题. 原理 问题: ...

  8. 深度学习必备:随机梯度下降(SGD)优化算法及可视化

    补充在前:实际上在我使用LSTM为流量基线建模时候,发现有效的激活函数是elu.relu.linear.prelu.leaky_relu.softplus,对应的梯度算法是adam.mom.rmspr ...

  9. 使用多个梯度下降的方式进行测试,同时使用ops.apply_gradient进行梯度的下降

    1. ops = tf.train.GradientDescentOptimizer(learning_rate) 构建优化器 参数说明:learning_rate 表示输入的学习率 2.ops.co ...

随机推荐

  1. 【LeetCode】9. 回文数

    题目 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1:输入: 121 输出: true 示例 2:输入: -121 输出: false 解释: 从左 ...

  2. Datatable数据转换成excel导出时 数值类型在EXCEL中为文本形式 无法进行统计

    功能背景 有地税上以及各企业的一个缴费情况的比对,基于两表进行匹配查看数据是否在合理范围内,对比对完成表进行数值导出. 2.问题描述 匹配和生成匹配结果导出已成功完成,但是在数值列导出后变成了文本形式 ...

  3. POJ 2895

    #include <iostream> #include <string> #define MAXN 27 using namespace std; short map[MAX ...

  4. UFLDL 教程学习笔记(三)自编码与稀疏性

    UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...

  5. c++11并行、并发与多线程编程

    首先,我们先理解并发和并行的区别. 你吃饭吃到一半,电话来了,你一直到吃完了以后才去接,这就说明你不支持并发也不支持并行. 你吃饭吃到一半,电话来了,你停了下来接了电话,接完后继续吃饭,这说明你支持并 ...

  6. PHP-CPP开发扩展(二)

    PHP-CPP是一个用于开发PHP扩展的C++库.本节讲解PHP输出和函数的实现. 输出和错误 上面的helloworld示例里,我们使用Php::out进行输出,并使用了std::endl换行刷新缓 ...

  7. HDFS-Architecture剖析

    1.概述 从HDFS的应用层面来看,我们可以非常容易的使用其API来操作HDFS,实现目录的创建.删除,文件的上传下载.删除.追加(Hadoop2.x版本以后开始支持)等功能.然而仅仅局限与代码层面是 ...

  8. Java并发编程笔记之PriorityBlockingQueue源码分析

    JDK 中无界优先级队列PriorityBlockingQueue 内部使用堆算法保证每次出队都是优先级最高的元素,元素入队时候是如何建堆的,元素出队后如何调整堆的平衡的? PriorityBlock ...

  9. Go 语言实践(一)

    本文由Austin发表 指导原则 我们要谈论在一个编程语言中的最佳实践,那么我们首先应该明确什么是"最佳".如果您们听了我昨天那场讲演的话,您一定看到了来自 Go 团队的 Russ ...

  10. Java学习笔记之——循环语句

    一.for循环 语法: for(变量初始化:条件判断:更新循环变量){ 循环体: } 案例: 二.while循环 语法: while(条件){ 循环体: } 如果条件为true,执行循环体,false ...