英文:
In tensorflow 1.15.5 / keras, how to use a tensor defined during building as a loss value for tape.gradient
问题
I want to change this line of a model training :
net.train_on_batch(train_subdata_batch_cache, train_subdata_to_batch_cache)
Into something like this:
with tf.GradientTape() as tape:
net(tf.convert_to_tensor(train_subdata_batch_cache, dtype=tf.float32))
loss_value = self.loss
grads = tape.gradient(loss_value, net.trainable_weights)
optimizer.apply_gradients(zip(grads, net.trainable_weights))
Where self.loss comes from here
But, when I execute this code, I get:
AttributeError: 'function' object has no attribute 'dtype'
So I also tried to add a new variable in model.py (just below l. 1534), namely:
self.otherloss = total_loss
and then do:
loss_value = self.otherloss
But this time I am getting:
AttributeError: 'RefVariable' object has no attribute '_id'
To sum up, I want to take the loss stored in the variable total_loss and use it in my custom training loop.
Thanks
英文:
I want to change this line of a model training :
net.train_on_batch(train_subdata_batch_cache, train_subdata_to_batch_cache)
Into something like this:
with tf.GradientTape() as tape:
net(tf.convert_to_tensor(train_subdata_batch_cache, dtype=tf.float32))
loss_value = self.loss
grads = tape.gradient(loss_value, net.trainable_weights)
optimizer.apply_gradients(zip(grads, net.trainable_weights))
Where self.loss comes from here
But, when I execute this code, I get:
> AttributeError: 'function' object has no attribute 'dtype'
So I also tried to add a new variable in model.py (just below l. 1534), namely:
self.otherloss = total_loss
and then do:
loss_value = self.otherloss
But this time I am getting:
> AttributeError: 'RefVariable' object has no attribute '_id'
To sum up, I want to take the loss stored in the variable total_loss and use it in my custom training loop.
Thanks
答案1
得分: 1
根据我的理解,问题在于net
是一个具有自己损失成本的神经网络层,而self.loss
是类Network
中的一个函数,作为执行多个层逻辑的接口。
我认为你正在寻找对象net
中的层的损失,但我不确定该对象是什么以获取损失,你可以尝试使用Keras API 检查损失:
tf.keras.losses.get(net)
或者你可以尝试查看Tf Keras keep loss of every batch。
如果你反而在寻找所有层的损失列表,你可以在self.losses
中找到它。
英文:
To my understanding the problem is that net
is the layer of a NN with its own loss cost, and self.loss
is a function in the class Network
working as an interface to execute logic of several layers.
I think what you are searching for the loss of a layer which it's in the object net
, but I am not sure which object is that to get the loss, you can try to use the Keras api to check the loss by:
tf.keras.losses.get(net)
Or you can try to check Tf Keras keep loss of every batch
If instead you search for the list of the losses of all the layers you can find it in self.losses
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论