简体   繁体   中英

ValueError: Dimensions must be equal (keras)

I'm trying to train an autoencoder but have problems in reshaping my X_train to fit it to my model 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()

The of dimension of my data is the following:

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

To fix your issue, change the following:

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

Delete the Flatten layer and use 100 neurons for the last layer as stated in the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM