首页 > 解决方案 > 根据数据的形状确定节点和层的数量

问题描述

有没有办法根据数据的形状确定节点和隐藏层的数量?另外,有没有办法根据主题确定最佳激活函数?

例如,我正在制作假新闻预测模型。我的特征是文本中的单词数、标题中的单词数、问题数、大写字母数等。我的数据集有 22 个特征和大约 35000 行。我的输出应该是 0 或 1。

基于此,我应该使用多少层和节点以及哪些激活函数最适合?

这是我的网:

model = Sequential()
model.add(Dense(100, input_dim = features.shape[1], activation = 'relu')) # input layer requires input_dim param
model.add(Dense(100, activation = 'relu'))
model.add(Dense(100, activation = 'relu'))
model.add(Dropout(0.1))
model.add(Dense(1, activation='sigmoid')) # sigmoid instead of relu for final probability between 0 and 1

sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss="mean_squared_error", optimizer=sgd, metrics=['accuracy'])


# call the function to fit to the data training the network)
model.fit(x_train, y_train, epochs = 10, shuffle = True, batch_size=32, validation_data=(x_test, y_test), verbose=1)


scores = model.evaluate(features, results)
print(model.metrics_names[1],  scores[1]*100)

标签: pythonkerasneural-network

解决方案


选择这些需要先前的经验,否则我们将不需要那么多 ML 工程师尝试不同的架构和撰写论文。

但首先我建议您看一下autokeras,这将有助于解决您的问题,因为它是一种已知问题 - 文本分类 -,您只需将数据结构化为输入(X 和 Y),然后将其提供给他们的文本分类器将尝试不同的模型(您可以指定)以选择最适合您的案例。

您可以在此处的文档中找到更多示例 https://autokeras.com/tutorial/text_classification/

import autokeras as ak

# Initialize the text classifier.
clf = ak.TextClassifier(max_trials=10) # It tries 10 different models
# Feed the text classifier with training data.
clf.fit(x_train, y_train)
# Predict with the best model.
predicted_y = clf.predict(x_test)
# Evaluate the best model with testing data.
print(clf.evaluate(x_test, y_test))

推荐阅读