如何使用Keras在Python中实现转学?

Tensorflow是Google提供的一种机器学习框架。它是一个开放源代码框架,与Python结合使用以实现算法,深度学习应用程序等等。它用于研究和生产目的。

Tensor是TensorFlow中使用的数据结构。它有助于连接流程图中的边缘。该流程图称为“数据流程图”。张量不过是多维数组或列表。

Keras在希腊语中的意思是“号角”。Keras被开发为ONEIROS(开放式神经电子智能机器人操作系统)项目研究的一部分。Keras是使用Python编写的深度学习API。它是一个高级API,具有可帮助解决机器学习问题的高效接口。

它在Tensorflow框架之上运行。它旨在帮助快速进行实验。它提供了在开发和封装机器学习解决方案中必不可少的基本抽象和构建块。

它具有高度的可扩展性,并具有跨平台功能。这意味着Keras可以在TPU或GPU集群上运行。Keras模型也可以导出为在Web浏览器或手机中运行。

Keras已经存在于Tensorflow软件包中。可以使用下面的代码行进行访问。

import tensorflow
from tensorflow import keras

我们正在使用Google合作实验室来运行以下代码。Google Colab或Colaboratory可以帮助通过浏览器运行Python代码,并且需要零配置和对GPU(图形处理单元)的免费访问。合作已建立在Jupyter Notebook的基础上。以下是代码片段-

示例

model = keras.Sequential([
   keras.Input(shape=(784))
   layers.Dense(32, activation='relu'),
   layers.Dense(32, activation='relu'),
   layers.Dense(32, activation='relu'),
   layers.Dense(10),
])
print("Load the pre-trained weights")
model.load_weights(...)
print("Freeze all the layers except the last layer")
for layer in model.layers[:-1]:
   layer.trainable = False
print("Recompile the model and train it")
print("The last layer weights will be updated")
model.compile(...)
model.fit(...)

代码信用-https://www.tensorflow.org/guide/keras/sequential_model

输出结果

Load the pre-trained weights
Freeze all the layers except the last layer
Recompile the model and train it
The last layer weights will be updated

解释

  • 转移学习表明冻结模型中的底层并训练顶层。

  • 建立了顺序模型。

  • 旧模型的预训练权重已加载并与此模型绑定。

  • 除最后一层外,最底层均被冻结。

  • 遍历各层,除最后一层外,所有层的“ layer.trainable”均设置为“ False”。

  • 它被编译并适合数据。

猜你喜欢