建设网站是什么,233建工网校官网,网站改版会降权吗,wordpress插件 占用内存TensorFlow提供了众多的API#xff0c;简单地可以分类为高阶API和低阶API. API太多太乱也是TensorFlow被诟病的重点之一#xff0c;可能因为Google的工程师太多了#xff0c;社区太活跃了~当然后来Google也意识到这个问题#xff0c;在TensorFlow 2.0中有了很大的改善。本文…TensorFlow提供了众多的API简单地可以分类为高阶API和低阶API. API太多太乱也是TensorFlow被诟病的重点之一可能因为Google的工程师太多了社区太活跃了~当然后来Google也意识到这个问题在TensorFlow 2.0中有了很大的改善。本文就简要介绍一下TensorFlow的高阶API和低阶API使用提供推荐的使用方式。
高阶APIFor beginners
The best place to start is with the user-friendly Keras sequential API. Build models by plugging together building blocks.
TensorFlow推荐使用Keras的sequence函数作为高阶API的入口进行模型的构建就像堆积木一样
# 导入TensorFlow, 以及下面的常用Keras层
import tensorflow as tf
from tensorflow.keras.layers import Flatten, Dense, Dropout# 加载并准备好MNIST数据集
mnist tf.keras.datasets.mnist(x_train, y_train), (x_test, y_test) mnist.load_data()# 将样本从0~255的整数转换为0~1的浮点数x_train, x_test x_train / 255.0, x_test / 255.0# 将模型的各层堆叠起来以搭建 tf.keras.Sequential 模型
model tf.keras.models.Sequential([Flatten(input_shape(28, 28)),Dense(128, activationrelu),Dropout(0.5),Dense(10, activationsoftmax)])# 为训练选择优化器和损失函数model.compile(optimizeradam,losssparse_categorical_crossentropy,metrics[accuracy])
# 训练并验证模型
model.fit(x_train, y_train, epochs5)model.evaluate(x_test, y_test, verbose2)
输出的日志
Train on 60000 samples
Epoch 1/5
60000/60000 [] - 4s 72us/sample - loss: 0.2919 - accuracy: 0.9156
Epoch 2/5
60000/60000 [] - 4s 58us/sample - loss: 0.1439 - accuracy: 0.9568
Epoch 3/5
60000/60000 [] - 4s 58us/sample - loss: 0.1080 - accuracy: 0.9671
Epoch 4/5
60000/60000 [] - 4s 59us/sample - loss: 0.0875 - accuracy: 0.9731
Epoch 5/5
60000/60000 [] - 3s 58us/sample - loss: 0.0744 - accuracy: 0.9766
10000/1 - 1s - loss: 0.0383 - accuracy: 0.9765
[0.07581, 0.9765] 日志的最后一行有两个数 [0.07581, 0.9765]0.07581是最终的loss值也就是交叉熵0.9765是测试集的accuracy结果这个数字手写体模型的精度已经将近98%.
低阶APIFor experts
The Keras functional and subclassing APIs provide a define-by-run interface for customization and advanced research. Build your model, then write the forward and backward pass. Create custom layers, activations, and training loops.
说到TensorFlow低阶API最先想到的肯定是tf.Session和著名的sess.run但随着TensorFlow的发展tf.Session最后出现在TensorFlow 1.15中TensorFlow 2.0已经取消了这个API如果非要使用的话只能使用兼容版本的tf.compat.v1.Session. 当然还是推荐使用新版的API这里也是用Keras但是用的是subclass的相关API以及GradientTape. 下面会详细介绍。 # 导入TensorFlow, 以及下面的常用Keras层
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2Dfrom tensorflow.keras import Model# 加载并准备好MNIST数据集mnist tf.keras.datasets.mnist(x_train, y_train), (x_test, y_test) mnist.load_data()# 将样本从0~255的整数转换为0~1的浮点数x_train, x_test x_train / 255.0, x_test / 255.0
# 使用 tf.data 来将数据集切分为 batch 以及混淆数据集batch_size 32
train_ds tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(batch_size)test_ds tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(batch_size)
# 使用 Keras 模型子类化model subclassing API 构建 tf.keras 模型
class MyModel(Model):def __init__(self):super(MyModel, self).__init__()self.flatten Flatten()self.d1 Dense(128, activationrelu)self.dropout Dropout(0.5)self.d2 Dense(10, activationsoftmax)def call(self, x):x self.flatten(x)x self.d1(x)x self.dropout(x)return self.d2(x)model MyModel()# 为训练选择优化器和损失函数loss_object tf.keras.losses.SparseCategoricalCrossentropy()optimizer tf.keras.optimizers.Adam()# 选择衡量指标来度量模型的损失值loss和准确率accuracy。这些指标在 epoch 上累积值然后打印出整体结果train_loss tf.keras.metrics.Mean(nametrain_loss)train_accuracy tf.keras.metrics.SparseCategoricalAccuracy(nametrain_accuracy)test_loss tf.keras.metrics.Mean(nametest_loss)test_accuracy tf.keras.metrics.SparseCategoricalAccuracy(nametest_accuracy)# 使用 tf.GradientTape 来训练模型tf.functiondef train_step(images, labels):with tf.GradientTape() as tape:predictions model(images)loss loss_object(labels, predictions)gradients tape.gradient(loss, model.trainable_variables)optimizer.apply_gradients(zip(gradients, model.trainable_variables))train_loss(loss)train_accuracy(labels, predictions)# 使用 tf.GradientTape 来训练模型tf.functiondef train_step(images, labels):with tf.GradientTape() as tape:predictions model(images)loss loss_object(labels, predictions)gradients tape.gradient(loss, model.trainable_variables)optimizer.apply_gradients(zip(gradients, model.trainable_variables))train_loss(loss)train_accuracy(labels, predictions)# 测试模型
tf.functiondef test_step(images, labels):predictions model(images)t_loss loss_object(labels, predictions)test_loss(t_loss)test_accuracy(labels, predictions)EPOCHS 5for epoch in range(EPOCHS):for images, labels in train_ds:train_step(images, labels)for test_images, test_labels in test_ds:test_step(test_images, test_labels)template Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}print (template.format(epoch1,train_loss.result(),train_accuracy.result()*100,test_loss.result(),test_accuracy.result()*100)) 输出
Epoch 1, Loss: 0.13822732865810394, Accuracy: 95.84833526611328, Test Loss: 0.07067110389471054, Test Accuracy: 97.75
Epoch 2, Loss: 0.09080979228019714, Accuracy: 97.25, Test Loss: 0.06446609646081924, Test Accuracy: 97.95999908447266
Epoch 3, Loss: 0.06777264922857285, Accuracy: 97.93944549560547, Test Loss: 0.06325332075357437, Test Accuracy: 98.04000091552734
Epoch 4, Loss: 0.054447807371616364, Accuracy: 98.33999633789062, Test Loss: 0.06611879169940948, Test Accuracy: 98.00749969482422
Epoch 5, Loss: 0.04556874558329582, Accuracy: 98.60433197021484, Test Loss: 0.06510476022958755, Test Accuracy: 98.10400390625 可以看出低阶API把整个训练的过程都暴露出来了包括数据的shuffle每个epoch重新排序数据使得训练数据随机化避免周期性重复带来的影响及组成训练batch组建模型的数据通路具体定义各种评估指标loss, accuracy计算梯度更新梯度这两步尤为重要。如果用户需要对梯度或者中间过程做处理甚至打印等使用低阶API可以完全进行完全的控制。
如何选择
从上面的标题也可以看出对于初学者来说建议使用高阶API简单清晰可以迅速入门。对于专家学者们建议使用低阶API可以随心所欲地对具体细节进行改造和加工。