对非图像数据进行卷积:检查目标时出错

时间:2018-12-21 09:18:47

标签: tensorflow keras

我正在尝试对非图像数据集使用卷积自动编码器。数据集有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)

1 个答案:

答案 0 :(得分:0)

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