首页 > 解决方案 > 非图像数据的卷积:检查目标时出错

问题描述

我正在尝试在非图像数据集上使用卷积自动编码器。该数据集有 5198 行和 8189 列(数据集链接 - http://upscfever.com/datasets/blognew.csv)。每行是一个特征向量,因此有 8189 个特征,我将其重塑为 (8189,1,1) 矩阵以进行卷积操作。

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

blog4_input_img = Input(shape=(8189,1,1))  # HERE

blog4_x = Conv2D(16, (3, 3), activation='relu', padding='same')(blog4_input_img)
blog4_x = MaxPooling2D((2, 2), padding='same')(blog4_x)
blog4_x = Conv2D(8, (3, 3), activation='relu', padding='same')(blog4_x)
blog4_x = MaxPooling2D((2, 2), padding='same')(blog4_x)
blog4_x = Conv2D(8, (3, 3), activation='relu', padding='same')(blog4_x)
blog4_encoded = MaxPooling2D((2, 2), padding='same')(blog4_x)

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

blog4_x = Conv2D(8, (3, 3), activation='relu', padding='same')(blog4_encoded)
blog4_x = UpSampling2D((2, 2))(blog4_x)
blog4_x = Conv2D(8, (3, 3), activation='relu', padding='same')(blog4_x)
blog4_x = UpSampling2D((2, 2))(blog4_x)
blog4_x = Conv2D(16, (3, 3), activation='relu')(blog4_x)
blog4_x = UpSampling2D((2, 2))(blog4_x)
blog4_decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(blog4_x)

blog4_autoencoder = Model(blog4_input_img, blog4_decoded)
blog4_autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

但是,当我尝试将卷积编码器应用于完整的数据集(numpy 数组)时,出现错误。

blog4_train = np.reshape(blog4_train, (blog4_train.shape[0],8189,1,1))
blog4_test = np.reshape(blog4_test, (blog4_test.shape[0],8189,1,1))


blog4_autoencoder.fit(blog4_train, blog4_train,
                epochs=50,
                batch_size=128,
                shuffle=True,
                validation_data=(blog4_test, blog4_test))

错误:

 ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-167-20c1a0f36946> in <module>()
      3                 batch_size=128,
      4                 shuffle=True,
----> 5                 validation_data=(blog4_test, blog4_test))
      6 

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, 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=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    787                 feed_output_shapes,
    788                 check_batch_axis=False,  # Don't enforce the batch size.
--> 789                 exception_prefix='target')
    790 
    791             # Generate sample-wise weight values given the `sample_weight` and

/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    136                             ': expected ' + names[i] + ' to have shape ' +
    137                             str(shape) + ' but got array with shape ' +
--> 138                             str(data_shape))
    139     return data
    140 

ValueError: Error when checking target: expected conv2d_171 to have shape (8188, 4, 1) but got array with shape (8189, 1, 1)

标签: tensorflowkeras

解决方案


Conv2D 需要一个 4D 张量,其形状为:(batch, channels, rows, cols)。您缺少数据集上的维度。


推荐阅读