什么是张量流浮动参考? [英] What is a tensorflow float ref?

查看:30
本文介绍了什么是张量流浮动参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试运行以下基本示例以运行条件计算,我收到以下错误消息:

<块引用>

'x' 传递的浮点数与预期的 float_ref 不兼容

什么是 tensorflow float_ref 以及如何修改代码?

 将 tensorflow 导入为 tf从 tensorflow.python.ops.control_flow_ops 导入 conda = tf.Variable(tf.constant(0.),name="a")b = tf.Variable(tf.constant(0.),name="b")x = tf.Variable(tf.constant(0.),name="x")定义添加():x.assign(a + b)返回 x定义最后():返回 x计算= cond(x==0.,add,last)使用 tf.Session() 作为 s:val = s.run([计算], {a: 1., b: 2., x: 0.})打印(val) # 3val=s.run([计算],{a:4.,b:5.,x:val})打印(val) # 3

解决方案

float_ref 这里指的是对浮点的引用,即你的 Tensorflow 浮点变量 x.

此处所述,您面临此错误是因为您无法同时分配和传递变量为同一会话中的 feed_dict 像您在此语句中所做的那样运行:

val = s.run([计算], {a: 1., b: 2., x: 0.})

当您解析该语句以结尾时,它变得更加明显:

val = s.run([x.assign( a + b)], {a: 1., b: 2., x: 0.})

Trying to run the following basic example to run a conditional calculation I got the following error message:

'x' was passed float incompatible with expected float_ref

what is a tensorflow float_ref and how does the code have to be modified?

import tensorflow as tf
from tensorflow.python.ops.control_flow_ops import cond

a = tf.Variable(tf.constant(0.),name="a")
b = tf.Variable(tf.constant(0.),name="b")
x = tf.Variable(tf.constant(0.),name="x")

def add():
    x.assign( a + b)
    return x

def last():
    return x

calculate= cond(x==0.,add,last)

with tf.Session() as s:
    val = s.run([calculate], {a: 1., b: 2., x: 0.})
    print(val) # 3
    val=s.run([calculate],{a:4.,b:5.,x:val})
    print(val) # 3

解决方案

float_ref here refers to a reference to a float, i.e. your Tensorflow float variable x.

As explained here you are facing this error because you can't simultaneously assign and pass a variable as a feed_dict in the same session run like you are doing in this statement:

val = s.run([calculate], {a: 1., b: 2., x: 0.})

It becomes more obvious when you resolve that statement to end up with:

val = s.run([x.assign( a + b)], {a: 1., b: 2., x: 0.})

这篇关于什么是张量流浮动参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆