- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 tensorflow 中的 map_fn 将转换应用于列向量,但它不起作用。
对于下面的列向量:
elems = np.array([[1.0], [2.0], [3.0]])
当我这样做时:
tf_m = tf.map_fn(lambda x: x + 1.0, elems)
with tf.Session() as sess:
res = sess.run(tf_m)
print(str(res))
我得到了我期望的结果,即这个列向量:
[[2.]
[3.]
[4.]]
但是,当我这样做时:
tf_m2 = tf.map_fn(lambda x: x+1 if x % 2 > 0 else x, elems)
with tf.Session() as sess:
res = sess.run(tf_m2)
print(str(res))
代码失败,出现以下异常:
TypeError: Using a
tf.Tensor
as a Pythonbool
is not allowed. Useif t is not None:
instead ofif t:
to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
我试过打印 x 的类型,它是一个形状为 (1,) 的张量。所以,看起来正在发生的事情是,这些值没有作为标量值传递到 lambda 中,而是作为形状为 (1,) 的张量; % 被广播,产生另一个形状为 (1,) 的张量,但该张量不能再应用 >= 运算符。
有没有办法让它工作?有没有办法获得一个我可以应用 >= 运算符的实际 标量?如果没有,是否有我可以使用的 map_fn 的有效替代方案?
(我查看了 tf.cond,但我不清楚如何在这种情况下使用它。据我了解,tf.cond 生成一个操作,而不是可调用的,所以我将如何使用它从 map_fn 应用的 lambda 中?)
最佳答案
您可以使用 tf.map_fn
来完成和 tf.cond
像这样:
elems_shape = tf.shape(elems)
elems_flat = tf.reshape(elems, [-1])
tf_m2_flat = tf.map_fn(lambda x: tf.cond(x % 2 > 0, lambda: x + 1, lambda: x), elems_flat)
tf_m2 = tf.reshape(tf_m2_flat, elems_shape)
但您也可以简单地使用 tf.where
像这样:
tf_m2 = tf.where(elems % 2 > 0, elems + 1, elems)
关于python - 尝试在 tensorflow 的 map_fn 中使用 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49649186/
当我在元组上使用 map_fn 时,我无法理解它的作用。为了测试,我做了以下事情: a = tf.constant([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) b = tf
看演示: elems = np.array([1, 2, 3, 4, 5, 6]) squares = map_fn(lambda x: x * x, elems) # squares == [1,
我正在尝试构造我的参数,以便它们可以与 tf.map_fn() 一起正常工作,但大多数示例文档仅讨论与函数参数形状相同的数组或张量。 链接包括: Does tensorflow map_fn supp
tf.map_fn 是否像 python 的 native map 函数(下面提供的示例)所支持的那样支持采用多个张量? a = [1,2,3,4] b = [17,12,11,10] print(m
我想在维度为 的矩阵中每个像素的深度 channel 对应的每个向量上映射一个 TensorFlow 函数。 [batch_size, H, W, n_channels] . 换句话说,对于我在批处理
我运行下面的代码,以便从给定的索引矩阵获取填充矩阵(words_chars_ids 形状为 (6,200,20))。结果的形状为 (6,200,20,emb_size),其中对于输出中的每个条目,它都
我正在使用 Tensorflow 的数据集 API 读取各种图像(数据和标签)。由于数据集队列在 CPU 上,因此复制数据的成本很高。但是,我似乎找不到避免这种情况的方法。 问题:我能否以统一的顺序(
使用 Tensorflow 1.4 我想使用映射函数计算张量的元素逆 (x --> 1/x)。如果张量中某个元素的值为零,我希望输出为零。 以 tensor: [[0, 1, 0], [0.5, 0.
我正在研究 map_fn 函数,注意到它输出一个 TensorArray,这应该意味着它能够输出“锯齿状”张量(其中内部的张量具有不同的第一维)。 我试着用这段代码看看这个: import tenso
我尝试使用 tf.map_fn 在 Pycharm 中使用 Tensorflow 获得多个输入的转折点。 但是,当我尝试这样做时, 我收到错误:TypeError: testzz() missing
a = tf.constant([[1,2,3],[4,5,6]]) b = tf.constant([True, False], dtype=tf.bool) a.eval() array([[1,
我想处理不同形状的张量序列(列表)并输出另一个张量列表。考虑每个时间戳上具有不同隐藏状态大小的 RNN。类似的东西 输入:[tf.ones((1, 2, 2)), tf.ones((2, 2, 3))
我正在尝试使用 tensorflow 中的 map_fn 将转换应用于列向量,但它不起作用。 对于下面的列向量: elems = np.array([[1.0], [2.0], [3.0]]) 当我这
我有一个形状为 [a,n] 的张量 A,我需要用另一个形状为 B 的张量执行操作 my_op [b,n] 使得生成的张量 C 的形状为 [a,b]。 换句话说:对于 A (A[0], A 1 ,...
是否可以在具有单个值的张量上运行 map_fn? 以下工作: import tensorflow as tf a = tf.constant(1.0, shape=[3]) tf.map_fn(lam
所以我想要的东西的伪代码是: splitted_outputs = [tf.split(output, rate, axis=0) for output in outputs] 其中,outputs
我在 tensorflow 中构建一个神经网络,它处理 3D 数据并且应该预测输入数据中地标的位置。该策略是密集地(针对每个体素)预测实际地标周围半径 r 的球体中的类别,并预测指向地标实际位置的偏移
当我试图让 TensorFlow 的 map_fn 在我的 GPU 上运行时,我遇到了一个奇怪的问题。这是一个最小的错误示例: import numpy as np import tensorflow
我有一个张量,我使用 tf.map_fn 逐行处理。现在我想将索引作为参数包含在传递给 tf.map_fn 的函数中。在 numpy 中,我可以使用 enumerate 获取该信息并将其传递到我的 l
我正在尝试创建自己的损失函数: def custom_mse(y_true, y_pred): tmp = 10000000000 a = list(itertools.permuta
我是一名优秀的程序员,十分优秀!