keras 自动编码器“检查目标时出错" [英] keras autoencoder "Error when checking target"

查看:24
本文介绍了keras 自动编码器“检查目标时出错"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试改编 keras 网站上的 2d 卷积自动编码器示例:https://blog.keras.io/building-autoencoders-in-keras.html

i'm trying to adapt the 2d convolutional autoencoder example from the keras website: https://blog.keras.io/building-autoencoders-in-keras.html

对于我自己使用一维输入的情况:

to my own case where i use 1d inputs:

from keras.layers import Input, Dense, Conv1D, MaxPooling1D, UpSampling1D
from keras.models import Model
from keras import backend as K
import scipy as scipy
import numpy as np 

mat = scipy.io.loadmat('edata.mat')
emat = mat['edata']

input_img = Input(shape=(64,1))  # adapt this if using `channels_first` image data format

x = Conv1D(32, (9), activation='relu', padding='same')(input_img)
x = MaxPooling1D((4), padding='same')(x)
x = Conv1D(16, (9), activation='relu', padding='same')(x)
x = MaxPooling1D((4), padding='same')(x)
x = Conv1D(8, (9), activation='relu', padding='same')(x)
encoded = MaxPooling1D(4, padding='same')(x)

x = Conv1D(8, (9), activation='relu', padding='same')(encoded)
x = UpSampling1D((4))(x)
x = Conv1D(16, (9), activation='relu', padding='same')(x)
x = UpSampling1D((4))(x)
x = Conv1D(32, (9), activation='relu')(x)
x = UpSampling1D((4))(x)
decoded = Conv1D(1, (9), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

x_train = emat[:,0:80000]
x_train = np.reshape(x_train, (x_train.shape[1], 64, 1))
x_test = emat[:,80000:120000]
x_test = np.reshape(x_test, (x_test.shape[1], 64, 1))

from keras.callbacks import TensorBoard

autoencoder.fit(x_train, x_train,
                epochs=50,
                batch_size=128,
                shuffle=True,
                validation_data=(x_test, x_test),
                callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

但是,当我尝试运行 autoencoder.fit() 时收到此错误:

however, i receive this error when i try to run the autoencoder.fit():

ValueError:检查目标时出错:预期 conv1d_165 具有shape (None, 32, 1) 但得到了形状为 (80000, 64, 1) 的数组

ValueError: Error when checking target: expected conv1d_165 to have shape (None, 32, 1) but got array with shape (80000, 64, 1)

我知道我在设置图层时可能做错了什么,我只是将 maxpool 和 conv2d 大小更改为一维形式...我对 keras 或自动编码器的经验很少,任何人都知道我在做什么做错了吗?

i know i'm probably doing something wrong when i set up my layers, i just changed the maxpool and conv2d sizes to a 1d form...i have very little experience with keras or autoencoders, anyone see what i'm doing wrong?

谢谢

我在新的控制台上运行它时的错误:

the error when i run it on a fresh console:

ValueError:检查目标时出错:预期 conv1d_7 具有shape (None, 32, 1) 但得到了形状为 (80000, 64, 1) 的数组

ValueError: Error when checking target: expected conv1d_7 to have shape (None, 32, 1) but got array with shape (80000, 64, 1)

这是autoencoder.summary()

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64, 1)             0         
_________________________________________________________________
conv1d_1 (Conv1D)            (None, 64, 32)            320       
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 16, 32)            0         
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 16, 16)            4624      
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 4, 16)             0         
_________________________________________________________________
conv1d_3 (Conv1D)            (None, 4, 8)              1160      
_________________________________________________________________
max_pooling1d_3 (MaxPooling1 (None, 1, 8)              0         
_________________________________________________________________
conv1d_4 (Conv1D)            (None, 1, 8)              584       
_________________________________________________________________
up_sampling1d_1 (UpSampling1 (None, 4, 8)              0         
_________________________________________________________________
conv1d_5 (Conv1D)            (None, 4, 16)             1168      
_________________________________________________________________
up_sampling1d_2 (UpSampling1 (None, 16, 16)            0         
_________________________________________________________________
conv1d_6 (Conv1D)            (None, 8, 32)             4640      
_________________________________________________________________
up_sampling1d_3 (UpSampling1 (None, 32, 32)            0         
_________________________________________________________________
conv1d_7 (Conv1D)            (None, 32, 1)             289       
=================================================================
Total params: 12,785
Trainable params: 12,785
Non-trainable params: 0
_________________________________________________________________

推荐答案

由于自动编码器的输出应该重构输入,所以最低要求是它们的维度应该匹配,对吧?

Since the autoencoder output should reconstruct the input, a minimum requirement is that their dimensions should match, right?

查看您的 autoencoder.summary(),很容易确认情况并非如此:您的输入的形状为 (64,1),而最后一个卷积层 conv1d_7 的输出是 (32,1)(我们忽略第一维中的 None,因为它们指的是批量大小).

Looking at your autoencoder.summary(), it is easy to confirm that this is not the case: your input is of shape (64,1), while the output of your last convolutional layer conv1d_7 is (32,1) (we ignore the None in the first dimension, since they refer to the batch size).

让我们看看 Keras 博客中的 示例你链接到(它是一个 2D 自动编码器,但想法是一样的):

Let's have a look at the example in the Keras blog you link to (it is a 2D autoencoder, but the idea is the same):

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K

input_img = Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data format

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

这是本例中 autoencoder.summary() 的结果:

Here is the result of autoencoder.summary() in this case:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 28, 28, 1)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 28, 28, 16)        160       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 14, 14, 8)         1160      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 7, 7, 8)           0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 7, 7, 8)           584       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 4, 4, 8)           0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 4, 4, 8)           584       
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 8, 8, 8)           0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 8, 8, 8)           584       
_________________________________________________________________
up_sampling2d_2 (UpSampling2 (None, 16, 16, 8)         0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 14, 14, 16)        1168      
_________________________________________________________________
up_sampling2d_3 (UpSampling2 (None, 28, 28, 16)        0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 28, 28, 1)         145       
=================================================================
Total params: 4,385
Trainable params: 4,385
Non-trainable params: 0

很容易确认,这里输入和输出(最后一个卷积层conv2d_7)的维度确实都是(28, 28, 1).

It is easy to confirm that here the dimensions of the input and the output (last convolutional layer conv2d_7) are indeed both (28, 28, 1).

因此,summary() 方法是您构建自动编码器时的朋友;您应该对参数进行试验,直到您确定产生与输入具有相同维度的输出.我通过将最后一个 UpSampling1D 层的 size 参数从 4 更改为 8 来设法使用您的自动编码器做到这一点:

So, the summary() method is your friend when building autoencoders; you should experiment with the parameters until you are sure that you produce an output of the same dimensionality as your input. I managed to do so with your autoencoder simply by changing the size argument of the last UpSampling1D layer from 4 to 8:

input_img = Input(shape=(64,1))  

x = Conv1D(32, (9), activation='relu', padding='same')(input_img)
x = MaxPooling1D((4), padding='same')(x)
x = Conv1D(16, (9), activation='relu', padding='same')(x)
x = MaxPooling1D((4), padding='same')(x)
x = Conv1D(8, (9), activation='relu', padding='same')(x)
encoded = MaxPooling1D(4, padding='same')(x)

x = Conv1D(8, (9), activation='relu', padding='same')(encoded)
x = UpSampling1D((4))(x)
x = Conv1D(16, (9), activation='relu', padding='same')(x)
x = UpSampling1D((4))(x) 
x = Conv1D(32, (9), activation='relu')(x)
x = UpSampling1D((8))(x)              ##   <-- change here (was 4)
decoded = Conv1D(1, (9), activation='sigmoid', padding='same')(x)  

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

在这种情况下,autoencoder.summary() 变为:

In which case, the autoencoder.summary() becomes:

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64, 1)             0         
_________________________________________________________________
conv1d_1 (Conv1D)            (None, 64, 32)            320       
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 16, 32)            0         
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 16, 16)            4624      
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 4, 16)             0         
_________________________________________________________________
conv1d_3 (Conv1D)            (None, 4, 8)              1160      
_________________________________________________________________
max_pooling1d_3 (MaxPooling1 (None, 1, 8)              0         
_________________________________________________________________
conv1d_4 (Conv1D)            (None, 1, 8)              584       
_________________________________________________________________
up_sampling1d_1 (UpSampling1 (None, 4, 8)              0         
_________________________________________________________________
conv1d_5 (Conv1D)            (None, 4, 16)             1168      
_________________________________________________________________
up_sampling1d_2 (UpSampling1 (None, 16, 16)            0         
_________________________________________________________________
conv1d_6 (Conv1D)            (None, 8, 32)             4640      
_________________________________________________________________
up_sampling1d_3 (UpSampling1 (None, 64, 32)            0         
_________________________________________________________________
conv1d_7 (Conv1D)            (None, 64, 1)             289       
=================================================================
Total params: 12,785
Trainable params: 12,785
Non-trainable params: 0

输入和输出的维度匹配,应该是......

with the dimensionality of your input and output matched, as it should be...

这篇关于keras 自动编码器“检查目标时出错"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆