Keras
- 不能直接
import tensorflow.keras
,而是應該直接用import tensorflow as tf
然後調用tf.keras
,但是可以用from tensorflow.keras import layers
。 - 可以用
tf.dataset.Dataset.from_tensor_slice
將Numpy Array變爲dataset layers
內置了各種常用計算網絡的layers,同時也可以自己定義。keras.Model
封裝了用於建立、訓練、評估、存儲一個計算網絡和用計算網絡來推斷新樣本所需要的方法,model
裏面是一個一個的layer,而model
本身只需要定義inputs
和outputs
。Model需要compile
以後才能用。- model裏面存儲了所有的變量,比如一個2D卷積層的變量包括一個kernel和一個bias
Functional API
是定義model
的結構的,它和自己定義的model subclassing一樣,都是自定義model用的,看起來,用functional API定義好model是最不容易出錯的,實在不行,自定義layers然後用functional API定義models也可以。- 定義一個customized model在eager execution模式下可以看到forward pass。但是如果直接在
call
函數內打斷點,在計算網絡執行的時候是不會停下的。 - 可以存儲且需要存儲的主要有網絡的結構和網絡內部的參數。
Eager Execution
- 這個模式下,用交互式python就可以直接出結果,是動態執行,不是靜態建立好了計算網絡再執行。
tf.Tensor objects reference concrete values instead of symbolic handles to nodes in a computational graph.
- model不需要compile也能直接計算。
- 訓練過程是顯性的,每一次計算梯度、參數值的更新都是可見的、在一個for loop裏面的
tf.Variable
的數據類型是根據輸入自動決定的,比如tf.Variable([3])
的類型是intergerself.kernel = self.add_variable("kernel", [input_shape[-1], self.output_units])
要改爲[int(input_shape[-1]), int(self.output_units)]
- 總之,在Eager Execution下,最大的特點就是不需要session了。