- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 while_loop 在 Tensorflow ,但是当我尝试返回目标时 输出 从可调用的 while 循环中,它给了我一个错误,因为每次都会增加形状。
输出应包含基于 的(0 或 1)个值数据值(输入数组)。如 数据值大于 5 返回 1 否则返回 0 .返回值必须加到输出
这是代码::
import numpy as np
import tensorflow as tf
data = np.random.randint(10, size=(30))
data = tf.constant(data, dtype= tf.float32)
global output
output= tf.constant([], dtype= tf.float32)
i = tf.constant(0)
c = lambda i: tf.less(i, 30)
def b(i):
i= tf.add(i,1)
cond= tf.cond(tf.greater(data[i-1], tf.constant(5.)), lambda: tf.constant(1.0), lambda: tf.constant([0.0]))
output =tf.expand_dims(cond, axis = i-1)
return i, output
r,out = tf.while_loop(c, b, [i])
print(out)
sess= tf.Session()
sess.run(out)
r, out = tf.while_loop(c, b, [i])
ValueError: The two structures don't have the same number of elements.
First structure (1 elements): [tf.Tensor 'while/Identity:0' shape=() dtype=int32]
Second structure (2 elements): [tf.Tensor 'while/Add:0' shape=() dtype=int32, tf.Tensor 'while/ExpandDims:0' shape=unknown dtype=float32>]
a = tf.ones([10,4])
print(a)
a = tf.reduce_sum(a, axis = 1)
i =tf.constant(0)
c = lambda i, _:tf.less(i,10)
def Smooth(x):
return tf.add(x,2)
summ = tf.constant(0.)
def b(i,_):
global summ
summ = tf.add(summ, tf.cast(Smooth(a[i]), tf.float32))
i= tf.add(i,1)
return i, summ
r, smooth_l1 = tf.while_loop(c, b, [i, smooth_l1])
print(smooth_l1)
sess = tf.Session()
print(sess.run(smooth_l1))
最佳答案
tf.while_loop()
function 要求以下四个列表具有相同的长度,并且每个元素的类型相同:
cond
的参数列表函数(在本例中为 c
)。 body
的参数列表函数(在本例中为 b
)。 body
的返回值列表功能。 loop_vars
名单表示循环变量。 b
中添加相应的参数。和
c
,以及对应于
loop_vars
的元素:
c = lambda i, _: tf.less(i, 30)
def b(i, _):
i = tf.add(i, 1)
cond = tf.cond(tf.greater(data[i-1], tf.constant(5.)),
lambda: tf.constant(1.0),
lambda: tf.constant([0.0]))
# NOTE: This line fails with a shape error, because the output of `cond` has
# a rank of either 0 or 1, but axis may be as large as 28.
output = tf.expand_dims(cond, axis=i-1)
return i, output
# NOTE: Use a shapeless `tf.placeholder_with_default()` because the shape
# of the output will vary from one iteration to the next.
r, out = tf.while_loop(c, b, [i, tf.placeholder_with_default(0., None)])
tf.expand_dims()
的调用)似乎不正确,该程序无法按原样运行,但希望这足以让您入门。
关于tensorflow - Tensorflow 中的 while_loop 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46768386/
在我的问题中,我需要在每个训练步骤的数据中使用 1 个示例运行 GD。众所周知, session.run() 有开销,因此训练模型时间太长。 为了避免开销,我尝试使用 while_loop 并通过一次
我目前很难理解 tensorflow 的工作原理,而且我觉得 python 界面有点晦涩难懂。 我最近尝试在 tf.while_loop 中运行一个简单的 print 语句,但有很多事情我仍然不清楚:
我想按行更新 tensorflow 中tf.while_loop内的二维tf.variable。因此,我使用 tf.assign 方法。问题是我的实现和 parallel_iterations>1 结
我正在使用 tf.while_loop 动态连接张量。 代码 embeds_raw = tf.constant(np.array([ [1, 1], [1, 1], [2, 2
我正在尝试更新嵌套 while_loop() 中的二维张量。但是,当将变量传递给第二个循环时,我无法使用 tf.assign() 更新它,因为它会抛出此错误: ValueError: Sliced a
我在 Tensorflow 中使用 while_loop 来迭代张量并提取给定维度上的特定切片。对于每一步,我都需要使用解码器 RNN 来生成一系列输出符号。我正在使用 tf.contrib.seq2
我已经使用带有大型矩阵的 TensorFlow while_loop 实现了一个算法,我最近注意到奇怪的行为:我在不同的运行中得到不同的结果,有时甚至是 nan 值。我花了一些时间来缩小问题范围,现在
我尝试使用 while_loop 在 Tensorflow ,但是当我尝试返回目标时 输出 从可调用的 while 循环中,它给了我一个错误,因为每次都会增加形状。 输出应包含基于 的(0 或 1)个
只要仍有一些列使用 tf.while_loop,我就会尝试将张量切成小张量。 注意:我使用这种方式是因为我无法在图形构建时(没有 session )循环占位符中的值被视为张量而不是整数。 [ 5 7
在文档中,tf.while_loop 的主体必须是 python 可调用的。 i = tf.constant(0) b = lambda i: tf.add(i,1) c = lambda i: tf
当我尝试运行代码时出现以下错误: model = Sequential() model.add(LSTM(4, input_shape=(1, look_back))) TypeError: whil
我正在使用 while_loop 迭代更新矩阵。对于密集张量,循环运行良好,但是当我使用稀疏张量时,出现以下错误: InvalidArgumentError: Number of rows of a_
目标是在 TensorFlow 中实现一个循环函数,以随时间过滤信号。 input随后呈现为 [batch, in_depth, in_height, in_width, in_channels]
给定一个 TensorFlow tf.while_loop,我如何计算每个时间步的 x_out 相对于网络所有权重的梯度? network_input = tf.placeholder(tf.floa
下面我有一个 Tensorflow RNN Cell 的实现,旨在模拟本文中 Alex Graves 的算法 ACT:http://arxiv.org/abs/1603.08983 . 在通过 rnn
我正在尝试使用 tf.scatter_update() 更新 tf.while_loop() 内的 tf.Variable。但是,结果是初始值而不是更新后的值。这是我想要做的示例代码: from __
我想处理不同形状的张量序列(列表)并输出另一个张量列表。考虑每个时间戳上具有不同隐藏状态大小的 RNN。类似的东西 输入:[tf.ones((1, 2, 2)), tf.ones((2, 2, 3))
我有一个 tensorflow 模型,其中层的输出是二维张量,例如 t = [[1,2], [3,4]] . 下一层需要一个由该张量的每一行组合组成的输入。也就是说,我需要把它变成t_new = [[
重现步骤 我正在使用TensorFlow实现一个需要使用tf.while_loop()的网络 import tensorflow as tf import numpy as np class mode
我最初使用 glfw 编写我的游戏。然而,由于它缺乏 android 的可移植性,我不得不用 SDL 替换我所有的 glfw 代码,因为 SDL 更具可移植性。 我使用 glfw 框架工作的原始游戏循
我是一名优秀的程序员,十分优秀!