【问题标题】:1D CNN input shape and the training data shape一维 CNN 输入形状和训练数据形状
【发布时间】:2021-03-19 18:42:43
【问题描述】:

我在尝试将以下数据输入我的网络时遇到错误。我对重塑训练数据和网络输入有疑问。我得到的错误是:

Error when checking target: expected conv1d_92 to have shape (4, 1) but got array with shape (1, 784)

代码如下:

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 17 20:57:51 2021

@author: morte 
"""
import keras
from keras import layers
from keras.datasets import mnist
import numpy as np
#(x_train, _), (x_test, _) = mnist.load_data()
#x_train = x_train.astype('float32') / 255.
#x_test = x_test.astype('float32') / 255.
#x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
#x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255. 
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train),1,28*28)) #
x_test = np.reshape(x_test, (len(x_test),1,28*28))  #

input_img = keras.Input(shape=(x_train.shape[1:]))

x = layers.Conv1D(16,(3), activation='relu', padding='same')(input_img)
x = layers.MaxPooling1D(2, padding='same')(x)
x = layers.Conv1D(8,(3), activation='relu', padding='same')(x)
x = layers.MaxPooling1D(2, padding='same')(x)
x = layers.Conv1D(8,(3), activation='relu', padding='same')(x)
encoded = layers.MaxPooling1D(2, padding='same')(x)

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

x = layers.Conv1D(8,(3), activation='relu', padding='same')(encoded)
x = layers.UpSampling1D(2)(x)
x = layers.Conv1D(8,(3), activation='relu', padding='same')(x)
x = layers.UpSampling1D(2)(x)
x = layers.Conv1D(16,(3), activation='relu')(x)
x = layers.UpSampling1D(2)(x)
decoded = layers.Conv1D(1, (3), activation='sigmoid', padding='same')(x)

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

from keras.datasets import mnist
import numpy as np






from keras.callbacks import TensorBoard

autoencoder.fit(x_train, x_train,
            epochs=2,
            batch_size=128,
            shuffle=True,
            validation_data=(x_test, x_test),
            )

decoded_imgs = autoencoder.predict(x_test)


import matplotlib.pyplot as plt
n = 10
plt.figure(figsize=(20, 4))
for i in range(1, n + 1):
# Display original
ax = plt.subplot(2, n, i)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# Display reconstruction
ax = plt.subplot(2, n, i + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()

【问题讨论】:

    标签: keras conv-neural-network


    【解决方案1】:

    由于它是一个自动编码器网络,编码器输入必须匹配解码器输出。 您正在给出一个 (1,785) 形状的输入数组并输出一个 (4,1) 数组。 有关更多详细信息,您可以添加以下行:autoencoder.summary()(例如在行后autoencoder = keras.Model(input_img, decoded)) 这将为您提供有关每一层形状的信息。 例如,一种方法是在解码器的末尾添加一个密集层。

    【讨论】:

    • 非常感谢您的回复!!很有帮助!!
    猜你喜欢
    • 2020-10-20
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2018-02-19
    相关资源
    最近更新 更多