从一个条件创建另一个张量

时间:2018-10-15 10:57:29

标签: python tensorflow

我有一个这样的张量

...
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]被随机放置只是为了产生想法)

1 个答案:

答案 0 :(得分:2)

您可以只与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.wheretf.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.]]]