Keras add_loss不适用于Encoder-Decoder模型上的y数据(y_train,y_test)

时间:2019-04-12 08:12:37

标签: keras autoencoder

在我进行自动编码器编码练习时,我在Keras模型中使用add_loss函数与在编译函数中使用loss ='mse之类的东西相比,发现了一些奇怪的活动。

在编译函数中使用损耗的同一模型通常需要y_train和y_test(如果我使用validation_data),但是如果我提供y数据(y_train / y_test),则add_loss(mse(inputs,outputs))会发出错误。

这是我为训练mnist编写的简单代码,从28 x 28变平为784暗淡。

示例代码的共同体如下:

encode_dim = 32
inputs = Input(shape=(784,))
encoded = Dense(128, activation='relu')(inputs)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(encode_dim, activation='relu')(encoded)

encoder = Model(inputs=inputs, outputs=encoded)
print(encoder.summary())

encoded_inputs = Input(shape=(encode_dim, ))

decoded = Dense(64, activation='relu')(encoded_inputs)
decoded = Dense(128, activation='relu')(decoded)
decoded = Dense(784, activation='sigmoid')(decoded)

decoder = Model(inputs=encoded_inputs, outputs=decoded)
print(decoder.summary())

outputs = decoder(encoder(inputs))
ae = Model(inputs, outputs)
print(ae.summary())
  1. 在编译函数中按名称提供损失如下所示:

    ae.compile(optimizer='adam', loss='mse')
    ae.fit(x_train, y_train, epochs=1, batch_size=256)
    

会工作, 2.但情况如下:

ae_loss = mse(inputs, outputs)
ae.add_loss(ae_loss )    
ae.compile(optimizer='adam')
ae.fit(x_train, y_train, epochs=1, batch_size=256)

将给出如下错误:

  

ValueError跟踪(最近的呼叫   最后)         1 deep_vaestyle_model2 = AutoEncoderTester(DeepModelVAEStyle())         2 deep_vaestyle_model2.train(x_train = x_train_flat,y_train = x_train_flat,x_test = x_test_flat,y_test = x_test_flat,   ----> 3个时代= 1,batch_size = 1024,详细= 1)         4 deep_vaestyle_model2.test(x_test = x_test_flat)

     

in train(self,x_train,y_train,   x_test,y_test,纪元,batch_size,冗长)        16 histogram_freq = 0,        17 write_graph = True,   ---> 18 write_grads =真,        19#batch_size =批处理大小,        20#write_images = True

     

d:\ igs_projects \ realtime_eeg_analyzer \ venv \ lib \ site-packages \ keras \ engine \ training.py   适合(自我,x,y,batch_size,时代,冗长,回调,   validate_split,validation_data,随机播放,class_weight,   sample_weight,initial_epoch,steps_per_epoch,validation_steps,   ** kwargs)       950 sample_weight = sample_weight,       951 class_weight = class_weight,   -> 952 batch_size =批量大小)       953#准备验证数据。       954 do_validation = False

     

d:\ igs_projects \ realtime_eeg_analyzer \ venv \ lib \ site-packages \ keras \ engine \ training.py   在_standardize_user_data(self,x,y,sample_weight,class_weight,   check_array_lengths,batch_size)       787 feed_output_shapes,       788 check_batch_axis = False,#不强制执行批量大小。   -> 789 exception_prefix ='target')       790       791#给定sample_weight

,生成按样本的权重值      

d:\ igs_projects \ realtime_eeg_analyzer \ venv \ lib \ site-packages \ keras \ engine \ training_utils.py   在standardize_input_data(数据,名称,形状,check_batch_axis,   exception_prefix)        61引发ValueError('检查模型时出错'+        62 exception_prefix +':'   ---> 63'预期没有数据,但得到:',数据)        64返回[]        如果数据为None,则为65:

     

ValueError :(“检查模型目标时出错:预期没有数据,但是   得到了:',数组([[[0.,0.,0.,...,0.,0.,0.],          [0.,0.,0.,...,0.,0.,0.],          [0.,0.,0.,...,0.,0.,0.],          ...,          [0.,0.,0.,...,0.,0.,0.],          [0.,0.,0.,...,0.,0.,0.],          [0.,0.,0.,...,0.,0.,0。]])))

但是如果我将y数据(y_train和y_test)设置为“无”,那么它将按预期运行。

我试图找出原因,使喀拉拉邦认为这两个是不同的,但我没有。有人可以解释吗?

0 个答案:

没有答案