ValueError: Dimensions must be equal (keras)

ValueError: Dimensions must be equal (keras)

我正在尝试训练自动编码器,但在重塑我的 X_train 以使其适合我的模型 model() 时遇到问题。

from tensorflow import keras
from keras.layers import *
from keras.models import Model
from keras.models import Sequential 
from keras.optimizers import Adam

from keras.optimizers import RMSprop

from keras.utils import plot_model

X_train = np.array(X_train, dtype=np.float)
X_test =np.array(X_train, dtype=np.float)

X_train = X_train.reshape(len(X_train), 100,1)
X_test = X_test.reshape(len(X_test), 100,1)

#inputs = Input(shape=(230, 1,100))
epoch = 100
batch = 128

def model():
    m = Sequential()
    # ##m.add(Reshape((,)))
    m.add(Flatten())
    m.add(Dense(512, activation='relu'))
    m.add(Dense(128, activation = 'relu'))
    m.add(Dense(2, activation = 'linear'))
    m.add(Dense(128, activation = 'relu'))
    m.add(Dense(512, activation = 'relu'))
    m.add(Dense(784, activation = 'sigmoid'))
    
    m.compile(loss='mean_squared_error', optimizer = 'rmsprop', metrics = ['accuracy'])
    # Fit data to model m
    m.fit(X_train, X_train, batch_size = batch, epochs = epoch)
    m.summary()
    
    #score = m.evaluate(X_test, Y_test, verbose = 0)
    #print('Test loss:' score[0])
    #print('Test accuracy:', score[1])
    #m.summary()
    
    
mod = model()

我的数据维度如下:

X_train = (523, 100,1) X_test = (523, 100,1)

要解决您的问题,请更改以下内容:

X_train = X_train.reshape((-1, 100))
X_test = X_test.reshape((-1, 100))

删除 Flatten 层,最后一层使用 100 神经元,如评论中所述。