简体   繁体   English

TensorFlow 中是否有与 python reduce 等效的函数?

[英]Is there any function in TensorFlow equivalent to python reduce?

I want a function in TensorFlow which has the same effect as Pythion reduce()我想要一个与 Pythion reduce()效果相同的 TensorFlow 函数

For example, if I have a tensor a with value [a1, a2, a3] and a function func() , I want [func(a1, a2), func(func(a1, a2), a3)] .例如,如果我有一个值为[a1, a2, a3]的张量a和一个函数func() ,我想要[func(a1, a2), func(func(a1, a2), a3)] If a is a Python list I can simply do如果a是一个 Python 列表,我可以简单地做

from functools import reduce
# a = [a1, a2, a3]
A = reduce(func, a)

What if a is a Tensor (not a python list!) of TF object and I want the same A?如果a是 TF 对象的张量(不是 python 列表!)并且我想要相同的 A 怎么办? If no alternative TF function, how can I do it effectively?如果没有替代TF功能,我如何有效地做到这一点?

If you actually mean reduce, so the tensorflow equivalent are tf.foldl and tf.foldr .如果您实际上是指减少,那么 tensorflow 等效项是tf.foldltf.foldr Example:例子:

elems = tf.constant(["hello", "my", "name", "is", "inigo", "montoya"])
tf.foldl(lambda a, x: a +" " + x, elems)
#<tf.Tensor: shape=(), dtype=string, numpy=b'hello my name is inigo montoya'>
tf.foldr(lambda a, x: a +" " + x, elems)
#<tf.Tensor: shape=(), dtype=string, numpy=b'montoya inigo is name my hello'>

However, according to your example you probably want tf.scan :但是,根据您的示例,您可能需要tf.scan

tf.scan(lambda a, x: a +" " + x, elems)
<tf.Tensor: shape=(6,), dtype=string, numpy=
    ([b'hello', b'hello my', b'hello my name', b'hello my name is',
    b'hello my name is inigo', b'hello my name is inigo montoya'], dtype=object)>

You have not mentioned which type of func you would like to achieve, ie whether you just wanted to simply add all tensors, or take mean etc. For those type of operations there is tf.reduce_sum , and set of other reduce operations.您还没有提到您想要实现哪种类型的func ,即您是否只想简单地添加所有张量,或者取均值等。对于这些类型的操作,有tf.reduce_sum和其他一组 reduce 操作。 But if you want to have your own custom operation to be implemented by means of reduce, you can achieve it with tf.py_function .但是如果你想通过reduce的方式实现自己的自定义操作,可以通过tf.py_function来实现。 Check the code below:检查下面的代码:

import tensorflow as tf
from functools import reduce

tf.enable_eager_execution()

x = tf.convert_to_tensor(tf.random.normal([1, 4]))
y = tf.convert_to_tensor(tf.random.normal([1, 4]))
z = tf.convert_to_tensor(tf.random.normal([1, 4]))

print('X = ', x.numpy())
print('Y = ', y.numpy())
print('Z = ', z.numpy())

def custom_function(*elements):
    data = reduce(lambda x, y: x + y, elements)
    return data


result = tf.py_function(custom_function, [x, y, z], Tout=tf.float32)
print('Result = ', result.numpy())

Output:输出:

X =  [[ 0.9403639  -0.41230875  0.08281742  0.24365784]]
Y =  [[-1.2166423  -0.18330204 -1.2322044  -1.1254508 ]]
Z =  [[-0.42474216  0.997245   -2.319666    1.4623599 ]]
Result =  [[-0.70102054  0.40163422 -3.4690528   0.5805669 ]]

EDIT:编辑:

The output of any reduce function in Python has to be a single value. Python 中任何reduce函数的输出都必须是单个值。 In your case, you wish to accumulate the values of subsequent elements in a tensor and in the end produce another 1 dimensional Tensor.在您的情况下,您希望在张量中累积后续元素的值,并最终生成另一个一维张量。

I can't think of any built-in TensorFlow equivalent operation which can be used in this way.我想不出任何可以以这种方式使用的内置 TensorFlow 等效操作。 But, we can do the processing by indexing of tensor elements.但是,我们可以通过张量元素的索引来进行处理。 However, I am not sure, how much this solution can integrate with your overall model architecture.但是,我不确定该解决方案可以与您的整体模型架构集成多少。 Check the code below:检查下面的代码:

import tensorflow as tf
tf.enable_eager_execution()

x = tf.convert_to_tensor(tf.random.normal([5]))
print('X = ', x.numpy())

y = []
sum = x[0]
for index in range(1, len(x)):
    sum += x[index]
    y.append(sum)
y = tf.convert_to_tensor(y)
print('Sum = ', y.numpy())

Output:输出:

X =  [ 1.0281714  -1.5870513  -1.7896062   0.11207424  0.50049555]
Sum =  [-0.55887985 -2.348486   -2.2364118  -1.7359163 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM