Wie implementiere ich Weighted Binary CrossEntropy auf theano?
Wie implementiere ich Weighted Binary CrossEntropy auf theano?
Mein Faltungsneurales Netzwerk sagt nur 0 ~ ~ 1 (Sigmoid) voraus.
Ich möchte meine Vorhersagen auf diese Weise bestrafen:
rundsätzlich möchte ich MEHR bestrafen, wenn das Modell 0 vorhersagt, aber die Wahrheit 1 ist.
Frage: Wie kann ich das erstellenWeighted Binary CrossEntropy Funktion mit Theano und Lasagne?
Ich habe das unten versucht
prediction = lasagne.layers.get_output(model)
import theano.tensor as T
def weighted_crossentropy(predictions, targets):
# Copy the tensor
tgt = targets.copy("tgt")
# Make it a vector
# tgt = tgt.flatten()
# tgt = tgt.reshape(3000)
# tgt = tgt.dimshuffle(1,0)
newshape = (T.shape(tgt)[0])
tgt = T.reshape(tgt, newshape)
#Process it so [index] < 0.5 = 0 , and [index] >= 0.5 = 1
# Make it an integer.
tgt = T.cast(tgt, 'int32')
weights_per_label = theano.shared(lasagne.utils.floatX([0.2, 0.4]))
weights = weights_per_label[tgt] # returns a targets-shaped weight matrix
loss = lasagne.objectives.aggregate(T.nnet.binary_crossentropy(predictions, tgt), weights=weights)
return loss
loss_or_grads = weighted_crossentropy(prediction, self.target_var)
Aber ich erhalte diesen Fehler unten:
TypeError: Neue Form in Umformung muss ein Vektor oder eine Liste / ein Tupel von Skalar sein. Erhaltener Subtensor {int64} .0 nach Konvertierung in einen Vektor.
Referenz :https: //github.com/fchollet/keras/issues/211
Referenz :https: //groups.google.com/forum/#! topic / theano-users / R_Q4uG9BXp8