SciTech-BigDataAIML-Tensorflow-Keras的API列表 + TensorFlow 模型建立与训练

Tensorflow链接:

https://www.tensorflow.org/install
https://www.tensorflow.org/guide
https://www.tensorflow.org/tutorials
https://www.tensorflow.org/learn

Tensorflow模型介绍

https://tf.wiki/zh_hans/basic/models.html

Keras API查询列表

https://keras.io/api/layers/core_layers/embedding/
https://keras.io/api/layers/reshaping_layers/flatten/

模型(Model)与层(Layer)

TensorFlow 推荐使用 Keras(tf.keras) 构建模型。
Keras 是一个广为流行的高级神经网络 API,简单、快速而不失灵活性,现已得到 TensorFlow 的官方内置和全面支持。

Keras 有两个重要的概念:Model(模型) 和 Layer(层)

  • Layer层将各种计算流程和变量进行封装(例如全连接层,CNN 卷积层和池化层等);
  • Model模型将各种Layer层进行组织和连接,并封装成一个整体,描述了如何将输入数据通过各种层以及运算而得到输出。

调用模型,使用 y_pred = model(X) 的形式即可。
Keras 在 tf.keras.layers 处内置深度学习会大量常用的的预定义层,同时也允许我们自定义层。

Keras 的Model模型class类的形式呈现,我们可以通过继承 tf.keras.Model 这个 Python 类来定义自己的模型。要继承类,需要重写 init() (构造函数,初始化)和 call(input) (模型调用)两个方法,同时也可以根据需要增加自定义的方法。

Tensorflow Models API

There are three ways to create Keras models:

  • The Sequential model, which is very straightforward (a simple list of layers), but is limited to single-input, single-output stacks of layers (as the name gives away).
  • The Functional API, which is an easy-to-use, fully-featured API that supports arbitrary model architectures. For most people and most use cases, this is what you should be using. This is the Keras "industry strength" model.
  • Model subclassing, where you implement everything from scratch on your own. Use this if you have complex, out-of-the-box research use cases.

Models API overview
The Model class
Model class
summary method
get_layer method
The Sequential class
Sequential class
add method
pop method
Model training APIs
compile method
fit method
evaluate method
predict method
train_on_batch method
test_on_batch method
predict_on_batch method
Saving & serialization
Whole model saving & loading
Weights-only saving & loading
Model config serialization
Model export for inference
Serialization utilities

Source Code Example

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

顺序模型:

model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)

posted @ 2024-05-06 22:23  abaelhe  阅读(26)  评论(0)    收藏  举报