简体   繁体   中英

how I can dynamically create an list in Tensorflow

how I can dynamically create a loss list from a list of tasks (self.prediction) without having to create the variables:

Current:

loss0 = tf.losses.softmax_cross_entropy( logits = self.prediction[0], onehot_labels = self.Y[0] ) # task 0
loss1 = tf.losses.softmax_cross_entropy( logits = self.prediction[1], onehot_labels = self.Y[1] ) # task 1
loss2 = tf.losses.softmax_cross_entropy( logits = self.prediction[2], onehot_labels = self.Y[2] ) # task 2

self.losses = tf.reduce_sum( [ loss0, loss1, loss2 ] )

Goal:

list_loss = ?
self.losses = tf.reduce_sum( list_loss )

If I understand what you mean, you're asking for

def calculate_loss(prediction, label, idx):
    return tf.losses.softmax_cross_entropy(logits = prediction[idx], 
                                           onehot_labels = label[idx])

losses = []
for i in range(3):
    losses.append(calculate_loss(self.prediction, self.Y, i)
self.losses = tf.reduce_sum(losses)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM