有没有办法将函数应用于 TensorFlow 数组的第 0 维,其形状为(None,2)?

huangapple go评论87阅读模式
英文:

Is there a way to apply a function to dimension 0 of a tensorflow array having the shape (None, 2)

问题

我有一个与TensorFlow相关的非常技术性的问题。

我有一个维度为(None, 2)TensorFlow矩阵。我需要在矩阵的维度0上应用一个函数,比如some_function,即在所有行上应用。问题是维度0是None类型(它是动态的,因为它取决于馈送到NN模型的输入大小),它会导致错误,显示None不是整数类型。有两个tf函数tf.map_fntf.scan,用于迭代Tensorflow数组。但这两个函数都无法在None维度上工作。

也许你可以通过定义一个形状为(None, 2)的测试TensorFlow数组,并尝试将任何测试函数应用于第一维来检查它。任何帮助或输入将不胜感激!

英文:

I have a very technical question related to TensorFlow.

I have a TensorFlow matrix having a dimension of (None, 2). I need to apply a function, say some_function, only on Dimension 0 of the matrix i.e. over all rows. The issue is dimension 0 is a None type (it is dynamic as it depends on the input size being fed to the NN model), and it gives an error showing None is not an integer type. There are two tf functions: tf.map_fn and tf.scan to iterate over a Tensorflow array. But both won't work over a None dimension.

Maybe you could check it by defining a test TensorFlow array of shape (None, 2) and try applying any test function to the first dimension. Any help/input would be appreciated!

答案1

得分: 1

由于这是一个Keras模型输出,如果我尝试执行以下操作,

res2 = tf.map_fn(lambda y: y*2, model.output)


你会得到,

> TypeError: 'Tensor'对象无法解释为整数

但是,以下方式可以正常工作,

生成产生要映射的输出的初始模型

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(2, input_shape=(2,)))

res = tf.keras.layers.Lambda(lambda x: tf.map_fn(lambda y: y*2, x))(model.output)


然后,你可以定义一个新模型,并使用它来获取`tf.map_fn`的结果。

model2 = tf.keras.Model(inputs=model.inputs, outputs=res)
print(model2.predict(np.array([[1,2],[3,4]])))


**PS**:但这与第一维度为`None`无关。`tf.map_fn`可以很好地处理`None`维度。你可以通过在TF 1.x上运行`tf.map_fn`在`tf.placeholder([None,2])`上来验证这一点。

因为它在该维度上迭代地应用一个函数,不需要知道大小来执行此操作。
英文:

Since this is a keras model output, if I try to do the following,

res2 = tf.map_fn(lambda y: y*2, model.output)

You get,

> TypeError: 'Tensor' object cannot be interpreted as an integer

But, the following would work,

# Inital model that produces the output you want to map
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(2, input_shape=(2,)))

res = tf.keras.layers.Lambda(lambda x: tf.map_fn(lambda y: y*2, x))(model.output)

Then you define a new model, and use that to get the result of the tf.map_fn.

model2 = tf.keras.Model(inputs=model.inputs, outputs=res)
print(model2.predict(np.array([[1,2],[3,4]])))

PS: But this is nothing to do with the first dimension being None. tf.map_fn can deal with None dimension just fine. You can verify this by running tf.map_fn on a tf.placeholder([None,2]) in TF 1.x.

Because it is iteratively applying a function over that dimension and does not need to know the size to do so.

huangapple
  • 本文由 发表于 2020年1月6日 14:52:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定