简体   繁体   中英

What am I doing wrong in implementing a Keras model?

I am new to Python. I am getting the following error while trying to implement keras model, what am I doing wrong?

I have 9 inputs, 19 outputs, 3 hidden layers in which there are 18 neurons in each layer.

Thank you.

ValueError: Input 0 of layer "sequential_7" is incompatible with the layer: expected shape=(None, 9), found shape=(10, 4)

X, y = scaled_df[[ "Part's Z-Height (mm)","Part's Weight (N)","Part's Volume (cm^3)","Part's Surface Area (cm^2)","Part's Orientation (Support's height) (mm)","Part's Orientation (Support's volume) (cm^3)","Layer Height (mm)","Printing/Scanning Speed (mm/s)","Infill Density (%)"]], scaled_df [["Climate change (kg CO2 eq.)","Climate change, incl biogenic carbon (kg CO2 eq.)","Fine Particulate Matter Formation (kg PM2.5 eq.)","Fossil depletion (kg oil eq.)","Freshwater Consumption (m^3)","Freshwater ecotoxicity (kg 1,4-DB eq.)","Freshwater Eutrophication (kg P eq.)","Human toxicity, cancer (kg 1,4-DB eq.)","Human toxicity, non-cancer (kg 1,4-DB eq.)","Ionizing Radiation (Bq. C-60 eq. to air)","Land use (Annual crop eq. yr)","Marine ecotoxicity (kg 1,4-DB eq.)","Marine Eutrophication (kg N eq.)","Metal depletion (kg Cu eq.)","Photochemical Ozone Formation, Ecosystem (kg NOx eq.)","Photochemical Ozone Formation, Human Health (kg NOx eq.)","Stratospheric Ozone Depletion (kg CFC-11 eq.)","Terrestrial Acidification (kg SO2 eq.)","Terrestrial ecotoxicity (kg 1,4-DB eq.)"]]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# define the keras model
model = Sequential()
model.add(Dense(18, input_shape=(9,), activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(18, activation='relu'))


# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])


# fit the keras model on the dataset
model.fit(X, y, epochs=150, batch_size=10)

!pip install livelossplot

from livelossplot import PlotLossesKeras
model.fit(X_train, y_train,
          epochs=10,
          validation_data=(X_test, y_test),
          callbacks=[PlotLossesKeras()],
          verbose=0)

Like @Djinn was implying, your X_train , X_test and model input_shape must be the same, and the last layer of your model must have the same amount neurons as your y_train and y_test datasets.

The error indicates that one input batch of samples has 10 samples with 4 inputs each; this does not match your input shape of 9. Check your data preprocessing to ensure inputs are the same length.

Therefore, with 9 inputs, input_shape should be (9,) (which it already is), and with 19 outputs, your last dense layer should contain 19 neurons. Additionally, it looks like you may be fitting the model twice. It would make sense to comment out the first call to model.fit() and let the second run. Hope this helps!

For more information on inputs, look here: Keras input explanation: input_shape, units, batch_size, dim, etc .

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