如何使用 Keras 在 Python 中实现集成?
Tensorflow是Google提供的机器学习框架。它是一个与Python结合使用以实现算法、深度学习应用程序等的开源框架。它用于研究和生产目的。
可以使用以下代码行在Windows上安装“tensorflow”包-
pip install tensorflow
Keras是作为ONEIROS(开放式神经电子智能机器人操作系统)项目研究的一部分而开发的。Keras是一个深度学习API,它是用Python编写的。它是一种高级API,具有有助于解决机器学习问题的高效界面。它运行在Tensorflow框架之上。它旨在帮助快速进行实验。它提供了在开发和封装机器学习解决方案中必不可少的基本抽象和构建块。
Keras已经存在于Tensorflow包中。可以使用以下代码行访问它。
import tensorflow from tensorflow import keras
与使用顺序API创建的模型相比,Keras函数式API有助于创建更灵活的模型。函数式API可以处理具有非线性拓扑结构的模型,可以共享层并处理多个输入和输出。深度学习模型通常是包含多个层的有向无环图(DAG)。函数式API有助于构建层图。
我们正在使用GoogleColaboratory运行以下代码。GoogleColab或Colaboratory帮助在浏览器上运行Python代码,并且需要零配置和免费访问GPU(图形处理单元)。Colaboratory建立在JupyterNotebook之上。以下是实现Ensemble模型的代码片段-
示例
def get_model():
inputs = keras.Input(shape=(128,))
outputs = layers.Dense(1)(inputs)
return keras.Model(inputs, outputs)
print("Calling the 'get_model' method ")
model_1 = get_model()
model_2 = get_model()
model_3 = get_model()
my_inputs = keras.Input(shape=(128,))
y1 = model_1(my_inputs)
y2 = model_2(my_inputs)
y3 = model_3(my_inputs)
print("The average of the layers in the model")
my_outputs = layers.average([y1, y2, y3])
print("Ensemble model is being created")
ensemble_model = keras.Model(inputs=my_inputs, outputs=my_outputs)代码信用-https://www.tensorflow.org/guide/keras/functional
输出结果
Calling the 'get_model' method The average of the layers in the model Ensemble model is being created
解释
模型可以嵌套,这意味着它可以包含子模型。
子模型用于集成。
这意味着将多个模型组合成一个模型,并对每个模型的预测进行平均。
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短