【TensorFlow】Day-1 TensorFlow環境準備&參考資料

一、環境準備

1、安裝

這裏採用容器的方式安裝,安裝文檔可以參考https://www.tensorflow.org/install

docker pull tensorflow/tensorflow:latest-py3
# 在容器中運行宿主機指定腳本,-v參數將宿主機和容器進行綁定,-w參數指定tf的運行目錄,此時script.py腳本位於宿主機上
docker run -it --rm -v /usr/local/software/docker_files/tf_data/script:/tmp -w /tmp tensorflow/tensorflow python ./script.py
# 啓動jupyter notebook,該鏡像的jupyter notebook默認打開的是容器的/tf目錄
docker run -it -d -p 8888:8888 \
--net mynetwork \
-v /usr/local/software/docker_files/tf_data/script:/tf \
-v /usr/local/software/docker_files/tf_data/data:/data \
-v /usr/local/software/docker_files/tf_data/model:/model \
tensorflow/tensorflow:nightly-py3-jupyter

在容器+jupyter的模式下進入TF,通過日誌中如下類似鏈接”http://127.0.0.1:8888/?token=fb35504be29fdb4f218912dd09d91f09960ff84a2da49d32” ,可以直接打開jupyter頁面。

後續使用中會出現鏡像中沒有pandas模塊的提示,如果想要修改已有鏡像,按如下操作

# 進入容器
docker run -t -i tensorflow/tensorflow:nightly-py3-jupyter /bin/bash
# 安裝插件
pip install pandas
# 提交
docker commit -m "add pandas" -a "kmc" b72a667502e9 tensorflow/tensorflow:nightly-py3-jupyter

2、驗證

安裝完成後,進入如jupyter中,輸入如下代碼驗證,

>>> import tensorflow as tf
>>> tf.add(1, 2).numpy()
3
>>> hello = tf.constant('Hello, TensorFlow!')
>>> hello.numpy()
b'Hello, TensorFlow!'

二、算法學習路徑

在TF的官方文檔中提供了兩種入門方式,分別針對初學者或者有一定經驗的人。同時採用了MNIST數據集做手寫數字的識別。

1、初學者模式

對初學者而言,TF中已經封裝好了一些接口,直接通過API調用的方式快速定義並組裝模型,如下所示,

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)

2、進階模式

對有算法經驗的人來說,上面的模式只能讓用戶選擇一些內置的算法模型,以及定義模型的組裝關係,以及一些參數,損失函數的選擇等。如果在實際使用過程中,需要使用更加複雜的模型時,可以對比如下代碼,

class MyModel(tf.keras.Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.conv1 = Conv2D(32, 3, activation='relu')
    self.flatten = Flatten()
    self.d1 = Dense(128, activation='relu')
    self.d2 = Dense(10, activation='softmax')

  def call(self, x):
    x = self.conv1(x)
    x = self.flatten(x)
    x = self.d1(x)
    return self.d2(x)
model = MyModel()

with tf.GradientTape() as tape:
  logits = model(images)
  loss_value = loss(logits, labels)
grads = tape.gradient(loss_value, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))

根據TF官方文檔中的手寫數字識別案例,運行第一個TF模型。

3、算法學習路徑

TF的官方文檔中有一個介紹如何學習機器學習的介紹。

(1)基礎課程

在這篇文檔中,推薦了《Deep Learning in Python》一書作爲入門,推薦了一個Coursera課程介紹
TensorFlow入門
TensorFlow和深度學習

(2)TensorFlow實戰

Coursera上有一個TF實戰的課程TensorFlow in Practice

(3)練習

用TF文檔中的案例繼續熟悉TensorFlow Core tutorials

(4)深入學習TF

接下來可以參考《 Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow》一書,深入學習其中的概念和案例。

有關機器學習更進一步的介紹文檔,Theoretical and advanced machine learning with TensorFlow

4、Keras

上面封裝的比較好,使用方便的模式,是TF基於Keras實現的。更多關於Keras的介紹,可以查看TensorFlow Keras Overview

三、Resources

TF的官方文檔中有一個分類是Libraries & extensions,上面介紹的機器學習入門路線,就是其中的一個模塊。

在這個模塊中還有值得關注的內容是,
Neural Structured Learning
theoretical-and-advanced-machine-learning
以及更多其他內容。

四、參考資料

1、官方文檔

官方文檔

主要有教程指南兩個部分,

(1)教程

主要介紹一些TF使用方面的問題,從目錄結構可以看到,這部分主要涉及到基礎功能,

  • Keras操作
  • 數據裝載
  • Estimator

進階功能

  • 自定義
  • 分佈式模型
  • 圖像
  • 文本
  • 數據結構
  • 前沿拓展,比如對抗,強化學習等

(2)指南

主要是一些API的介紹,相對教程部分更加偏向於後續學習過程中的API介紹查閱。

2、Github

項目官方的Github地址。

另外還有一些對應的練習項目,

TensorFlow Tutorial and Examples for Beginners

3、系統學習文檔

中文文檔
TF2中文教程
30天喫掉那隻 TensorFlow2.0

4、源碼解讀

  1. 圖解tensorflow 源碼

https://github.com/yao62995/tensorflow
https://www.cnblogs.com/yao62995/p/5773578.html

5、學習資源

https://www.tensorflow.org/resources/learn-ml#four-areas

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章