簡體   English   中英

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

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

我遇到了以下問題:通過使用@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

類型錯誤:在用戶代碼中:

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

TypeError: 'set' object is not callable

不確定這是否是由於我對 AutoGraph 和 @tf.function 的無知,還是因為其他問題。 如果有人能讓我了解發生了什么,我將不勝感激:-)

我可以在 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)

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