本文整理汇总了Python中lasagne.objectives.binary_crossentropy方法的典型用法代码示例。如果您正苦于以下问题:Python objectives.binary_crossentropy方法的具体用法?Python objectives.binary_crossentropy怎么用?Python objectives.binary_crossentropy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lasagne.objectives
的用法示例。
在下文中一共展示了objectives.binary_crossentropy方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calc_loss_multi
# 需要导入模块: from lasagne import objectives [as 别名]
# 或者: from lasagne.objectives import binary_crossentropy [as 别名]
def calc_loss_multi(prediction, targets):
#we need to clip predictions when calculating the log-loss
prediction = T.clip(prediction, 0.0000001, 0.9999999)
#binary crossentropy is the best choice for a multi-class sigmoid output
loss = T.mean(objectives.binary_crossentropy(prediction, targets))
return loss
#theano variable for the class targets
示例2: build_loss
# 需要导入模块: from lasagne import objectives [as 别名]
# 或者: from lasagne.objectives import binary_crossentropy [as 别名]
def build_loss(pred_var, target_var, resp_dur, t_ind):
if t_ind==0 or t_ind==1 or t_ind==4:
loss = T.mean(T.mod(T.abs_(pred_var[:, -resp_dur:, :] - target_var[:, -resp_dur:, :]), np.pi))
elif t_ind==2 or t_ind==6 or t_ind==8:
loss = T.mean(binary_crossentropy(pred_var[:,-resp_dur:,-1], target_var[:,-resp_dur:,-1]))
return loss
示例3: compile_update_softmax
# 需要导入模块: from lasagne import objectives [as 别名]
# 或者: from lasagne.objectives import binary_crossentropy [as 别名]
def compile_update_softmax(nnet, inputs, targets):
"""
create a softmax loss for network given in argument
"""
floatX = Cfg.floatX
C = Cfg.C
final_layer = nnet.all_layers[-1]
trainable_params = lasagne.layers.get_all_params(final_layer, trainable=True)
# Regularization
if Cfg.weight_decay:
l2_penalty = (floatX(0.5) / C) * get_l2_penalty(nnet)
else:
l2_penalty = T.cast(0, dtype='floatX')
# Backpropagation
prediction = lasagne.layers.get_output(final_layer, inputs=inputs, deterministic=False)
if Cfg.ad_experiment:
train_loss = T.mean(l_objectives.binary_crossentropy(prediction.flatten(), targets), dtype='floatX')
train_acc = T.mean(l_objectives.binary_accuracy(prediction.flatten(), targets), dtype='floatX')
else:
train_loss = T.mean(l_objectives.categorical_crossentropy(prediction, targets), dtype='floatX')
train_acc = T.mean(T.eq(T.argmax(prediction, axis=1), targets), dtype='floatX')
train_obj = T.cast(train_loss + l2_penalty, dtype='floatX')
updates = get_updates(nnet, train_obj, trainable_params, solver=nnet.solver)
nnet.backprop = theano.function([inputs, targets], [train_obj, train_acc], updates=updates)
# Forwardpropagation
test_prediction = lasagne.layers.get_output(final_layer, inputs=inputs, deterministic=True)
if Cfg.ad_experiment:
test_loss = T.mean(l_objectives.binary_crossentropy(test_prediction.flatten(), targets), dtype='floatX')
test_acc = T.mean(l_objectives.binary_accuracy(test_prediction.flatten(), targets), dtype='floatX')
else:
test_loss = T.mean(l_objectives.categorical_crossentropy(test_prediction, targets), dtype='floatX')
test_acc = T.mean(T.eq(T.argmax(test_prediction, axis=1), targets), dtype='floatX')
test_obj = T.cast(test_loss + l2_penalty, dtype='floatX')
nnet.forward = theano.function([inputs, targets], [test_obj, test_acc, test_prediction, l2_penalty, test_loss])
示例4: create_autoencoder
# 需要导入模块: from lasagne import objectives [as 别名]
# 或者: from lasagne.objectives import binary_crossentropy [as 别名]
def create_autoencoder(nnet):
"""
create autoencoder Theano update for network given in argument
"""
floatX = Cfg.floatX
C = Cfg.ae_C
ndim = nnet.data._X_train.ndim
if ndim == 2:
inputs = T.matrix('inputs')
elif ndim == 4:
inputs = T.tensor4('inputs')
final_layer = nnet.all_layers[-1]
# Backpropagation
trainable_params = lasagne.layers.get_all_params(final_layer, trainable=True)
prediction = lasagne.layers.get_output(final_layer, inputs=inputs, deterministic=False)
# use l2 or binary crossentropy loss (features are scaled to [0,1])
if Cfg.ae_loss == "l2":
loss = lasagne.objectives.squared_error(prediction, inputs)
if Cfg.ae_loss == "ce":
loss = lasagne.objectives.binary_crossentropy(prediction, inputs)
scores = T.sum(loss, axis=range(1, ndim), dtype='floatX')
loss = T.mean(scores)
# Regularization
if Cfg.ae_weight_decay:
l2_penalty = (floatX(0.5) / C) * regularize_network_params(final_layer, l2)
else:
l2_penalty = T.cast(0, dtype='floatX')
train_obj = loss + l2_penalty
updates = get_updates(nnet, train_obj, trainable_params, solver=nnet.ae_solver)
nnet.ae_backprop = theano.function([inputs], [loss, l2_penalty, scores], updates=updates)
# Forwardpropagation
test_prediction = lasagne.layers.get_output(final_layer, inputs=inputs, deterministic=True)
# use l2 or binary crossentropy loss (features are scaled to [0,1])
if Cfg.ae_loss == "l2":
test_loss = lasagne.objectives.squared_error(test_prediction, inputs)
if Cfg.ae_loss == "ce":
test_loss = lasagne.objectives.binary_crossentropy(test_prediction, inputs)
test_scores = T.sum(test_loss, axis=range(1, ndim), dtype='floatX')
test_loss = T.mean(test_scores)
nnet.ae_forward = theano.function([inputs],
[test_loss, l2_penalty, test_scores, test_prediction])