tensorflow: variable的值与variable.read_value()的值区别详解

yipeiwu_com7年前Python基础

问题

查看 tensorflow api manual 时,看到关于 variable.read_value() 的注解如图:

那么在 tensorflow 中,variable的值 与 variable.read_value()的值 到底有何区别?

实验代码

# coding=utf-8
import tensorflow as tf

# Create a variable.
w = tf.Variable(initial_value=10., dtype=tf.float32)

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

print '\nw.read_value() :\n', w.read_value(), '\n', w.read_value().eval()
print '\nw :\n', w, '\n', w.eval()

sess.close()

由打印结果可看出,

variable的值 与 variable.read_value()的值 之间的 区别 仅仅在于 tensor类型 的不一样;

但 eval() 后打印出的结果值是 一样的 。

w.read_value() :
Tensor("read:0", shape=(), dtype=float32) 
10.0

w :
<tf.Variable 'Variable:0' shape=() dtype=float32_ref> 
10.0

以上这篇tensorflow: variable的值与variable.read_value()的值区别详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python函数局部变量、全局变量、递归知识点总结

函数局部变量 全局变量 及其作用域 #简单类型(int str等)变量的局部变量与全局变量及其作用域的关系 name = "xxx" #第一级顶头定义的变量都称为全局变量,其作用域从...

在Python中使用itertools模块中的组合函数的教程

理解新概念 Python V2.2 中引入了迭代器的思想。唔,这并不十分正确;这种思想的“苗头”早已出现在较老的函数 xrange() 以及文件方法 .xreadlines() 中了。通...

Python编程中装饰器的使用示例解析

装饰函数和方法 我们先定义两个简单的数学函数,一个用来计算平方和,一个用来计算平方差: # get square sum def square_sum(a, b): return...

Python-Tkinter Text输入内容在界面显示的实例

使用Tkinter(py2.7)text文本框中输入内容在界面中显示–较为规整的代码: import Tkinter as tk class Window: def __init...

python创建线程示例

复制代码 代码如下:import threadingfrom time import sleep def test_func(id):    for i i...