python - 根据数据的形状确定节点和层的数量
问题描述
有没有办法根据数据的形状确定节点和隐藏层的数量?另外,有没有办法根据主题确定最佳激活函数?
例如,我正在制作假新闻预测模型。我的特征是文本中的单词数、标题中的单词数、问题数、大写字母数等。我的数据集有 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)
解决方案
选择这些需要先前的经验,否则我们将不需要那么多 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))
推荐阅读
- julia - Documenter.jl 构建挂起
- php - 如何使用 php simple dom 抓取无效的 html?
- php - 一个具有可变表/字段/值的函数,还是具有可变值的 60 个函数更好?
- powershell - PowerShell:“MemberOf”输出一个空白列而不是用户的“组名”
- javascript - 如何使 axios 表达式等待直到它收到响应
- python - 在 FastAPI 中提供大型请求示例数据失败
- css - 在某个元素之后选择第二个孩子
- laravel - Laravel+Nuxt CORS 在生产环境中被阻塞
- labview - 在事件结构内执行while循环期间无法按下LabVIEW中的停止按钮
- python - 在 azure 函数中导入自定义模块时出现 ModuleNotFound