简体   繁体   English

TensorFlow 2.0 - @tf.function 和 tf.unstack TypeError

[英]TensorFlow 2.0 - @tf.function and tf.unstack TypeError

I have encountered the following problem: by using @tf.function I would like to unstack a tensor along a defined component.我遇到了以下问题:通过使用@tf.function,我想沿定义的组件展开张量。

@tf.function
def f1(x):
    y = tf.unstack(x)
    return y 

@tf.function
def f2(x):
    y = tf.unstack(x, axis=0)
    return y

@tf.function
def f3(x):
    y = tf.unstack(x, axis=1)
    return y

x = tf.random.uniform((4,2))
y1 = tf.unstack(x, axis=0) #f2
y2 = tf.unstack(x, axis=1) #f3
y = f1(x) # No problem! (output equal to y1)
z = f2(x) #Problem!
zz = f3(x) #Problem

TypeError: in user code:类型错误:在用户代码中:

<ipython-input-339-c5b8c0b032bb>:8 f2  *
    y = tf.unstack(x, axis=0)

TypeError: 'set' object is not callable

Not sure if it is due to my ignorance with AutoGraph and @tf.function or something else is going wrong.不确定这是否是由于我对 AutoGraph 和 @tf.function 的无知,还是因为其他问题。 Would appreciate if someone there can make me understand what's going on:-)如果有人能让我了解发生了什么,我将不胜感激:-)

I am able to execute your code in Jupyter Notebook both in Tensorflow 1.15.0 and 2.1.0 without any error.我可以在 Tensorflow 1.15.0 和 2.1.0 中的 Jupyter Notebook 中执行您的代码,而不会出现任何错误。

For the benefit of community, below i have mentioned successful run with outputs using TF 2.1.0.为了社区的利益,下面我提到了使用 TF 2.1.0 成功运行输出。

import tensorflow as tf
print(tf.__version__)

@tf.function
def f1(x):
    y = tf.unstack(x)
    return y 

@tf.function
def f2(x):
    y = tf.unstack(x, axis=0)
    return y

@tf.function
def f3(x):
    y = tf.unstack(x, axis=1)
    return y

x = tf.random.uniform((4,2))
y1 = tf.unstack(x, axis=0) #f2
y2 = tf.unstack(x, axis=1) #f3
y = f1(x) # No problem! (output equal to y1)
z = f2(x) 
zz = f3(x)
print(y)
print(z)
print(zz)

Output: Output:

2.1.0
[<tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.42976737, 0.00961947], dtype=float32)>, <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.64688444, 0.7597277 ], dtype=float32)>, <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.05788946, 0.5703846 ], dtype=float32)>, <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.5646384 , 0.36961722], dtype=float32)>]
[<tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.42976737, 0.00961947], dtype=float32)>, <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.64688444, 0.7597277 ], dtype=float32)>, <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.05788946, 0.5703846 ], dtype=float32)>, <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.5646384 , 0.36961722], dtype=float32)>]
[<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0.42976737, 0.64688444, 0.05788946, 0.5646384 ], dtype=float32)>, <tf.Tensor: shape=(4,), dtype=float32, numpy=array([0.00961947, 0.7597277 , 0.5703846 , 0.36961722], dtype=float32)>]

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

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