In tensorflow 1.15.5 / keras, how to use a tensor defined during building as a loss value for tape.gradient

huangapple go评论48阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月12日 16:05:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定