TensorFlow 1.9 新增 tf.keras 官方入門教程(Keras與TF的深度集成)

2021-03-02 專知

【導讀】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等)交流~

關注專知公眾號,獲取人工智慧的專業知識!

點擊「閱讀原文」,使用專知

相關焦點

  • Python安裝TensorFlow 2、tf.keras和深度學習模型的定義
    它使普通的深度學習任務(如分類和回歸預測建模)可供希望完成任務的普通開發人員使用。在本教程中,您將找到使用tf.keras API在TensorFlow中開發深度學習模型的分步指南。完成本教程後,您將知道:Keras和tf.keras之間的區別以及如何安裝和確認TensorFlow是否有效。
  • TensorFlow 2.0正式版官宣!深度集成Keras
    為了提高易用性,TensorFlow 2.0進行了許多修改,如取消了一些被認為是多餘的API,並緊密集成和依賴tf.keras作為中央高級API。TensorFlow與Keras深度學習庫的集成化最初始於2017年2月發布的TensorFlow1.0,本次更新讓二者的集成程度進一步提高。
  • TensorFlow 1.9.0-rc0 升級 tf.keras 至 Keras 2.1.6 API
    TensorFlow 1.9.0-rc0 已發布。該版本帶來了不少改進和新特性:Update tf.keras to the Keras 2.1.6 API.
  • 一文上手Tensorflow2.0之tf.keras|三
    【磐創AI導讀】:本系列文章介紹了與tensorflow的相關知識,
  • 心法利器[3] | tf.keras自學筆記
    當然,我不想寫成一個教程,也不想整成一個API,更希望這是一個導學,能給大家一些學習的思路,學這個之前,希望大家還是要對深度學習和tensorflow有些了解。下面講的實質性知識的東西,全部來源於tensorflow的源碼和官網版本的API文檔。
  • 深度學習環境配置指南:Pytorch、TensorFlow、Keras
    導讀本文介紹了作者使用RTX3090進行深度學習環境配置pytorch、tensorflow、keras等的詳細過程及代碼。筆者中山大學研究生,醫學生+計科學生的集合體,機器學習愛好者。基本環境(整個流程大約需要5分鐘甚至更少)py37或py38cuda11.0cudnn8.0.4tf2.5(tf-nightly)或 tf1.15.4pytorch1.7keras2.3
  • Keras和TensorFlow究竟哪個會更好?
    雖然自一年多之前,TensorFlow 就宣稱 Keras 將被併入 TensorFlow 的官方發布版本中,但令我詫異的是,仍有很多深度學習開發者沒有意識到,他們可以通過 tf.keras 子模塊來調用 Keras。更重要的是,Keras 與 TensorFlow 是無縫銜接的,使得我們將 TensorFlow 的原始碼直接寫入 Keras 模型中。
  • 用RNN構建文本生成器(TensorFlow Eager+ tf.keras)
    簡介教程包含了用Tensorflow Eager(動態圖)和tf.keras實現的可執行代碼,下面是代碼運行的示例結果:QUEENE:I had thought thou hadst a Roman; for the oracle,Thus by All bids the man against
  • RTX 3090 的深度學習環境配置指南:Pytorch、TensorFlow、Keras
    導讀本文介紹了作者使用RTX3090進行深度學習環境配置pytorch、tensorflow、keras等的詳細過程及代碼。筆者中山大學研究生,醫學生+計科學生的集合體,機器學習愛好者。基本環境(整個流程大約需要5分鐘甚至更少)py37或py38cuda11.0cudnn8.0.4tf2.5(tf-nightly)或 tf1.15.4pytorch1.7keras2.3
  • 圖像分類入門,輕鬆拿下90%準確率 | 教你用Keras搞定Fashion-MNIST
    pip install -q -U tensorflow>=1.8.03import tensorflow as tf4import numpy as np5import matplotlib.pyplot as plt67(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data
  • Keras入門系列教程:兩分鐘構建你的第一個神經網絡模型
    本教程由深度學習中文社區(Studydl.com)持續發布與更新, 本系列其餘教程地址見文章末尾.引言Keras是一個用於構建和訓練深度學習模型的高級API, 後端計算主要使用TensorFlow或者Theano, 由於Theano已停止開發新功能轉入維護階段, 而且目前最新版的TensorFlow已經包含了Keras模塊, 所以本教程採用基於TensorFlow的Keras進行講解, TensorFlow版本1.4.1
  • 英文教程太難啃?這裡有一份TensorFlow2.0中文教程
    今年 3 月份,谷歌在 Tensorflow Developer Summit 2019 大會上發布 TensorFlow 2.0 Alpha 版。作為當前最為流行的深度學習框架,2.0 Alpha 版的正式發布引人關注。近兩個月,網上已經出現了大量 TensorFlow 2.0 英文教程。在此文章中,機器之心為大家推薦一個持續更新的中文教程,以便大家學習。
  • 100天搞定機器學習|day39 Tensorflow Keras手寫數字識別
    TensorFlow 最初由Google大腦小組(隸屬於Google機器智能研究機構)的研究員和工程師們開發出來,用於機器學習和深度神經網絡方面的研究,但這個系統的通用性使其也可廣泛用於其他計算領域。1、安裝庫tensorflow有些教程會推薦安裝nightly,它適用於在一個全新的環境下進行TensorFlow的安裝,默認會把需要依賴的庫也一起裝上。
  • 教程 | 一招教你使用 tf.keras 和 eager execution 解決複雜問題
    (圖片標註)在暑期實習期間,我使用 TensorFlow 的兩個最新 API(tf.keras 和 eager execution)開發了這些示例,以下是分享內容。希望你們能覺得它們有用,有趣! Eager execution 是一個由運行定義的命令式接口,一旦從 Python 調用,其操作將被立即執行。這使得入門 TensorFlow 變得更簡單,也使研發更直觀。
  • Tensorflow.keras筆記-卷積神經網絡
    Tensorflow.keras筆記-卷積神經網絡cifar10數據集    1.
  • 基於RTX2060構建TensorFlow-gpu(keras)學習平臺
    開始菜單運行anaconda navigator檢查是否安裝了notebook(默認有安裝)三、安裝tensorflow/keras在激活的環境中安裝:1. 如果機器上有gpu,則安裝gpu版本,沒有GPU就安裝cpu版。
  • TensorFlow 2.1指南:keras模式、渴望模式和圖形模式(附代碼)
    Keras模式import numpy as npimport tensorflow as tffrom tensorflow import kerasfrom tensorflow.keras.layers import Input, Dense, Flatten, Conv2Dfrom tensorflow.keras
  • 圖像分類入門,輕鬆拿下90%準確率|教你用Keras搞Fashion-MNIST
    Keras是個容易上手且深受歡迎的深度學習高級庫,是一個獨立開源項目。在TensorFlow中,可以使用tf.keras函數來編寫Keras程序,這樣就能充分利用動態圖機制eager execution和tf.data函數。下面可能還會遇到其他深度學習名詞,我們就不提前介紹啦。
  • 使用tensorflow和Keras的初級教程
    https://www.kaggle.com/mlg-ulb/creditcardfraudimport tensorflow as tfprint(tf.__version__)import pandas as pdimport numpy as npfrom sklearn.model_selection import train_test_splitimport tensorflow as tffrom sklearn import preprocessingfrom tensorflow.keras.models import Sequentialfrom
  • 深度學習第17講:keras入門和快速上手指南
    作者:魯偉一個數據科學踐行者的學習日記。