【发布时间】:2020-05-01 03:51:50
【问题描述】:
我正在解决一个多类分类问题。数据集如下所示:
|---------------------|------------------|----------------------|------------------|
| feature 1 | feature 3 | feature 4 | feature 2 |
|---------------------|------------------|------------------------------------------
| 1.302 | 102.987 | 1.298 | 99.8 |
|---------------------|------------------|----------------------|------------------|
|---------------------|------------------|----------------------|------------------|
| 1.318 | 102.587 | 1.998 | 199.8 |
|---------------------|------------------|----------------------|------------------|
这 4 个特征是浮点数,我的目标变量类是 1,2 或 3。当我构建跟随模型并训练时,它需要很长时间才能收敛(24 小时并且仍在运行)
我使用了如下的 keras 模型:
def create_model(optimizer='adam', init='uniform'):
# create model
if verbose: print("**Create model with optimizer: %s; init: %s" % (optimizer, init) )
model = Sequential()
model.add(Dense(16, input_dim=X.shape[1], kernel_initializer=init, activation='relu'))
model.add(Dense(8, kernel_initializer=init, activation='relu'))
model.add(Dense(4, kernel_initializer=init, activation='relu'))
model.add(Dense(1, kernel_initializer=init, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
拟合模型
best_epochs = 200
best_batch_size = 5
best_init = 'glorot_uniform'
best_optimizer = 'rmsprop'
verbose=0
model_pred = KerasClassifier(build_fn=create_model, optimizer=best_optimizer, init=best_init, epochs=best_epochs, batch_size=best_batch_size, verbose=verbose)
model_pred.fit(X_train,y_train)
我按照这里的教程进行操作:https://www.kaggle.com/stefanbergstein/keras-deep-learning-on-titanic-data
还有一个快速的人工智能模型,如下所示:
cont_names = [ 'feature1', 'feature2', 'feature3', 'feature4']
procs = [FillMissing, Categorify, Normalize]
test = TabularList.from_df(test,cont_names=cont_names, procs=procs)
data = (TabularList.from_df(train, path='.', cont_names=cont_names, procs=procs)
.random_split_by_pct(valid_pct=0.2, seed=43)
.label_from_df(cols = dep_var)
.add_test(test, label=0)
.databunch())
learn = tabular_learner(data, layers=[1000, 200, 15], metrics=accuracy, emb_drop=0.1, callback_fns=ShowGraph)
我按照下面的教程进行操作
print(X_train.shape,y_train.shape,X_test.shape,y_test.shape)
(138507, 4) (138507, 1) (34627, 4) (34627, 1)
不知道为什么这两个模型都需要这么长时间才能运行。我的输入是否有任何错误?任何帮助表示赞赏。
【问题讨论】:
-
你有多少训练样例?多少个纪元?能否提供带有 fit 方法的代码块?
-
@alan.elkin 我在问题中添加了它。谢谢
-
你有多少个班级(根据上面的描述我猜是 3 个)。如果是多类分类,为什么要使用
binary_crossentropy(需要根据你的目标标签使用categorical_crossentropy或sparse_categorical_crossentropy)
标签: keras neural-network fast-ai