Keras:使用model.fit时修复“ IndexError:列表索引超出范围”错误

时间:2019-10-05 15:57:12

标签: python tensorflow keras deep-learning tf.keras

我正在尝试构建具有几个与cifar-10一起使用的Conv2d层的变体自动编码器。 看来还好,但是当我进行培训时,出现此错误:

Train on 50000 samples, validate on 10000 samples
  100/50000 [..............................] - ETA: 2:19

---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

<ipython-input-8-a9198aa155a7> in <module>()
      3         epochs=1,
      4         batch_size=batch_size,
----> 5         validation_data=(x_test, None))

20 frames

/tensorflow-2.0.0-rc2/python3.6/tensorflow_core/python/keras/engine/training_eager.py in _model_loss(model, inputs, targets, output_loss_metrics, sample_weights, training)
    164 
    165         if hasattr(loss_fn, 'reduction'):
--> 166           per_sample_losses = loss_fn.call(targets[i], outs[i])
    167           weighted_losses = losses_utils.compute_weighted_loss(
    168               per_sample_losses,

IndexError: list index out of range

我尝试重置内核,也尝试使用tensorflow 2.0和1.14.0,但没有任何变化。 我是keras和tf的新手,所以我可能犯了一些错误。

这是我的VAE的体系结构:

(x_train, _), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32') / 255.
x_train = x_train.reshape((x_train.shape[0],) + original_img_size)
x_test = x_test.astype('float32') / 255.
x_test = x_test.reshape((x_test.shape[0],) + original_img_size)

latent_dim = 128
kernel_size = (4,4)
original_img_size = (32,32,3)

#Encoder
x_in = Input(shape=original_img_size)
x = x_in
x = Conv2D(128, kernel_size=kernel_size, strides=2, padding='SAME', input_shape=original_img_size)(x)
x = BatchNormalization()(x)
x = layers.ReLU()(x)
x = Conv2D(256, kernel_size=kernel_size, strides=2, padding='SAME')(x)
x = BatchNormalization()(x)
x = layers.ReLU()(x)
x = Conv2D(512, kernel_size=kernel_size, strides=2, padding='SAME')(x)
x = BatchNormalization()(x)
x = layers.ReLU()(x)
x = Conv2D(1024, kernel_size=kernel_size, strides=2, padding='SAME')(x)
x = BatchNormalization()(x)
x = layers.ReLU()(x)

flat = Flatten()(x)
hidden = Dense(128, activation='relu')(flat)

#mean and variance
z_mean = hidden
z_log_var = hidden

#Decoder
decoder_input = Input(shape=(latent_dim,))

decoder_fc3 = Dense(8*8*1024) (decoder_input)
decoder_fc3 = BatchNormalization()(decoder_fc3)
decoder_fc3 = Activation('relu')(decoder_fc3)

decoder_reshaped = layers.Reshape((8,8,1024))(decoder_fc3)

decoder_ConvT1 = layers.Conv2DTranspose(512, kernel_size=(4,4), strides=(2,2), padding='SAME', input_shape=(8,8,1024))(decoder_reshaped)
decoder_ConvT1 = BatchNormalization()(decoder_ConvT1)
decoder_ConvT1 = Activation('relu')(decoder_ConvT1)

decoder_ConvT2 = layers.Conv2DTranspose(256, kernel_size=(4,4), strides=(2,2), padding='SAME')(decoder_ConvT1)
decoder_ConvT2 = BatchNormalization()(decoder_ConvT2)
decoder_ConvT2 = Activation('relu')(decoder_ConvT2)

decoder_ConvT3 = layers.Conv2DTranspose(3,kernel_size=(4,4), strides=(1,1), padding='SAME')(decoder_ConvT2)

y = decoder_ConvT3

decoder = Model(decoder_input, y)

x_out = decoder(encoder(x_in))

vae = Model(x_in, x_out)
vae.compile(optimizer='adam', loss=vae_loss) #custom loss 
vae.fit(x_train,
        shuffle=True,
        epochs=1,
        batch_size=batch_size,
        validation_data=(x_test, None))

这是我的自定义损失函数:

def vae_loss(x, x_decoded_mean):
  xent_loss = losses.binary_crossentropy(x, x_decoded_mean)
  kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
  return xent_loss + kl_loss

根据qmeeus的建议,我尝试添加目标输出,但是现在出现此错误:

Train on 50000 samples, validate on 10000 samples
  100/50000 [..............................] - ETA: 12:33

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

/tensorflow-2.0.0-rc2/python3.6/tensorflow_core/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     60                                                op_name, inputs, attrs,
---> 61                                                num_outputs)
     62   except core._NotOkStatusException as e:

TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: dense/Identity:0


During handling of the above exception, another exception occurred:

_SymbolicException                        Traceback (most recent call last)

11 frames

/tensorflow-2.0.0-rc2/python3.6/tensorflow_core/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     73       raise core._SymbolicException(
     74           "Inputs to eager execution function cannot be Keras symbolic "
---> 75           "tensors, but found {}".format(keras_symbolic_tensors))
     76     raise e
     77   # pylint: enable=protected-access

_SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'dense/Identity:0' shape=(None, 128) dtype=float32>]

如果您需要更多详细信息,请告诉我。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法吗?

vae.fit(x_train, x_train
        shuffle=True,
        epochs=1,
        batch_size=batch_size,
        validation_data=(x_test,x_test))

Keras期望提供您未提供的目标输出(例如,在监督学习中为y_train,在自动编码器中为x_train)。来自文档:

  

您可以传递现有损失函数的名称,也可以传递TensorFlow / Theano符号函数,该函数返回每个数据点的标量并采用以下两个参数:

y_true: True labels. TensorFlow/Theano tensor.
y_pred: Predictions. TensorFlow/Theano tensor of the same shape as y_true.
     

实际的优化目标是所有数据点上输出数组的均值。

我通常的做法是简单地提供与fit方法的输入相同的目标,如上面的代码所示...

[编辑]: 该错误来自您对kld的定义,该定义使用了tf.keras.backend中的方法。我不是tensorflow 2的专家,但这绝对是错误的原因。请参阅this tutorial以了解如何增加损失。

另一种解决方法是建立一个具有两个输出的模型并创建两个损失函数,每个输出一个,例如

model = Model(x_in, [hidden, y])
model.compile(loss=[custom_kld, binary_crossentropy], optimizer=optimizer)

答案 1 :(得分:0)

我有一个类似的错误,但使用的是普通监督模型(不是AE)。这不是您的问题,但可能与其他人相同,但有相同的错误:请确保您的validation_data是一个tupple。