我想使用注意模型提取注意分数。但找不到任何要使用的TF2 API。 简单代码:
import tensorflow as tf
model = train_model()
func = tf.function(model)
tensor_specs1 = tf.TensorSpec.from_tensor(model.input)
call = func.get_concrete_function(tensor_specs1)
graph = call.graph
tensor_names = [n for n in graph.as_graph_def().node]
for name in tensor_names:
print(name)
outputs = graph.get_tensor_by_name('model_1/word_encoder_time/word_attention/Softmax:0')
pred_model = tf.keras.models.Model(model.input,outputs)
results = pred_model(tensor_specs1)
print(results)
但提出一个例外:
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("model_1/word_encoder_time/model/word_attention/BiasAdd:0", shape=(?, 10), dtype=float32) is not an element of this graph
它起作用了,但不是我想要的:
outputs = [model.get_layer(name=output).get_output_at(0) for output in output_layers]
pred_model = tf.keras.models.Model(model.input,outputs)
我想得到中间张量,而不是层的输出
为了评估
Keras
模型中任意层的输出,可以使用Keras函数来避免使用会话和图形输出:
相关问题 更多 >
编程相关推荐