gpt4 book ai didi

python - Keras - 检查目标时出错

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:48 25 4
gpt4 key购买 nike

给定以下代码:

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) 的数组

我错过了什么?

最佳答案

您的最后一层 dense_5 的大小为 5,而您的目标层的大小为 4。

为了工作,每个目标的大小必须是您要预测的类的数量。请记住,他们必须出现在一个热门场景中。您可以使用 to_categorical来自 Keras。

关于python - Keras - 检查目标时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46177721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com