【问题标题】:TensorFlow 2.0 - @tf.function and tf.unstack TypeErrorTensorFlow 2.0 - @tf.function 和 tf.unstack TypeError
【发布时间】:2020-05-21 08:47:05
【问题描述】:

我遇到了以下问题:通过使用@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:在用户代码中:

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

TypeError: 'set' object is not callable

不确定是由于我对 AutoGraph 和 @tf.function 的无知还是其他原因造成的。如果有人能让我了解发生了什么,将不胜感激:-)

【问题讨论】:

  • 您的代码在 TensorFlow 2.2.0 上运行良好。
  • 天哪!你说的对;我从 jupyter-notebook 运行。非常感谢!

标签: python tensorflow machine-learning tensorflow2.0


【解决方案1】:

我可以在 Tensorflow 1.15.0 和 2.1.0 中的 Jupyter Notebook 中执行您的代码,而不会出现任何错误。

为了社区的利益,下面我提到了使用 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)

输出:

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)>]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多