簡體   English   中英

Keras 的 TensorFlow 后端是否依賴於 Eager Execution?

[英]Does the TensorFlow backend of Keras rely on the eager execution?

Keras 的 TensorFlow 后端是否依賴於 Eager Execution?

如果不是這種情況,我是否可以基於 Keras 和 TensorFlow 操作構建一個 TensorFlow 圖,然后使用 Keras 高級 API 訓練整個模型?

這是出於研究目的,我無法在這里介紹。

這使得回答你的問題真的很困難。 如果你能找到一個玩具示例——與你的研究無關——你想要什么,我們會嘗試從那里構建一些東西,那就更好了。

Keras 的 TensorFlow 后端是否依賴於 Eager Execution?

不,它沒有。 Keras 是在引入 Eager Execution 之前構建的。 然而,Keras(tf 中的那個)可以在急切執行模式下工作(請參閱 fchollet 的回答)。

我可以構建一個 TensorFlow 圖並將其與 Keras 模型結合,然后使用 Keras 高級 API 聯合訓練它們嗎?

我不確定“構建 TensorFlow 圖”是什么意思,因為每當您使用 keras 時,圖就已經存在。 如果您正在談論向現有圖形添加一堆操作,那么這絕對是可能的。 你只需要用一個 Lambda 層把它包裹起來,就像在符號模式下使用 Keras 一樣:

import tensorflow as tf
from sacred import Experiment

ex = Experiment('test-18')

tf.enable_eager_execution()


@ex.config
def my_config():
    pass


@ex.automain
def main():
    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

    x_train, x_test = (e.reshape(e.shape[0], -1) for e in (x_train, x_test))
    y_train, y_test = (tf.keras.utils.to_categorical(e) for e in (y_train, y_test))

    def complex_tf_fn(x):
        u, v = tf.nn.moments(x, axes=[1], keep_dims=True)
        return (x - u) / tf.sqrt(v)

    with tf.device('/cpu:0'):
        model = tf.keras.Sequential([
            tf.keras.layers.Lambda(complex_tf_fn, input_shape=[784]),
            tf.keras.layers.Dense(1024, activation='relu'),
            tf.keras.layers.Lambda(complex_tf_fn),
            tf.keras.layers.Dense(10, activation='softmax')
        ])
        model.compile(optimizer=tf.train.AdamOptimizer(),
                      loss='categorical_crossentropy')

        model.fit(x_train, y_train,
                  epochs=10,
                  validation_data=(x_test, y_test),
                  batch_size=1024,
                  verbose=2)
python test-18.py with seed=21

INFO - test-18 - Running command 'main'
INFO - test-18 - Started
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
 - 9s - loss: 3.4012 - val_loss: 1.3575
Epoch 2/10
 - 9s - loss: 0.9870 - val_loss: 0.7270
Epoch 3/10
 - 9s - loss: 0.6097 - val_loss: 0.6071
Epoch 4/10
 - 9s - loss: 0.4459 - val_loss: 0.4824
Epoch 5/10
 - 9s - loss: 0.3352 - val_loss: 0.4436
Epoch 6/10
 - 9s - loss: 0.2661 - val_loss: 0.3997
Epoch 7/10
 - 9s - loss: 0.2205 - val_loss: 0.4048
Epoch 8/10
 - 9s - loss: 0.1877 - val_loss: 0.3788
Epoch 9/10
 - 9s - loss: 0.1511 - val_loss: 0.3506
Epoch 10/10
 - 9s - loss: 0.1304 - val_loss: 0.3330
INFO - test-18 - Completed after 0:01:31

Process finished with exit code 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM