Tensorflow梯度下降应用
import tensorflow as tf
import numpy as np
#使用numpy生成随机点
x_data = np.random.rand(100)
y_data = x_data*0.1 + 0.2
#构造一个线性模型
b = tf.Variable(0.0)
k = tf.Variable(0.0)
y = k*x_data+b
#二次代价函数
loss = tf.reduce_mean(tf.square(y_data-y))#误差平方求平均值
#定义一个梯度下降来进行训练的优化器
optimizer = tf.train.GradientDescentOptimizer(0.2)
#最小化代价函数
train = optimizer.minimize(loss)
#初始化变量
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for index in range(201):
sess.run(train)
if index%10==0:
print(index,sess.run([k,b]))
###########输出
0 [0.058540713, 0.10185367]
10 [0.10913987, 0.19464658]
20 [0.10734161, 0.19575559]
30 [0.10587782, 0.19660187]
40 [0.10470589, 0.19727939]
50 [0.10376761, 0.19782184]
60 [0.10301641, 0.19825613]
70 [0.10241497, 0.19860384]
80 [0.10193346, 0.19888222]
90 [0.10154796, 0.19910508]
100 [0.10123933, 0.19928351]
110 [0.10099223, 0.19942637]
120 [0.10079438, 0.19954075]
130 [0.10063599, 0.19963232]
140 [0.10050918, 0.19970562]
150 [0.10040767, 0.19976433]
160 [0.10032637, 0.19981132]
170 [0.1002613, 0.19984894]
180 [0.1002092, 0.19987905]
190 [0.1001675, 0.19990316]
200 [0.10013408, 0.19992249]
Tensorflow梯度下降应用的更多相关文章
- Tensorflow 梯度下降实例
# coding: utf-8 # #### 假设我们要最小化函数 $y=x^2$, 选择初始点 $x_0=5$ # #### 1. 学习率为1的时候,x在5和-5之间震荡. # In[1]: imp ...
- tensorflow梯度下降
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt num_points = 1000 vectors ...
- TensorFlow实现梯度下降
# -*- coding: utf-8 -*- """ Created on Mon Oct 15 17:38:39 2018 @author: zhen "& ...
- Python之TensorFlow的变量收集、自定义命令参数、矩阵运算、梯度下降-4
一.TensorFlow为什么要存在变量收集的过程,主要目的就是把训练过程中的数据,比如loss.权重.偏置等数据通过图形展示的方式呈现在开发者的眼前. 自定义参数:自定义参数,主要是通过Python ...
- Tensorflow细节-P84-梯度下降与批量梯度下降
1.批量梯度下降 批量梯度下降法是最原始的形式,它是指在每一次迭代时使用所有样本来进行梯度的更新.从数学上理解如下: 对应的目标函数(代价函数)即为: (1)对目标函数求偏导: (2)每次迭代对参数进 ...
- 采用梯度下降优化器(Gradient Descent optimizer)结合禁忌搜索(Tabu Search)求解矩阵的全部特征值和特征向量
[前言] 对于矩阵(Matrix)的特征值(Eigens)求解,采用数值分析(Number Analysis)的方法有一些,我熟知的是针对实对称矩阵(Real Symmetric Matrix)的特征 ...
- 梯度下降与pytorch
记得在tensorflow的入门里,介绍梯度下降算法的有效性时使用的例子求一个二次曲线的最小值. 这里使用pytorch复现如下: 1.手动计算导数,按照梯度下降计算 import torch #使用 ...
- 深度学习必备:随机梯度下降(SGD)优化算法及可视化
补充在前:实际上在我使用LSTM为流量基线建模时候,发现有效的激活函数是elu.relu.linear.prelu.leaky_relu.softplus,对应的梯度算法是adam.mom.rmspr ...
- 使用多个梯度下降的方式进行测试,同时使用ops.apply_gradient进行梯度的下降
1. ops = tf.train.GradientDescentOptimizer(learning_rate) 构建优化器 参数说明:learning_rate 表示输入的学习率 2.ops.co ...
随机推荐
- c++primer 第四章编程练习答案
4.13.1 #include<iostream> struct students { ]; ]; char grade; int age; }; int main() { using n ...
- Hadoop单机模式和伪分布式搭建教程CentOS
1. 安装JAVA环境 2. Hadoop下载地址: http://archive.apache.org/dist/hadoop/core/ tar -zxvf hadoop-2.6.0.tar.gz ...
- hdoj-1106-排序(stringstream)
题目链接 /* Name: Copyright: Author: Date: 2018/5/2 20:56:53 Description: */ #include <iostream> # ...
- vector map迭代器失效解决方案
vector : iter = container.erase(iter); //erase的返回值是删除元素下一个元素的迭代器 vector<int>::iterator it = ...
- UVA - 11916 Emoogle Grid (组合计数+离散对数)
假如有这样一道题目:要给一个M行N列的网格涂上K种颜色,其中有B个格子不用涂色,其他每个格子涂一种颜色,同一列中的上下两个相邻格子不能涂相同颜色.给出M,N,K和B个格子的位置,求出涂色方案总数除以1 ...
- CodeForces - 961D:Pair Of Lines (几何,问两条直线是否可以覆盖所有点)
You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordin ...
- LeetCode Binary Tree Tilt
原题链接在这里:https://leetcode.com/problems/binary-tree-tilt/description/ 题目: Given a binary tree, return ...
- MyBatis嵌套查询column传多个参数描述
代码如下,红色部分为关键代码. 注意parameterType要为java.util.HashMap <resultMap id="baseResultMap" type=& ...
- CentOS6.5 下MySQL傻瓜式安装
为了为服务器上装mysql我先在虚拟机上练习了一下特此记录并分享; 注:参考文章https://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/ ...
- htmlunit 自动化提交/获取网页数据,自动化测试
开源组件: https://sourceforge.net/projects/htmlunit/ demo public void post() { try { WebClient client = ...