tf.keras在训练过程中获得计算的梯度 [英] tf.keras get computed gradient during training

查看:775
本文介绍了tf.keras在训练过程中获得计算的梯度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照写在此处的内容在 tf.keras 的训练过程中获得计算出的梯度,我最终得到了在拟合阶段调用的以下回调函数:

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:

使用的网络是非常标准的网络,它是完全连接的和顺序的.

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

出现以下错误消息:

~\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

推荐答案

在python 3.6.9上使用较旧版本的keras(v.2.2.4)和tensorflow(1.13.1)解决了该问题.

Resolved the issue using an older version of keras(v. 2.2.4) and tensorflow (1.13.1) on python 3.6.9.

这篇关于tf.keras在训练过程中获得计算的梯度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆