gpt4 book ai didi

python - Keras:修复使用 model.fit 时的 "IndexError: list index out of range"错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:19:32 28 4
gpt4 key购买 nike

我正在尝试构建一个具有多个 Conv2d 层的变分自动编码器,可与 cifar-10 配合使用。看起来没问题,但是当我运行训练时,我收到此错误:

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>]

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

最佳答案

我也有类似的错误,但使用的是正常的监督模型(不是 AE)。这可能不是您的情况的问题,但可能与具有相同错误的其他人相关:确保您的validation_data是一个元组。

关于python - Keras:修复使用 model.fit 时的 "IndexError: list index out of range"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58249968/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com