Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeError:您试图在未声明为动态的层中使用Python控制流。将'dynamic=True'传递给类构造函数_Python_Tensorflow_Neural Network_Tensorflow2.0_Tf.keras - Fatal编程技术网

TypeError:您试图在未声明为动态的层中使用Python控制流。将'dynamic=True'传递给类构造函数

TypeError:您试图在未声明为动态的层中使用Python控制流。将'dynamic=True'传递给类构造函数,python,tensorflow,neural-network,tensorflow2.0,tf.keras,Python,Tensorflow,Neural Network,Tensorflow2.0,Tf.keras,我正在使用TensorFlow 2.0.0和tf.keras创建一个模型网络,该网络接受n个输入,[x1,x2,x3,x4,x5,.xn],并计算f(x1,x2,x3,x4,x5,.xn) 我已将我的模型定义如下: def custom_func(vec): # Test function specifically for a 2-D input [x,y] = vec x1 = tf.math.atanh(x) y1 = tf.math.at

我正在使用
TensorFlow 2.0.0
tf.keras
创建一个模型网络,该网络接受n个输入,
[x1,x2,x3,x4,x5,.xn]
,并计算
f(x1,x2,x3,x4,x5,.xn)

我已将我的模型定义如下:

def custom_func(vec):   # Test function specifically for a 2-D input
        [x,y] = vec
        x1 = tf.math.atanh(x)
        y1 = tf.math.atanh(y)
        return tf.math.exp(-x1**2 + -y1**2)*(x1**2 + y1**2)

ndim = 2     #Input is 2-D for a sample case
model2 = Sequential()
model2.add(Dense(1, kernel_initializer='ones',bias_initializer='zeros',
                    activation=custom_func, input_shape=(ndim,)))

print(model2.predict(np.array([[1,2],[3,4]])))
运行以下代码块时,我得到错误:

TypeError: You are attempting to use Python control flow in a layer that was not declared to be dynamic. Pass `dynamic=True` to the class constructor.
Encountered error:
"""
iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
"""

什么可能导致这个错误?我该如何解决这个问题?任何帮助/建议都会非常有用。

请尝试在函数定义之前添加@tf.function。

即使我不清楚您想要做什么,我还是以这种方式为您进行了尝试。希望它能帮助你。当我们使用渴望执行时,它允许立即评估操作,而无需构建图形:操作返回具体值,而不是构建计算图形以供以后运行。m.我使用keras激活作为自定义激活函数,使用dynamic=True作为稠密层。它一直工作到这一点

import tensorflow as tf

import numpy as np

from keras.layers import Activation
from keras import backend as K
from keras.utils.generic_utils import get_custom_objects

@tf.function
def custom_activation(vec):   # Test function specifically for a 2-D input
  [x,y] = vec
  x1 = tf.math.atanh(x)
  y1 = tf.math.atanh(y)
  return tf.math.exp(-x1**2 + -y1**2)*(x1**2 + y1**2)



ndim = 2     #Input is 2-D for a sample case


mnist_model = tf.keras.Sequential([tf.keras.layers.Dense(1, kernel_initializer='ones',bias_initializer='zeros',activation=Activation('custom_activation'), input_shape=(ndim,),dynamic=True)])

@tf.function
def output():
  print(mnist_model(np.array([[1,2],[3,4]])))


output()
输出结果:

Using TensorFlow backend.
Tensor("sequential/dense/Placeholder:0", shape=(2, 1), dtype=float32)

这个答案可能会有帮助:
vec
是一个
tf.Tensor
,您不能执行
[x,y]=vec
,这会引发错误。您想在这里做什么?如果可能,请努力提供额外的解释,而不仅仅是代码。这些答案往往更有用,因为它们帮助社区成员,特别是新开发人员更好地理解解决方案的推理,并有助于防止需要解决后续问题。