[英]tf.keras get computed gradient during training
Following what is written here I was trying to get the computed gradient during the training using tf.keras , I've end up with the following callback function which is called during the fitting's phase:按照这里写的内容,我试图在训练期间使用tf.keras获得计算的梯度,我最终得到了以下在拟合阶段调用的回调函数:
The used networks is a very standard one, fully connected and sequential.使用的网络是一个非常标准的网络,完全连接和顺序的。
r = network.fit(x=trn.X,y=trn.Y,verbose=2,batch_size=50,epochs=50,callbacks=[reporter,])
def on_train_begin(self, logs={}):
# Functions return weights of each layer
self.layerweights = []
for lndx, l in enumerate(self.model.layers):
if hasattr(l, 'kernel'):
self.layerweights.append(l.kernel)
input_tensors = [self.model.inputs[0],
self.model.sample_weights[0],
self.model.targets[0],
K.learning_phase()]
# Get gradients of all the relevant layers at once
grads = self.model.optimizer.get_gradients(self.model.total_loss, self.layerweights)
self.get_gradients = K.function(inputs=input_tensors,outputs=grads) # <-- Error Here
which rise the following Error Message:出现以下错误消息:
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\eager\lift_to_graph.py in (.0)
312 # Check that the initializer does not depend on any placeholders.
313 sources = set(sources or [])
--> 314 visited_ops = set([x.op for x in sources])
315 op_outputs = collections.defaultdict(set)
316
AttributeError: 'NoneType' object has no attribute 'op'
Any idea how to resolve it?知道如何解决吗? Already read this one , and this one , but got no luck
已经阅读了这一本和这一本,但没有运气
AttributeError: 'NoneType' object has no attribute 'op'
means that you have a objects or attributes got None.意味着你有一个对象或属性没有。
To handle it you can use this:要处理它,您可以使用它:
visited_ops = set([x.op for x in sources if x])
在 python 3.6.9 上使用旧版本的 keras(v. 2.2.4) 和 tensorflow (1.13.1) 解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.