【導讀】Keras是深度學習框架中最適合工程化的框架之一,具有簡單友好的接口。TensorFlow雖然功能強大,但是對於工程師來說,它的使用卻十分的繁瑣。好消息是Keras的許多核心功能已經融入了原生的TensorFlow(tf.keras),TensorFlow 1.9新增tf.keras官方入門文檔,介紹了tf.keras的使用。
這幾天打開TensorFlow的官方教程頁(https://www.tensorflow.org/tutorials/),會發現教程頁中給出的DEMO變成了基於tf.keras的教程。其實,tf.keras模塊早就存在於老版本的TensorFlow中,官方一直在完善它的功能。在TensorFlow 1.9放出了tf.keras的官方教程,看來是tf.keras模塊已經做得很穩定了。
用過Keras的都知道,對於工程來說,Keras相對於TensorFlow具有更友好的接口、更簡潔的代碼以及一些常用的工具類。例如,Keras Applications是Keras提供的一套非常好用的模型庫,直接包含了下面的模型,簡直就是工程師的瑞士軍刀:
Xception
VGG16
VGG19
ResNet50
InceptionV3
InceptionResNetV2
MobileNet
DenseNet
NASNet
MobileNetV2
例如通過下面的代碼就可以直接使用VGG 16提取特徵:
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
model = VGG16(weights='imagenet', include_top=False)
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
雖然現在Keras的主流是使用TensorFlow作為內核,但是直接混用Keras和TensorFlow還是很有難度的。在Keras中使用TensorFlow要按照Keras的規範使用TensorFlow來定製各種層,還是比較繁瑣的。tf.keras使得TensorFlow和Keras進行了深度的融合,不需要安裝Keras,就可以在TensorFlow中直接使用Keras的各種功能,而且tf.keras中的模塊是按照TensorFlow的規範定製的,自身就是原生的TensorFlow程序,可以和你原來寫的TensorFlow代碼進行無縫接軌。
在TensorFlow的官方文檔中,可以找到名為tf.keras.applications的Module ( https://www.tensorflow.org/api_docs/python/tf/keras/applications ),並且,keras.applications下的模型幾乎都被搬運了過來。
densenet module: DenseNet models for Keras.
inception_resnet_v2 module: Inception-ResNet V2 model for Keras.
inception_v3 module: Inception V3 model for Keras.
mobilenet module: MobileNet v1 models for Keras.
nasnet module: NASNet-A models for Keras.
resnet50 module: ResNet50 model for Keras.
vgg16 module: VGG16 model for Keras.
vgg19 module: VGG19 model for Keras.
xception module: Xception V1 model for Keras.
可以看一下用tf.keras構建模型有多麼容易,下面的例子與Keras的寫法差不多。同樣的模型使用TensorFlow靜態圖來構建可能需要兩倍以上的代碼量:
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(),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.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)
另外值得一提的是,tf.keras在TensorFlow的動態圖(Eager)中也扮演著重要的作用,在TensorFlow Eager的官方教程中,推薦使用tf.keras.Model來封裝模型:
https://www.tensorflow.org/guide/eager
class MNISTModel(tf.keras.Model):
def __init__(self):
super(MNISTModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(units=10)
self.dense2 = tf.keras.layers.Dense(units=10)
def call(self, input):
"""Run the model."""
result = self.dense1(input)
result = self.dense2(result)
result = self.dense2(result) # reuse variables from dense2 layer
return result
model = MNISTModel()
除了tf.keras,TensorFlow 1.9發布了更多的和動態圖相關的教程。
參考連結:https://www.tensorflow.org/tutorials/
人工智慧領域主題知識資料查看與加入專知人工智慧服務群:
【專知AI服務計劃】專知AI知識技術服務會員群加入與人工智慧領域26個主題知識資料全集獲取。歡迎微信掃一掃加入專知人工智慧知識星球群,獲取專業知識教程視頻資料和與專家交流諮詢!
請PC登錄www.zhuanzhi.ai或者點擊閱讀原文,註冊登錄專知,獲取更多AI知識資料!
請加專知小助手微信(掃一掃如下二維碼添加),加入專知主題群(請備註主題類型:AI、NLP、CV、 KG等)交流~
請關注專知公眾號,獲取人工智慧的專業知識!
點擊「閱讀原文」,使用專知