本文整理汇总了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, :, :]))
示例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, :, :]))
示例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
示例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)
示例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)
示例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)
示例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
示例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
示例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
示例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
示例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
示例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