我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用keras.objectives.categorical_crossentropy()。
def class_loss_cls(y_true, y_pred): return lambda_cls_class * K.mean(categorical_crossentropy(y_true[0, :, :], y_pred[0, :, :]))
def model_from_thumbnails(train_x, train_y, val_x, val_y): n_obs, n_channels, n_rows, n_cols = train_x.shape n_classes = y.shape[1] model = Sequential() model.add(Convolution2D(32, 2, 2, border_mode='valid', activation='relu', input_shape=(n_channels, n_rows, n_cols))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 2, 2, border_mode='valid', activation='relu')) model.add(Convolution2D(64, 2, 2, border_mode='valid', activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 2, 2, border_mode='valid', activation='relu')) model.add(Flatten()) model.add(Dropout(0.5)) model.add(Dense(100, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(100, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(n_classes, activation='softmax')) optimizer = Adam() model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) stopper = EarlyStopping(monitor='val_loss', patience=15, verbose=0, mode='auto') model.fit(train_x, train_y, shuffle=True, nb_epoch=100, validation_data=(val_x, val_y), callbacks = [stopper]) return model
def __init__(self, model_path, flip_map=None, gray_state=True, **kwargs): # load model model = load_model( model_path, custom_objects={'loss_fn': categorical_crossentropy} ) if flip_map is not None: assert model.output_shape[1] == len(flip_map) super(KerasAgent, self).__init__(n_actions=model.output_shape[1], **kwargs) self.gray_state = gray_state if len(model.input_shape) == 5: self.n_frames = model.input_shape[2] self.rnn = True else: self.n_frames = model.input_shape[1] self.rnn = False if not gray_state: self.n_frames /= 3 self.height, self.width = model.input_shape[2:] self.model = model self.flip_map = flip_map self.reset()
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
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