本文整理汇总了Python中keras.losses.binary_crossentropy方法的典型用法代码示例。如果您正苦于以下问题:Python losses.binary_crossentropy方法的具体用法?Python losses.binary_crossentropy怎么用?Python losses.binary_crossentropy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.losses
的用法示例。
在下文中一共展示了losses.binary_crossentropy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: breast_cancer
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def breast_cancer():
from keras.optimizers import Adam, Nadam, RMSprop
from keras.losses import logcosh, binary_crossentropy
from keras.activations import relu, elu, sigmoid
# then we can go ahead and set the parameter space
p = {'lr': (0.5, 5, 10),
'first_neuron': [4, 8, 16, 32, 64],
'hidden_layers': [0, 1, 2],
'batch_size': (2, 30, 10),
'epochs': [50, 100, 150],
'dropout': (0, 0.5, 5),
'shapes': ['brick', 'triangle', 'funnel'],
'optimizer': [Adam, Nadam, RMSprop],
'losses': [logcosh, binary_crossentropy],
'activation': [relu, elu],
'last_activation': [sigmoid]}
return p
示例2: __init__
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def __init__(
self,
n_hidden_set_units=32,
learning_rate=1e-3,
batch_size=256,
loss_function=binary_crossentropy,
epochs_drop=300,
drop=0.1,
random_state=None,
**kwargs,
):
self.n_hidden_set_units = n_hidden_set_units
self.learning_rate = learning_rate
self.batch_size = batch_size
self.random_state = random_state
self.loss_function = loss_function
self.epochs_drop = epochs_drop
self.drop = drop
self.current_lr = None
self.weight1 = None
self.bias1 = None
self.weight2 = None
self.bias2 = None
self.optimizer = None
示例3: online_bootstrapping
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def online_bootstrapping(y_true, y_pred, pixels=512, threshold=0.5):
""" Implements nline Bootstrapping crossentropy loss, to train only on hard pixels,
see https://arxiv.org/abs/1605.06885 Bridging Category-level and Instance-level Semantic Image Segmentation
The implementation is a bit different as we use binary crossentropy instead of softmax
SUPPORTS ONLY MINIBATCH WITH 1 ELEMENT!
# Arguments
y_true: A tensor with labels.
y_pred: A tensor with predicted probabilites.
pixels: number of hard pixels to keep
threshold: confidence to use, i.e. if threshold is 0.7, y_true=1, prediction=0.65 then we consider that pixel as hard
# Returns
Mean loss value
"""
y_true = K.flatten(y_true)
y_pred = K.flatten(y_pred)
difference = K.abs(y_true - y_pred)
values, indices = K.tf.nn.top_k(difference, sorted=True, k=pixels)
min_difference = (1 - threshold)
y_true = K.tf.gather(K.gather(y_true, indices), K.tf.where(values > min_difference))
y_pred = K.tf.gather(K.gather(y_pred, indices), K.tf.where(values > min_difference))
return K.mean(K.binary_crossentropy(y_true, y_pred))
示例4: bce_border
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def bce_border(y_true, y_pred):
border = get_border_mask((21, 21), y_true)
border = K.flatten(border)
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
y_true_f = K.tf.gather(y_true_f, K.tf.where(border > 0.5))
y_pred_f = K.tf.gather(y_pred_f, K.tf.where(border > 0.5))
return binary_crossentropy(y_true_f, y_pred_f)
示例5: make_loss
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def make_loss(loss_name):
if loss_name == 'crossentropy':
return K.binary_crossentropy
elif loss_name == 'crossentropy_boot':
def loss(y, p):
return bootstrapped_crossentropy(y, p, 'hard', 0.9)
return loss
elif loss_name == 'dice':
return dice_coef_loss
elif loss_name == 'bce_dice':
def loss(y, p):
return dice_coef_loss_bce(y, p, dice=0.8, bce=0.2, bootstrapping='soft', alpha=1)
return loss
elif loss_name == 'boot_soft':
def loss(y, p):
return dice_coef_loss_bce(y, p, dice=0.8, bce=0.2, bootstrapping='soft', alpha=0.95)
return loss
elif loss_name == 'boot_hard':
def loss(y, p):
return dice_coef_loss_bce(y, p, dice=0.8, bce=0.2, bootstrapping='hard', alpha=0.95)
return loss
elif loss_name == 'online_bootstrapping':
def loss(y, p):
return online_bootstrapping(y, p, pixels=512 * 64, threshold=0.7)
return loss
elif loss_name == 'dice_coef_loss_border':
return dice_coef_loss_border
elif loss_name == 'bce_dice_loss_border':
return bce_dice_loss_border
else:
ValueError("Unknown loss.")
示例6: output_suggested_loss
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def output_suggested_loss(self):
self._check_output_support()
suggested_loss = losses.binary_crossentropy
return suggested_loss
示例7: bce_loss_graph
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def bce_loss_graph(gt, pr):
return K.mean(binary_crossentropy(gt, pr))
示例8: get_model
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def get_model():
nclass = 1
inp = Input(shape=(187, 1))
img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(inp)
img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(img_1)
img_1 = MaxPool1D(pool_size=2)(img_1)
img_1 = Dropout(rate=0.1)(img_1)
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1)
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1)
img_1 = MaxPool1D(pool_size=2)(img_1)
img_1 = Dropout(rate=0.1)(img_1)
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1)
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1)
img_1 = MaxPool1D(pool_size=2)(img_1)
img_1 = Dropout(rate=0.1)(img_1)
img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1)
img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1)
img_1 = GlobalMaxPool1D()(img_1)
img_1 = Dropout(rate=0.2)(img_1)
dense_1 = Dense(64, activation=activations.relu, name="dense_1")(img_1)
dense_1 = Dense(64, activation=activations.relu, name="dense_2")(dense_1)
dense_1 = Dense(nclass, activation=activations.sigmoid, name="dense_3_ptbdb")(dense_1)
model = models.Model(inputs=inp, outputs=dense_1)
opt = optimizers.Adam(0.001)
model.compile(optimizer=opt, loss=losses.binary_crossentropy, metrics=['acc'])
model.summary()
return model
示例9: get_model
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def get_model():
nclass = 1
inp = Input(shape=(187, 1))
img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid", trainable=False)(inp)
img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid", trainable=False)(img_1)
img_1 = MaxPool1D(pool_size=2)(img_1)
img_1 = Dropout(rate=0.1)(img_1)
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid", trainable=False)(img_1)
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid", trainable=False)(img_1)
img_1 = MaxPool1D(pool_size=2)(img_1)
img_1 = Dropout(rate=0.1)(img_1)
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid", trainable=False)(img_1)
img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid", trainable=False)(img_1)
img_1 = MaxPool1D(pool_size=2)(img_1)
img_1 = Dropout(rate=0.1)(img_1)
img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid", trainable=False)(img_1)
img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid", trainable=False)(img_1)
img_1 = GlobalMaxPool1D()(img_1)
img_1 = Dropout(rate=0.2)(img_1)
dense_1 = Dense(64, activation=activations.relu, name="dense_1")(img_1)
dense_1 = Dense(64, activation=activations.relu, name="dense_2")(dense_1)
dense_1 = Dense(nclass, activation=activations.sigmoid, name="dense_3_ptbdb")(dense_1)
model = models.Model(inputs=inp, outputs=dense_1)
opt = optimizers.Adam(0.001)
model.compile(optimizer=opt, loss=losses.binary_crossentropy, metrics=['acc'])
model.summary()
return model
示例10: test_single_pixel_measurement_index
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def test_single_pixel_measurement_index(self):
with self.test_session() as sess:
def test_different_input(sess, test_shape_height, test_shape_width):
random_x_true = np.random.randint(0, test_shape_width)
random_y_true = np.random.randint(0, test_shape_height)
random_x_false = np.random.randint(0, test_shape_width)
random_y_false = np.random.randint(0, test_shape_height)
test_true_np = np.array([[1, random_y_true, random_x_true],
[0, random_y_false, random_x_false]],
dtype=np.float32)
test_pred_np = np.zeros((2, test_shape_height, test_shape_width, 1), dtype=np.float32)
test_pred_np[0, random_y_true, random_x_true, 0] = 1.0
test_pred_np[0, random_y_false, random_x_false, 0] = 0.0
test_pred_tf = tf.convert_to_tensor(test_pred_np, tf.float32)
test_true_tf = tf.convert_to_tensor(test_true_np, tf.float32)
measure_tf_true = grasp_loss.segmentation_single_pixel_binary_crossentropy(test_true_tf, test_pred_np)
measure_tf_true = sess.run(measure_tf_true)
direct_call_result = binary_crossentropy(test_true_tf[:, :1], tf.constant([[1.0], [0.0]], tf.float32))
direct_call_result = sess.run(direct_call_result)
assert np.allclose(measure_tf_true, np.array([0.0], dtype=np.float32), atol=1e-06)
assert np.allclose(direct_call_result, measure_tf_true)
test_different_input(sess, 30, 20)
test_different_input(sess, 40, 50)
test_different_input(sess, 25, 30)
test_different_input(sess, 35, 35)
示例11: _makeModel
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def _makeModel(self, features, arm, gripper, arm_cmd, gripper_cmd, label,
example, *args, **kwargs):
img_shape = features.shape[1:]
arm_size = arm.shape[1]
if len(gripper.shape) > 1:
gripper_size = gripper.shape[1]
else:
gripper_size = 1
enc_ins, enc = GetEncoder(img_shape,
arm_size,
gripper_size,
self.generator_dim,
self.dropout_rate,
self.img_num_filters,
pre_tiling_layers=0,
post_tiling_layers=2,
discriminator=True)
dec_ins, dec = GetDecoder(self.generator_dim,
img_shape,
arm_size,
gripper_size,
dropout_rate=self.dropout_rate,
filters=self.img_num_filters,)
self.make([dec_ins, enc_ins], [dec, enc], loss="binary_crossentropy")
self.discriminator.trainable = False
self.generator.trainable = True
self.adversarial.summary()
self.discriminator.summary()
示例12: titanic
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def titanic():
# here use a standard 2d dictionary for inputting the param boundaries
p = {'lr': (0.5, 5, 10),
'first_neuron': [4, 8, 16],
'batch_size': [20, 30, 40],
'dropout': (0, 0.5, 5),
'optimizer': ['Adam', 'Nadam'],
'losses': ['logcosh', 'binary_crossentropy'],
'activation': ['relu', 'elu'],
'last_activation': ['sigmoid']}
return p
示例13: bce_dice_loss
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def bce_dice_loss(y_true, y_pred):
loss = binary_crossentropy(y_true, y_pred) + dice_loss(y_true, y_pred)
return loss
示例14: dummy_0_build_fn
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def dummy_0_build_fn(input_shape=(30,)):
model = Sequential(
[
Dense(50, kernel_initializer="uniform", input_shape=input_shape, activation="relu"),
Dropout(0.5),
Dense(1, kernel_initializer="uniform", activation="sigmoid"),
]
)
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
return model
示例15: elasticnet_bincross_loss_on_valid_joints
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import binary_crossentropy [as 别名]
def elasticnet_bincross_loss_on_valid_joints(y_true, y_pred):
idx = K.cast(K.greater(y_true, 0.), 'float32')
num_joints = K.clip(K.sum(idx, axis=(-1, -2)), 1, None)
l1 = K.abs(y_pred - y_true)
l2 = K.square(y_pred - y_true)
bc = 0.01*K.binary_crossentropy(y_true, y_pred)
dummy = 0. * y_pred
return K.sum(tf.where(K.cast(idx, 'bool'), l1 + l2 + bc, dummy),
axis=(-1, -2)) / num_joints