9

我正在尝试在 Eager Execution 中运行基本的 CNN keras 模型,但 Tensorflow 拒绝将该模型视为急切。我最初在稳定的 1.13 分支(最新)中尝试过这个,确保启用急切执行而没有结果。我升级到 2.0(最新)但又没有。

模型

class CNN2(tf.keras.Model):
  
  def __init__(self, num_classes=7):
    super(CNN2, self).__init__()
    self.cnn1 = tf.keras.layers.Conv2D(32, (5,5), padding='same', strides=(2, 2),
                                      kernel_initializer='he_normal')
    self.bn1 = tf.keras.layers.BatchNormalization()
    self.cnn2 = tf.keras.layers.Conv2D(64, (5,5), padding='same', strides=(2, 2),
                                      kernel_initializer='he_normal')
    self.cnn3 = tf.keras.layers.Conv2D(128, (5,5), padding='same', strides=(2, 2),
                                      kernel_initializer='he_normal')
    self.bn2 = tf.keras.layers.BatchNormalization()
    self.pool = tf.keras.layers.MaxPooling2D((2,2))
    self.dnn1 = tf.keras.layers.Dense(128)
    self.dropout1 = tf.keras.layers.Dropout(0.45)
    self.flatten = tf.keras.layers.Flatten()
    self.dnn2 = tf.keras.layers.Dense(512)
    self.dnn3 = tf.keras.layers.Dense(256)
    self.classifier = tf.keras.layers.Dense(num_classes)    

  def simpleLoop(self, inputs, x):
        #x_Numpy = x.numpy(),
        for i, input in inputs:
            print("{0} - {1}".format(i,len(input)))             
    
  def call(self, inputs, training=None, mask=None):
    print(tf.executing_eagerly())
    x = tf.nn.leaky_relu(self.cnn1(inputs))
    x = self.bn1(x)
    x = self.pool(x)
    x = tf.nn.leaky_relu(x)
    x = tf.nn.leaky_relu(self.bn2(self.cnn2(x)))
    x = self.pool(x)
    x = self.dropout1(tf.nn.leaky_relu(self.cnn3(x)))
    x = self.flatten(x)
    self.simpleLoop(inputs, x)
    x = self.dropout1(self.dnn1(x))
    x = self.dropout1(self.dnn2(x))
    x = self.dropout1(self.dnn3(x))
    output = self.classifier(x)
    
    #with tf.device('/cpu:0'):
    output = tf.nn.softmax(output)
      
    return output

参数设置

batch_size = 50
epochs = 150
num_classes = 7

检查 Eager 是否开启和版本

print(tf.executing_eagerly())
print(tf.__version__)
>>True
>>2.0.0-alpha0

运行模型

modelE = CNN2(num_classes)
modelE.run_eagerly = True
print(modelE.run_eagerly)


#model = CNN2(num_classes)
modelE.compile(optimizer=tf.optimizers.Adam(0.00008), loss='categorical_crossentropy', 
              metrics=['accuracy'], run_eagerly=True)

# TF Keras tries to use entire dataset to determine shape without this step when using .fit()
# Fix = Use exactly one sample from the provided input dataset to determine input/output shape/s for the model
dummy_x = tf.zeros((1, size, size, 1))
modelE._set_inputs(dummy_x)

# Train
hist = modelE.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, 
          validation_data=(x_test, y_test), verbose=1)

# Evaluate on test set
scores = modelE.evaluate(x_test, y_test, batch_size, verbose=1)

这会导致错误 AttributeError: 'Tensor' object has no attribute 'numpy'

当我删除有问题的行时x.numpy(),我得到了这个错误 TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn.

它还为模型方法中的print(tf.executing_eagerly())位置打印 False。def call()


如何强制它进入渴望模式而不是图形?我再次在最新的 1.13 和 2.0 中尝试了这个。这是一个错误吗?

4

2 回答 2

4

花了一段时间才找到适合我的解决方案tensorflow==2.0.0,所以我想在这里分享它,以防它也帮助其他人:

model.compile(run_eagerly=True)

如果这不起作用,您可以在模型编译后尝试强制它:

model.compile()
model.run_eagerly = True
于 2020-02-07T20:54:05.520 回答
0

此处说明的解决方案:https ://github.com/tensorflow/tensorflow/issues/26268 应该可以解决问题,还有一个完整的解释是什么导致了这种行为

于 2019-08-27T16:25:29.233 回答