簡體   English   中英

Keras包含的TensorFlow操作如何獲取model

[英]How to get TensorFlow operations contained in Keras model

我有一個 TensorFlow Keras model (TensorFlow 2.6.0); 這是一個基本示例:

import tensorflow as tf

x = inp = tf.keras.Input((5,))
x = tf.keras.layers.Dense(7, activation="relu")(x)
x = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inp, x)

我想獲取圖中 model、select 特定操作的所有tf.Operation對象,然后創建一個新的tf.functiontf.keras.Model到 output 這些張量的任意輸入值

例如,在我上面的簡單 model 中,我可能想要獲取所有relu運算符的輸出。 我知道在那種情況下,我可以重新定義 model 以包括該層的 output 作為 model 的另一個 output,但這里的重點是我已經有了 model(它比上面復雜得多),並且有特定的運算符我想找到的輸出。

你有沒有試過這個:

all_ops = tf.get_default_graph().get_operations()

如果你有一個空列表並且你使用 Tensorflow 2.x,你可以試試這個:

import tensorflow as tf
print(tf.__version__)
tf.compat.v1.disable_eager_execution() # disable eager execution

a = tf.constant([1],name='aa')
print(tf.compat.v1.get_default_graph().get_operations())
print(tf.compat.v1.get_default_graph().get_tensor_by_name('aa:0'))

暫無
暫無

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

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