K.learning_phase()
获取值,而不是张量本身。我需要学习阶段张量反馈到K.function
以获得层梯度、输出等。在import keras.backend as K
工作正常,但在import tensorflow.keras.backend as K
工作失败。
Relevant Git带部分解决方法
如何提取张量本身?你知道吗
可复制示例:
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
import numpy as np
ipt = Input((16,))
out = Dense(16)(ipt)
model = Model(ipt, out)
model.compile('adam', 'mse')
x = np.random.randn(32, 16)
model.train_on_batch(x, x)
grads = model.optimizer.get_gradients(model.total_loss, model.layers[-1].output)
grads_fn = K.function(inputs=[model.inputs[0], model._feed_targets[0], K.learning_phase()],
outputs=grads)
完整错误跟踪:
File "<ipython-input-2-7f74922d7492>", line 3, in <module>
outputs=grads)
File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3773, in function
return EagerExecutionFunction(inputs, outputs, updates=updates, name=name)
File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3670, in __init__
base_graph=source_graph)
File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\eager\lift_to_graph.py", line 249, in lift_to_graph
visited_ops = set([x.op for x in sources])
File "D:\Anaconda\envs\tf2_env\lib\site-packages\tensorflow_core\python\eager\lift_to_graph.py", line 249, in <listcomp>
visited_ops = set([x.op for x in sources])
AttributeError: 'int' object has no attribute 'op'
作为一种(不太好的)解决方法,您可以使用
symbolic_learning_phase()
fromtensorflow.python.keras.backend
:我不知道为什么这个函数不像
learning_phase()
,没有被导出到tensorflow.keras.backend
。也许有一个很好的理由不这样做。你知道吗此外,请注意,仅当您的模型包含在训练和推理模式中表现不同的层/操作(例如退出)时,在此处使用学习阶段才有意义。否则,函数的输出将是相同的。你知道吗
相关问题 更多 >
编程相关推荐