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

查看:63
本文介绍了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具有 形状(无,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大小更改为1d形式...我对keras或autoencoders的经验很少,任何人都知道我在做什么做错了吗?

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具有 形状(无,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()变为:

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天全站免登陆