TensorFlow梯度求解tf.gradients实例

我就废话不多说了,直接上代码吧!

import tensorflow as tf 

w1 = tf.Variable([[1,2]])
w2 = tf.Variable([[3,4]]) 

res = tf.matmul(w1, [[2],[1]]) 

grads = tf.gradients(res,[w1]) 

with tf.Session() as sess:
 tf.global_variables_initializer().run()
 print sess.run(res)
 print sess.run(grads)

输出结果为:

[[4]]
[array([[2, 1]], dtype=int32)]

可以这样看res与w1有关,w1的参数设为[a1,a2],则:

2*a1 + a2 = res

所以res对a1,a2求导可得 [[2,1]]为w1对应的梯度信息。

import tensorflow as tf
def gradient_clip(gradients, max_gradient_norm):
 """Clipping gradients of a model."""
 clipped_gradients, gradient_norm = tf.clip_by_global_norm(
   gradients, max_gradient_norm)
 gradient_norm_summary = [tf.summary.scalar("grad_norm", gradient_norm)]
 gradient_norm_summary.append(
  tf.summary.scalar("clipped_gradient", tf.global_norm(clipped_gradients)))

 return clipped_gradients
w1 = tf.Variable([[3.0,2.0]])
# w2 = tf.Variable([[3,4]])
params = tf.trainable_variables()
res = tf.matmul(w1, [[3.0],[1.]])
opt = tf.train.GradientDescentOptimizer(1.0)
grads = tf.gradients(res,[w1])
clipped_gradients = gradient_clip(grads,2.0)
global_step = tf.Variable(0, name='global_step', trainable=False)
#update = opt.apply_gradients(zip(clipped_gradients,params), global_step=global_step)
with tf.Session() as sess:
 tf.global_variables_initializer().run()
 print sess.run(res)
 print sess.run(grads)
 print sess.run(clipped_gradients)

以上这篇TensorFlow梯度求解tf.gradients实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • tensorflow 实现自定义layer并添加到计算图中

    目的 将用户自定义的layer结合tensorflow自带的layer组成多层layer的计算图. 实现功能 对2D图像进行滑动窗口平均,并通过自定义的操作layer返回结果. import tensorflow as tf import numpy as np sess = tf.Session() #将size设为[1, 4, 4, 1]是因为tf中图像函数是处理四维图片的. #这四维依次是: 图片数量,高度, 宽度, 颜色通道 x_shape = [1,4,4,1] x_val = np.

  • TensorFlow实现自定义Op方式

    『写在前面』 以CTC Beam search decoder为例,简单整理一下TensorFlow实现自定义Op的操作流程. 基本的流程 1. 定义Op接口 #include "tensorflow/core/framework/op.h" REGISTER_OP("Custom") .Input("custom_input: int32") .Output("custom_output: int32"); 2. 为Op实现

  • tensorflow使用指定gpu的方法

    TensorFlow是一个基于数据流编程(dataflow programming)的符号数学系统,被广泛应用于各类机器学习(machine learning)算法的编程实现,其前身是谷歌的神经网络算法库DistBelief [1]  . Tensorflow拥有多层级结构,可部署于各类服务器.PC终端和网页并支持GPU和TPU高性能数值计算,被广泛应用于谷歌内部的产品开发和各领域的科学研究 . TensorFlow由谷歌人工智能团队谷歌大脑(Google Brain)开发和维护,拥有包括Ten

  • tensorflow 查看梯度方式

    1. 为什么要查看梯度 对于初学者来说网络经常不收敛,loss很奇怪(就是不收敛),所以怀疑是反向传播中梯度的问题 (1)求导之后的数(的绝对值)越来越小(趋近于0),这就是梯度消失 (2)求导之后的数(的绝对值)越来越大(特别大,发散),这就是梯度爆炸 所以说呢,当loss不正常时,可以看看梯度是否处于爆炸,或者是消失了,梯度爆炸的话,网络中的W也会很大,人工控制一下(初始化的时候弄小点等等肯定还有其它方法,只是我不知道,知道的大神也可以稍微告诉我一下~~),要是梯度消失,可以试着用用resn

  • TensorFlow设置日志级别的几种方式小结

    TensorFlow中的log共有INFO.WARN.ERROR.FATAL 4种级别.有以下几种设置方式. 1. 通过设置环境变量控制log级别 可以通过环境变量TF_CPP_MIN_LOG_LEVEL进行设置,TF_CPP_MIN_LOG_LEVEL的不同值的含义分别如下: Level Level for Humans Level Description 0 DEBUG all messages are logged (Default) 1 INFO INFO messages are no

  • 基于TensorFlow中自定义梯度的2种方式

    前言 在深度学习中,有时候我们需要对某些节点的梯度进行一些定制,特别是该节点操作不可导(比如阶梯除法如 ),如果实在需要对这个节点进行操作,而且希望其可以反向传播,那么就需要对其进行自定义反向传播时的梯度.在有些场景,如[2]中介绍到的梯度反转(gradient inverse)中,就必须在某层节点对反向传播的梯度进行反转,也就是需要更改正常的梯度传播过程,如下图的 所示. 在tensorflow中有若干可以实现定制梯度的方法,这里介绍两种. 1. 重写梯度法 重写梯度法指的是通过tensorf

  • TensorFlow梯度求解tf.gradients实例

    我就废话不多说了,直接上代码吧! import tensorflow as tf w1 = tf.Variable([[1,2]]) w2 = tf.Variable([[3,4]]) res = tf.matmul(w1, [[2],[1]]) grads = tf.gradients(res,[w1]) with tf.Session() as sess: tf.global_variables_initializer().run() print sess.run(res) print se

  • tensorflow中的梯度求解及梯度裁剪操作

    1. tensorflow中梯度求解的几种方式 1.1 tf.gradients tf.gradients( ys, xs, grad_ys=None, name='gradients', colocate_gradients_with_ops=False, gate_gradients=False, aggregation_method=None, stop_gradients=None, unconnected_gradients=tf.UnconnectedGradients.NONE )

  • tensorflow更改变量的值实例

    如下所示: from __future__ import print_function,division import tensorflow as tf #create a Variable w=tf.Variable(initial_value=[[1,2],[3,4]],dtype=tf.float32) x=tf.Variable(initial_value=[[1,1],[1,1]],dtype=tf.float32,validate_shape=False) init_op=tf.gl

  • tensorflow 变长序列存储实例

    问题 问题是这样的,要把一个数组存到tfrecord中,然后读取 a = np.array([[0, 54, 91, 153, 177,1], [0, 50, 89, 147, 196], [0, 38, 79, 157], [0, 49, 89, 147, 177], [0, 32, 73, 145]]) 图片我都存储了,这个不还是小意思,一顿操作 import tensorflow as tf import numpy as np def _int64_feature(value): if

  • tensorflow模型继续训练 fineturn实例

    解决tensoflow如何在已训练模型上继续训练fineturn的问题. 训练代码 任务描述: x = 3.0, y = 100.0, 运算公式 x×W+b = y,求 W和b的最优解. # -*- coding: utf-8 -*-) import tensorflow as tf # 声明占位变量x.y x = tf.placeholder("float", shape=[None, 1]) y = tf.placeholder("float", [None,

  • 有关Tensorflow梯度下降常用的优化方法分享

    1.tf.train.exponential_decay() 指数衰减学习率: #tf.train.exponential_decay(learning_rate, global_steps, decay_steps, decay_rate, staircase=True/False): #指数衰减学习率 #learning_rate-学习率 #global_steps-训练轮数 #decay_steps-完整的使用一遍训练数据所需的迭代轮数:=总训练样本数/batch #decay_rate-

  • TensorFlow 读取CSV数据的实例

    TensorFlow 读取CSV数据原理在此就不做详细介绍,直接通过代码实现: 方法一: 详细读取tf_read.csv 代码 #coding:utf-8 import tensorflow as tf filename_queue = tf.train.string_input_producer(["/home/yongcai/tf_read.csv"]) reader = tf.TextLineReader() key, value = reader.read(filename_q

  • tensorflow之自定义神经网络层实例

    如下所示: import tensorflow as tf tfe = tf.contrib.eager tf.enable_eager_execution() 大多数情况下,在为机器学习模型编写代码时,您希望在比单个操作和单个变量操作更高的抽象级别上操作. 1.关于图层的一些有用操作 许多机器学习模型可以表达为相对简单的图层的组合和堆叠,TensorFlow提供了一组许多常用图层,以及您从头开始或作为组合创建自己的应用程序特定图层的简单方法.TensorFlow在tf.keras包中包含完整的

  • tensorflow多维张量计算实例

    两个三维矩阵的乘法怎样计算呢?我通过实验发现,tensorflow把前面的维度当成是batch,对最后两维进行普通的矩阵乘法.也就是说,最后两维之前的维度,都需要相同. 首先计算shape为(2, 2, 3)乘以shape为(2, 3, 2)的张量. import tensorflow as tf import numpy as np a = tf.constant(np.arange(1, 13, dtype=np.float32), shape=[2, 2, 3]) b = tf.const

  • 关于Tensorflow中的tf.train.batch函数的使用

    这两天一直在看tensorflow中的读取数据的队列,说实话,真的是很难懂.也可能我之前没这方面的经验吧,最早我都使用的theano,什么都是自己写.经过这两天的文档以及相关资料,并且请教了国内的师弟.今天算是有点小感受了.简单的说,就是计算图是从一个管道中读取数据的,录入管道是用的现成的方法,读取也是.为了保证多线程的时候从一个管道读取数据不会乱吧,所以这种时候 读取的时候需要线程管理的相关操作.今天我实验室了一个简单的操作,就是给一个有序的数据,看看读出来是不是有序的,结果发现是有序的,所以

随机推荐