0

compile在删除步骤的损失函数(将其设置为loss=None)并添加一个以通过该add_loss方法添加另一个损失函数后,我的代码遇到了问题。我可以打电话fit,它训练了一个时代,但后来我得到了这个错误:

ValueError: operands could not be broadcast together with shapes (128,) (117,) (128,) 

我的批量大小是 128。它看起来117在某种程度上取决于我正在使用的示例数量。当我改变示例的数量时,我会从117. 它们都是我的示例数量以及我的批量大小。我不知道如何解决这个问题。我tf.data.TFRecordDataset用作输入。

我有以下简化模型:

class MyModel(Model):

  def __init__(self):
    super(MyModel, self).__init__()

    encoder_input = layers.Input(shape=INPUT_SHAPE, name='encoder_input')
    x = encoder_input
    x = layers.Conv2D(64, (3, 3), activation='relu', padding='same', strides=2)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Conv2D(32, (3, 3), activation='relu', padding='same', strides=2)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Flatten()(x)

    encoded = layers.Dense(LATENT_DIM, name='encoded')(x)

    self.encoder = Model(encoder_input, outputs=[encoded])

    self.decoder = tf.keras.Sequential([
      layers.Input(shape=LATENT_DIM),
      layers.Dense(32 * 32 * 32),
      layers.Reshape((32, 32, 32)),
      layers.Conv2DTranspose(32, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2DTranspose(64, kernel_size=3, strides=2, activation='relu', padding='same'),
      layers.Conv2D(3, kernel_size=(3, 3), activation='sigmoid', padding='same')])

  def call(self, x):
    encoded = self.encoder(x)

    decoded = self.decoder(encoded)

    # Loss function. Has to be here because I intend to add another, more layer-interdependent, loss function.
    r_loss = tf.math.reduce_sum(tf.math.square(x - decoded), axis=[1, 2, 3])
    self.add_loss(r_loss)

    return decoded


def read_tfrecord(example):
  example = tf.io.parse_single_example(example, CELEB_A_FORMAT)
  image = decode_image(example['image'])

  return image, image

def load_dataset(filenames, func):
  dataset = tf.data.TFRecordDataset(
    filenames
  )

  dataset = dataset.map(partial(func), num_parallel_calls=tf.data.AUTOTUNE)

  return dataset

def train_autoencoder():
  filenames_train = glob.glob(TRAIN_PATH)
  train_dataset_x_x = load_dataset(filenames_train[:4], func=read_tfrecord)

  autoencoder = Autoencoder()

  # The loss function used to be defined here and everything worked fine before.
  def r_loss(y_true, y_pred):
    return tf.math.reduce_sum(tf.math.square(y_true - y_pred), axis=[1, 2, 3])

  optimizer = tf.keras.optimizers.Adam(1e-4)

  autoencoder.compile(optimizer=optimizer, loss=None)

  autoencoder.fit(train_dataset_x_x.batch(AUTOENCODER_BATCH_SIZE),
                  epochs=AUTOENCODER_NUM_EPOCHS,
                  shuffle=True)
4

2 回答 2

1

如果您只想摆脱错误并且不关心数据集的最后一个“剩余”批次,您可以在drop_remainder=True内部使用关键字参数train_dataset_x_x.batch(),这样您的所有批次都将具有相同的大小。

仅供参考,通常更好的做法是在函数调用之外对数据集进行批处理fit

data = data.batch(32)
model.fit(data)
于 2021-10-22T16:52:47.010 回答
-1

损失函数不能在调用方法中设置。call 方法旨在进行前向传递而不是计算损失。

您需要在编译方法中或之后添加损失函数

于 2021-10-22T17:09:24.617 回答