Dan*_*ele 5 python deep-learning keras tensorflow 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
Run Code Online (Sandbox Code Playgroud)
我尝试重置内核,也尝试过 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))
Run Code Online (Sandbox Code Playgroud)
这是我的自定义损失函数:
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
Run Code Online (Sandbox Code Playgroud)
按照 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>]
Run Code Online (Sandbox Code Playgroud)
如果您需要更多详细信息,请告诉我。
你可以尝试用这个代替:
vae.fit(x_train, x_train
shuffle=True,
epochs=1,
batch_size=batch_size,
validation_data=(x_test,x_test))
Run Code Online (Sandbox Code Playgroud)
Keras 期望您未提供的目标输出(例如y_train在监督学习中、在自动编码器中)。x_train来自文档:
您可以传递现有损失函数的名称,也可以传递 TensorFlow/Theano 符号函数,该函数为每个数据点返回一个标量并采用以下两个参数:
Run Code Online (Sandbox Code Playgroud)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 方面的专家,但这绝对是错误的原因。请参阅本教程以了解如何建立您的损失。
另一种解决方法是构建一个具有两个输出的模型并创建两个损失函数,每个输出一个,例如
y_true: True labels. TensorFlow/Theano tensor.
y_pred: Predictions. TensorFlow/Theano tensor of the same shape as y_true.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10561 次 |
| 最近记录: |