python - 从另一个有条件的张量创建一个张量
问题描述
我有这样的张量
...
0
0
2
0
1
0
3
1
0
0
0
0
2
3
...
我必须找到一种方法来创建一个形状相同但只有 0 和 1 的张量。这个张量必须与特定数字处于相同的位置。这里有一个例子
# for number 2
...
0
0
1
0
0
0
0
0
0
0
0
0
1
0
...
有没有内置函数可以做到这一点?我在网上和文档中找不到任何东西。
然后我必须将这个张量乘以这样的数字列表
l = [..., 1.3, 4.3, ...]
获得这个
...
0
0
1.3
0
0
0
0
0
0
0
0
0
4.3
0
...
有没有办法获得这个结果?
编辑
在我的情况下,我在应用这种方法时遇到了麻烦。我解释一下。我的指数张量是这样的
points = tf.constant([[[0], [1], [0], [2], [1], [0], [2], [2], [0], [1]],
[[1], [2], [0], [0], [1], [0], [1], [2], [0], [2]],
[[0], [2], [1], [0], [2], [0], [1], [2], [0], [1]]], dtype=tf.float32)
# shape=(3, 10, 1)
我必须只取第一行的索引,所以我以这种方式取它们
idx = tf.cast(tf.where(tf.equal(tf.slice(points, [0, 0, 0], [1, 10, 1]), tf.constant([2], dtype=tf.float32))), dtype=tf.int32)
# shape=(3, 3)
idx = tf.expand_dims(idx, 0)
# shape=(1, 3, 3)
要提供的值在一个名为的列表中vectors
,我将它们转换为具有相同的形状,如下所示
to_feed = np.expand_dims(np.array(vectors), 0)
# shape=(1, 3, 3)
然后我以这种方式应用该方法
res = tf.scatter_nd(idx, to_feed, tf.shape(tf.slice(points, [0, 0, 0], [1, 10, 3])))
但我得到这个错误
ValueError: The inner 0 dimensions of output.shape=[?,?,?] must match the inner 1 dimensions of updates.shape=[1,3,3]: Shapes must be equal rank, but are 0 and 1 for 'ScatterNd' (op: 'ScatterNd') with input shapes: [1,3,3], [1,3,3], [3].
我最后需要的是这样的张量
to_feed = [[[10, 10, 10], [11, 11, 11], [12, 12, 12]]] # shape=(1, 3, 3)
res = [[[0, 0, 0], [10, 10, 10], [0, 0, 0], [11, 11, 11], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [12, 12, 12], [0, 0, 0]]] # shape=(1, 10, 3)
([0, 0, 0]
随机放置只是为了有一个想法)
解决方案
您可以与 进行比较,tf.equal
然后将布尔结果转换为数字tf.cast
:
import tensorflow as tf
vector = tf.placeholder(tf.int32, [None])
num = tf.placeholder(tf.int32, [])
result = tf.cast(tf.equal(vector, num), tf.int32)
with tf.Session() as sess:
print(sess.run(result, feed_dict={vector: [0, 0, 2, 1, 0, 3, 2, 0], num: 2}))
输出:
[0 0 1 0 0 0 1 0]
编辑:
以上解决了更简单的第一个问题,但我认为您需要解决您的问题类似于以下内容,使用tf.where
and tf.scatter_nd
:
import tensorflow as tf
vector = tf.placeholder(tf.int32, [None])
num = tf.placeholder(tf.int32, [])
values = tf.placeholder(tf.float32, [None])
idx = tf.where(tf.equal(vector, num))
result = tf.scatter_nd(idx, values, tf.cast(tf.shape(vector), idx.dtype))
with tf.Session() as sess:
print(sess.run(result, feed_dict={vector: [0, 2, 1, 2, 3, 2, 2, 0],
num: 2,
values: [3, 4.4, 2, 2.2]}))
输出:
[0. 3. 0. 4.4 0. 2. 2.2 0. ]
编辑:
关于您的最新示例,我整理了一个片段,说明我认为您正在努力实现的目标:
import tensorflow as tf
points = tf.constant([[[0], [1], [0], [2], [1], [0], [2], [2], [0], [1]],
[[1], [2], [0], [0], [1], [0], [1], [2], [0], [2]],
[[0], [2], [1], [0], [2], [0], [1], [2], [0], [1]]], dtype=tf.float32)
vectors = tf.constant([[10, 11, 12], [20, 21, 22], [30, 31, 21]], dtype=tf.float32)
points_row = points[:1]
idx = tf.where(tf.equal(points_row, 2))
idx = tf.cast(idx, tf.int32)
res_shape = tf.concat([tf.shape(points_row), [tf.shape(vectors)[1]]], axis=0)
res = tf.scatter_nd(idx, vectors, res_shape)
res = tf.squeeze(res, 2)
with tf.Session() as sess:
print(sess.run(res))
输出:
[[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[10. 11. 12.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[20. 21. 22.]
[30. 31. 21.]
[ 0. 0. 0.]
[ 0. 0. 0.]]]
推荐阅读
- javascript - 使用 ajax 编辑要点
- javascript - 无法解析字符串 API 数据
- protractor - Proto.Actor 中的异步请求/响应?
- google-maps - Google Places API 自动完成不返回远程邮政编码
- wordpress - 如何在 Wordpress 类别 rss 提要上显示特色图片?
- algorithm - 递归时间复杂度计算
- node.js - 在 nodejs 上使用加密为 jsonwebtoken 创建 RSA SHA256
- angular - Aspnetboilerplate Angular 文件上传
- c - 即使文件存在,访问(路径,标志)也返回 0
- c - 在 C 中使用 pthread 和并发的 Segfault