当前位置: 首页>>代码示例>>Python>>正文


Python objectives.categorical_crossentropy方法代码示例

本文整理汇总了Python中keras.objectives.categorical_crossentropy方法的典型用法代码示例。如果您正苦于以下问题:Python objectives.categorical_crossentropy方法的具体用法?Python objectives.categorical_crossentropy怎么用?Python objectives.categorical_crossentropy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在keras.objectives的用法示例。


在下文中一共展示了objectives.categorical_crossentropy方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: class_loss_cls

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def class_loss_cls(y_true, y_pred):
	return lambda_cls_class * K.mean(categorical_crossentropy(y_true[0, :, :], y_pred[0, :, :])) 
开发者ID:akshaylamba,项目名称:FasterRCNN_KERAS,代码行数:4,代码来源:losses.py

示例2: class_loss_cls

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def class_loss_cls(y_true, y_pred):
    return lambda_cls_class * K.mean(categorical_crossentropy(y_true[0, :, :], y_pred[0, :, :])) 
开发者ID:you359,项目名称:Keras-FasterRCNN,代码行数:4,代码来源:losses.py

示例3: loss_function

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def loss_function(self, y_true, y_pred):
        y_true_item = y_true[:, :self.n_classes]
        unlabeled_flag = y_true[:, self.n_classes]
        entropies = categorical_crossentropy(y_true_item, y_pred)
        coefs = 1.0-unlabeled_flag + self.alpha_t * unlabeled_flag # 1 if labeled, else alpha_t
        return coefs * entropies 
开发者ID:koshian2,项目名称:Pseudo-Label-Keras,代码行数:8,代码来源:mobilenet_pseudo_cifar.py

示例4: train

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def train(n_labeled_data):
    model = create_cnn()
    
    pseudo = PseudoCallback(model, n_labeled_data, min(512, n_labeled_data))

    # pretrain
    model.compile("adam", loss="categorical_crossentropy", metrics=["acc"])
    model.fit(pseudo.X_train_labeled/255.0, to_categorical(pseudo.y_train_labeled),
              batch_size=pseudo.batch_size, epochs=30,
              validation_data=(pseudo.X_test/255.0, to_categorical(pseudo.y_test)))
    pseudo.y_train_unlabeled_prediction = np.argmax(
            model.predict(pseudo.X_train_unlabeled), axis=-1,).reshape(-1, 1)

    #main-train
    model.compile("adam", loss=pseudo.loss_function, metrics=[pseudo.accuracy])

    if not os.path.exists("result_pseudo"):
        os.mkdir("result_pseudo")

    hist = model.fit_generator(pseudo.train_generator(), steps_per_epoch=pseudo.train_steps_per_epoch,
                               validation_data=pseudo.test_generator(), callbacks=[pseudo],
                               validation_steps=pseudo.test_stepes_per_epoch, epochs=100).history
    hist["labeled_accuracy"] = pseudo.labeled_accuracy
    hist["unlabeled_accuracy"] = pseudo.unlabeled_accuracy

    with open(f"result_pseudo/history_{n_labeled_data:05}.dat", "wb") as fp:
        pickle.dump(hist, fp) 
开发者ID:koshian2,项目名称:Pseudo-Label-Keras,代码行数:29,代码来源:pseudo_pretrain_cifar.py

示例5: compile_model

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def compile_model(self):
        self.model.compile(optimizer=args.optimizers,
                           loss=categorical_crossentropy,
                           metrics=args.metrics) 
开发者ID:yongzhuo,项目名称:nlp_xiaojiang,代码行数:6,代码来源:keras_bert_classify_bi_lstm.py

示例6: class_loss_cls

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def class_loss_cls(y_true, y_pred):
	return lambda_cls_class * categorical_crossentropy(y_true, y_pred) 
开发者ID:small-yellow-duck,项目名称:keras-frcnn,代码行数:4,代码来源:losses.py

示例7: create_model

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def create_model(env, args):
    h = x = Input(shape=(None,) + env.observation_space.shape, name="x")

    # policy network
    for i in range(args.layers):
        h = TimeDistributed(Dense(args.hidden_size, activation=args.activation), name="h%d" % (i + 1))(h)
    p = TimeDistributed(Dense(env.action_space.n, activation='softmax'), name="p")(h)

    # baseline network
    h = TimeDistributed(Dense(args.hidden_size, activation=args.activation), name="hb")(h)
    b = TimeDistributed(Dense(1), name="b")(h)

    # advantage is additional input
    A = Input(shape=(None,))

    # policy gradient loss and entropy bonus
    def policy_gradient_loss(l_sampled, l_predicted):
        return K.mean(A * categorical_crossentropy(l_sampled, l_predicted), axis=1) \
            - args.beta * K.mean(categorical_crossentropy(l_predicted, l_predicted), axis=1)

    # inputs to the model are observation and total reward,
    # outputs are action probabilities and baseline
    model = Model(input=[x, A], output=[p, b])

    # baseline is optimized with MSE
    model.compile(optimizer=args.optimizer, loss=[policy_gradient_loss, 'mse'])
    model.optimizer.lr = args.optimizer_lr

    return model 
开发者ID:tambetm,项目名称:gymexperiments,代码行数:31,代码来源:a2c.py

示例8: policy_gradient_loss

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def policy_gradient_loss(l_sampled, l_predicted):
    return A * categorical_crossentropy(l_sampled, l_predicted)[:, np.newaxis]

# inputs to the model are obesvation and advantage,
# outputs are action probabilities and baseline 
开发者ID:tambetm,项目名称:gymexperiments,代码行数:7,代码来源:pg.py

示例9: create_model

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def create_model(env, batch_size, num_steps):
    # network inputs are observations and advantages
    h = x = Input(batch_shape=(batch_size, num_steps) + env.observation_space.shape, name="x")
    A = Input(batch_shape=(batch_size, num_steps), name="A")

    # convolutional layers
    h = TimeDistributed(Convolution2D(32, 3, 3, subsample=(2, 2), border_mode="same", activation='elu', dim_ordering='tf'), name='c1')(h)
    h = TimeDistributed(Convolution2D(32, 3, 3, subsample=(2, 2), border_mode="same", activation='elu', dim_ordering='tf'), name='c2')(h)
    h = TimeDistributed(Convolution2D(32, 3, 3, subsample=(2, 2), border_mode="same", activation='elu', dim_ordering='tf'), name='c3')(h)
    h = TimeDistributed(Convolution2D(64, 3, 3, subsample=(2, 2), border_mode="same", activation='elu', dim_ordering='tf'), name='c4')(h)
    h = TimeDistributed(Flatten(), name="fl")(h)

    # recurrent layer
    h = LSTM(32, return_sequences=True, stateful=True, name="r1")(h)

    # policy network
    p = TimeDistributed(Dense(env.action_space.n, activation='softmax'), name="p")(h)

    # baseline network
    b = TimeDistributed(Dense(1), name="b")(h)

    # inputs to the model are observation and advantages,
    # outputs are action probabilities and baseline
    model = Model(input=[x, A], output=[p, b])

    # policy gradient loss and entropy bonus
    def policy_gradient_loss(l_sampled, l_predicted):
        return K.mean(A * categorical_crossentropy(l_sampled, l_predicted), axis=1) \
            - 0.01 * K.mean(categorical_crossentropy(l_predicted, l_predicted), axis=1)

    # baseline is optimized with MSE
    model.compile(optimizer='adam', loss=[policy_gradient_loss, 'mse'])

    return model 
开发者ID:tambetm,项目名称:gymexperiments,代码行数:36,代码来源:a2c_atari.py

示例10: vae_loss

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def vae_loss(y_true, y_pred):
    xent_loss = objectives.categorical_crossentropy(y_true, y_pred)
    kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var))
    loss = xent_loss + kl_loss
    return loss  

# create the vocabulary 
开发者ID:PacktPublishing,项目名称:Hands-On-Deep-Learning-for-Games,代码行数:9,代码来源:pitch-generator.py

示例11: vae_p_loss

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def vae_p_loss(y_true, y_pred):
    xent_loss = objectives.categorical_crossentropy(y_true, y_pred)
    kl_loss = - 0.5 * K.mean(1 + z_log_var_p - K.square(z_mean_p) - K.exp(z_log_var_p))
    loss = xent_loss + kl_loss
    return loss

# durations VAE loss 
开发者ID:PacktPublishing,项目名称:Hands-On-Deep-Learning-for-Games,代码行数:9,代码来源:note-generator.py

示例12: vae_d_loss

# 需要导入模块: from keras import objectives [as 别名]
# 或者: from keras.objectives import categorical_crossentropy [as 别名]
def vae_d_loss(y_true, y_pred):
    xent_loss = objectives.categorical_crossentropy(y_true, y_pred)
    kl_loss = - 0.5 * K.mean(1 + z_log_var_d - K.square(z_mean_d) - K.exp(z_log_var_d))
    loss = xent_loss + kl_loss
    return loss

# load Bach chorales 
开发者ID:PacktPublishing,项目名称:Hands-On-Deep-Learning-for-Games,代码行数:9,代码来源:note-generator.py


注:本文中的keras.objectives.categorical_crossentropy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。