Keras / Tensorflow中的add_loss的两种不同方法不能一起使用吗?

时间:2019-10-09 17:24:42

标签: tensorflow keras autoencoder loss

我编写了以下代码,通过Autoencoder进行了简单的实验,我只想使用两个损失,第一个损失是输入和输出的传统MSE损失,这些损失是根据AE的潜矢量重建的,第二个损失是编码器和解码器中对称层的两个输出之间的MSE损失,也就是说,如果AE有5层,我想在第二层和第四层之间加上MSE损失,因为它们是对称的。代码在这里:

from time import time
import numpy as np
import random
from keras.models import Model
import keras.backend as K
from keras.engine.topology import Layer, InputSpec
from keras.layers import Dense, Input, GaussianNoise, Layer, Activation
from keras.models import Model
from keras.optimizers import SGD, Adam
from keras.utils.vis_utils import plot_model
from keras.callbacks import EarlyStopping

#build vae model

input_place = Input(shape=(128,))

e_layer1 = Dense(64,activation='relu')(input_place)
e_layer2 = Dense(32,activation='relu')(e_layer1)
hidden = Dense(16,activation='relu')(e_layer2)

d_layer1 = Dense(32,activation='relu')(hidden)
d_layer2 = Dense(64,activation='relu')(d_layer1)

output_place = Dense(128,activation='sigmoid')(d_layer2)

model = Model(inputs=input_place,outputs=output_place)

loss = K.mean(K.square(d_layer1 - e_layer2),axis = -1)

model.add_loss(loss)

model.compile(optimizer = 'adam',
              loss=['mse'],
              metrics=['accuracy'])

input_data = np.random.randn(400,128)

model.fit(input_data,
          input_data,
          batch_size = 32,
          epochs=5)

但是当我运行这段代码时,它会发生关于

的错误
Epoch 1/5
 32/400 [=>............................] - ETA: 12s - loss: 1.6429 - acc: 0.0000e+00Traceback (most recent call last):

  File "<ipython-input-49-eac3a65824ec>", line 1, in <module>
    runfile('/Users/jishilun/Desktop/keras_loss_test.py', wdir='/Users/jishilun/Desktop')

  File "/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/jishilun/Desktop/keras_loss_test.py", line 49, in <module>
    epochs=5)

  File "/anaconda3/lib/python3.7/site-packages/keras/engine/training.py", line 1039, in fit
    validation_steps=validation_steps)

  File "/anaconda3/lib/python3.7/site-packages/keras/engine/training_arrays.py", line 204, in fit_loop
    callbacks.on_batch_end(batch_index, batch_logs)

  File "/anaconda3/lib/python3.7/site-packages/keras/callbacks.py", line 115, in on_batch_end
    callback.on_batch_end(batch, logs)

  File "/anaconda3/lib/python3.7/site-packages/keras/callbacks.py", line 236, in on_batch_end
    self.totals[k] += v * batch_size

ValueError: operands could not be broadcast together with shapes (32,) (16,) (32,) 

如果我删除了add_loss,代码可以运行,所以我认为Keras / Tensorflow中的两种add_loss方法不能简单地一起使用,或者可能会有一些变化(也许问题出在迷你批处理中) ?) 请帮我!任何意见将受到欢迎! 非常感谢你!

1 个答案:

答案 0 :(得分:1)

问题不是来自add_loss(),而是您的batch_size。您的输入数据为(400,128),但是batch_size为32。请尝试将其更改为400的分解因数,例如40或20,它将可以使用。