是否热修复Tensorflow模型无法通过.fit()在Eager模式下运行?

rya*_*651 5 python machine-learning keras tensorflow eager-execution

我正在尝试在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
Run Code Online (Sandbox Code Playgroud)

参数设定

batch_size = 50
epochs = 150
num_classes = 7
Run Code Online (Sandbox Code Playgroud)

检查Eager的版本和版本

print(tf.executing_eagerly())
print(tf.__version__)
>>True
>>2.0.0-alpha0
Run Code Online (Sandbox Code Playgroud)

运行模型

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)
Run Code Online (Sandbox Code Playgroud)

这导致错误 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())def call()方法中的定位打印False 。


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

Bre*_*ley 6

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

model.compile(run_eagerly=True)
Run Code Online (Sandbox Code Playgroud)

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

model.compile()
model.run_eagerly = True
Run Code Online (Sandbox Code Playgroud)


Alm*_*vid -1

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