给出以下代码:
import matplotlib.pyplot as plt
import numpy
from keras import callbacks
from keras import optimizers
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.callbacks import ModelCheckpoint
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.utils import shuffle
# Early stopping - Stop training before overfitting
early_stop = callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=3, verbose=1, mode='auto')
# fix random seed for reproducibility
seed = 42
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("./data/poc.csv",skiprows=1, delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:, 0:14]
Y = dataset[:, 14:18]
# # Standardize features by removing the mean and scaling to unit variance
scaler = StandardScaler()
X = scaler.fit_transform(X)
#ADAM Optimizer with learning rate decay
opt = optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0001)
## Create our model
model = Sequential()
model.add(Dense(200, input_dim=14, kernel_initializer='uniform', activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(60, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(30, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(5, activation='sigmoid'))
model.summary()
# Compile the model using binary crossentropy since we are predicting 0/1
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
# checkpoint
filepath="./checkpoints/weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
# Fit the model
history = model.fit(X, Y, validation_split=0.33, epochs=10000, batch_size=10, verbose=0, callbacks=[early_stop,checkpoint])
和数据一样:
17.6,1,1,0,1,0,0,0,0,0,0,3.9,9.2,20.29,0,1,0,0,0
12.9,1,0,1,0,0,0,0,0,0,0,4.1,13.5,0.08,0,0,0,1,0
3.2,1,0,1,0,0,0,0,0,0,0,4.122031746,13.8,0.01,0,0,0,0,0
...
我得到以下输出/错误:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 200) 3000
_________________________________________________________________
dropout_1 (Dropout) (None, 200) 0
_________________________________________________________________
dense_2 (Dense) (None, 100) 20100
_________________________________________________________________
dropout_2 (Dropout) (None, 100) 0
_________________________________________________________________
dense_3 (Dense) (None, 60) 6060
_________________________________________________________________
dropout_3 (Dropout) (None, 60) 0
_________________________________________________________________
dense_4 (Dense) (None, 30) 1830
_________________________________________________________________
dropout_4 (Dropout) (None, 30) 0
_________________________________________________________________
dense_5 (Dense) (None, 5) 155
=================================================================
Total params: 31,145
Trainable params: 31,145
Non-trainable params: 0
_________________________________________________________________
检查目标时出错:期望dense_5有形状(None,1)但是有形状的数组(716,4)
我错过了什么?
答案 0 :(得分:2)
您的上一个图层dense_5
的大小为5,而您的目标大小为4。
为了工作,每个目标的大小必须是您要预测的类的数量。请记住,他们必须在一个热门的enconding中表示。您可以使用Keras的to_categorical。