可变自动编码器不兼容的形状:[50]对[35]

时间:2018-07-12 04:49:20

标签: keras autoencoder

我已经阅读了另一篇类似的文章,但并不完全确定该解决方案是否适合我的问题。我对keras并不陌生,并使用教程尝试不同的数据集。在两个维度上都可以很好地工作,但是当我尝试使用DataFrame(3035,881)时,我发现下面的错误代码有麻烦。有任何提示或想法吗?是否还需要修复我的自负功能?即使是简单的同意也可以帮助

vae.fit(data, data, batch_size=m, nb_epoch=n_epoch)
__main__:49: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
Epoch 1/10
3000/3035 [============================>.] - ETA: 0s - loss: 5639.7026Traceback (most recent call last):

  File "<ipython-input-53-5cb9b0d29f84>", line 49, in <module>
    vae.fit(data, data, batch_size=m, nb_epoch=n_epoch)

  File "C:\Users\gonza\Anaconda2\envs\py35\lib\site-packages\keras\engine\training.py", line 1042, in fit
    validation_steps=validation_steps)

  File "C:\Users\gonza\Anaconda2\envs\py35\lib\site-packages\keras\engine\training_arrays.py", line 199, in fit_loop
    outs = f(ins_batch)

  File "C:\Users\gonza\Anaconda2\envs\py35\lib\site-packages\keras\backend\tensorflow_backend.py", line 2661, in __call__
    return self._call(inputs)

  File "C:\Users\gonza\Anaconda2\envs\py35\lib\site-packages\keras\backend\tensorflow_backend.py", line 2631, in _call
    fetched = self._callable_fn(*array_vals)

  File "C:\Users\gonza\Anaconda2\envs\py35\lib\site-packages\tensorflow\python\client\session.py", line 1454, in __call__
    self._session._session, self._handle, args, status, None)

  File "C:\Users\gonza\Anaconda2\envs\py35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 519, in __exit__
    c_api.TF_GetCode(self.status.status))

InvalidArgumentError: Incompatible shapes: [50] vs. [35]
     [[Node: training_9/Adam/gradients/loss_16/dense_93_loss/mul_1_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _class=["loc:@training_9/Adam/gradients/loss_16/dense_93_loss/mul_1_grad/Reshape"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](training_9/Adam/gradients/loss_16/dense_93_loss/mul_1_grad/Shape, training_9/Adam/gradients/loss_16/dense_93_loss/mul_1_grad/Shape_1)]]

代码:

import pandas as pd
from tensorflow.examples.tutorials.mnist import input_data
from keras.layers import Input, Dense, Lambda
from keras.models import Model
from keras.objectives import binary_crossentropy
from keras.callbacks import LearningRateScheduler

import numpy as np
import matplotlib.pyplot as plt
import keras.backend as K
import tensorflow as tf

data=#one dimensional input data of 880 features and 3035 samples
m = 50
n_z = 2
n_epoch = 10


# Q(z|X) -- encoder
inputs = Input(shape=(880,))
h_q = Dense(512, activation='relu')(inputs)
mu = Dense(n_z, activation='linear')(h_q)
log_sigma = Dense(n_z, activation='linear')(h_q)

def sample_z(args):
    mu, log_sigma = args
    eps = K.random_normal(shape=(m, n_z), mean=0., stddev=1.)
    return mu + K.exp(log_sigma / 2) * eps


# Sample z ~ Q(z|X)
z = Lambda(sample_z)([mu, log_sigma])

# P(X|z) -- decoder
decoder_hidden = Dense(512, activation='relu')
decoder_out = Dense(880, activation='sigmoid')

h_p = decoder_hidden(z)
outputs = decoder_out(h_p)

# Overall VAE model, for reconstruction and training
vae = Model(inputs, outputs)

# Encoder model, to encode input into latent variable
# We use the mean as the output as it is the center point, the representative of the gaussian
encoder = Model(inputs, mu)

# Generator model, generate new data given latent variable z
d_in = Input(shape=(n_z,))
d_h = decoder_hidden(d_in)
d_out = decoder_out(d_h)
decoder = Model(d_in, d_out)


def vae_loss(y_true, y_pred):
    """ Calculate loss = reconstruction loss + KL loss for each data in minibatch """
    # E[log P(X|z)]
    recon = K.sum(K.binary_crossentropy(y_pred, y_true), axis=1)
    # D_KL(Q(z|X) || P(z|X)); calculate in closed form as both dist. are Gaussian
    kl = 0.5 * K.sum(K.exp(log_sigma) + K.square(mu) - 1. - log_sigma, axis=1)

    return recon + kl


vae.compile(optimizer='adam', loss=vae_loss)
vae.fit(data, data, batch_size=m, nb_epoch=n_epoch)

0 个答案:

没有答案