當前位置: 首頁>>代碼示例>>Python>>正文


Python losses.categorical_crossentropy方法代碼示例

本文整理匯總了Python中keras.losses.categorical_crossentropy方法的典型用法代碼示例。如果您正苦於以下問題:Python losses.categorical_crossentropy方法的具體用法?Python losses.categorical_crossentropy怎麽用?Python losses.categorical_crossentropy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在keras.losses的用法示例。


在下文中一共展示了losses.categorical_crossentropy方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: nn_model

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def nn_model():
    (x_train, y_train), _ = mnist.load_data()
    # 歸一化
    x_train = x_train.reshape(x_train.shape[0], -1) / 255.
    # one-hot
    y_train = np_utils.to_categorical(y=y_train, num_classes=10)
    # constant(value=1.)自定義常數,constant(value=1.)===one()
    # 創建模型:輸入784個神經元,輸出10個神經元
    model = Sequential([
        Dense(units=200, input_dim=784, bias_initializer=constant(value=1.), activation=tanh),
        Dense(units=100, bias_initializer=one(), activation=tanh),
        Dense(units=10, bias_initializer=one(), activation=softmax),
    ])

    opt = SGD(lr=0.2, clipnorm=1.)  # 優化器
    model.compile(optimizer=opt, loss=categorical_crossentropy, metrics=['acc', 'mae'])  # 編譯
    model.fit(x_train, y_train, batch_size=64, epochs=20, callbacks=[RemoteMonitor()])
    model_save(model, './model.h5') 
開發者ID:jtyoui,項目名稱:Jtyoui,代碼行數:20,代碼來源:HandWritingRecognition.py

示例2: get_model_compiled

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def get_model_compiled(shapeinput, num_class, w_decay=0, lr=1e-3):
    clf = Sequential()
    clf.add(Conv3D(32, kernel_size=(5, 5, 24), input_shape=shapeinput))
    clf.add(BatchNormalization())
    clf.add(Activation('relu'))
    clf.add(Conv3D(64, (5, 5, 16)))
    clf.add(BatchNormalization())
    clf.add(Activation('relu'))
    clf.add(MaxPooling3D(pool_size=(2, 2, 1)))
    clf.add(Flatten())
    clf.add(Dense(300, kernel_regularizer=regularizers.l2(w_decay)))
    clf.add(BatchNormalization())
    clf.add(Activation('relu'))
    clf.add(Dense(num_class, activation='softmax'))
    clf.compile(loss=categorical_crossentropy, optimizer=Adam(lr=lr), metrics=['accuracy'])
    return clf 
開發者ID:mhaut,項目名稱:hyperspectral_deeplearning_review,代碼行數:18,代碼來源:cnn3d.py

示例3: crf_loss

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def crf_loss(y_true, y_pred):
    """General CRF loss function depending on the learning mode.

    # Arguments
        y_true: tensor with true targets.
        y_pred: tensor with predicted targets.

    # Returns
        If the CRF layer is being trained in the join mode, returns the negative
        log-likelihood. Otherwise returns the categorical crossentropy implemented
        by the underlying Keras backend.

    # About GitHub
        If you open an issue or a pull request about CRF, please
        add `cc @lzfelix` to notify Luiz Felix.
    """
    crf, idx = y_pred._keras_history[:2]
    if crf.learn_mode == 'join':
        return crf_nll(y_true, y_pred)
    else:
        if crf.sparse_target:
            return sparse_categorical_crossentropy(y_true, y_pred)
        else:
            return categorical_crossentropy(y_true, y_pred) 
開發者ID:keras-team,項目名稱:keras-contrib,代碼行數:26,代碼來源:crf_losses.py

示例4: crf_loss

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def crf_loss(y_true, y_pred):
    """General CRF loss function depending on the learning mode.
    # Arguments
        y_true: tensor with true targets.
        y_pred: tensor with predicted targets.
    # Returns
        If the CRF layer is being trained in the join mode, returns the negative
        log-likelihood. Otherwise returns the categorical crossentropy implemented
        by the underlying Keras backend.
    # About GitHub
        If you open an issue or a pull request about CRF, please
        add `cc @lzfelix` to notify Luiz Felix.
    """
    crf, idx = y_pred._keras_history[:2]
    if crf.learn_mode == 'join':
        return crf_nll(y_true, y_pred)
    else:
        if crf.sparse_target:
            return sparse_categorical_crossentropy(y_true, y_pred)
        else:
            return categorical_crossentropy(y_true, y_pred)

# crf_marginal_accuracy, crf_viterbi_accuracy 
開發者ID:yongzhuo,項目名稱:nlp_xiaojiang,代碼行數:25,代碼來源:keras_bert_layer.py

示例5: iris

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def iris():

    from keras.optimizers import Adam, Nadam
    from keras.losses import logcosh, categorical_crossentropy
    from keras.activations import relu, elu, softmax

    # here use a standard 2d dictionary for inputting the param boundaries
    p = {'lr': (0.5, 5, 10),
         'first_neuron': [4, 8, 16, 32, 64],
         'hidden_layers': [0, 1, 2, 3, 4],
         'batch_size': (2, 30, 10),
         'epochs': [2],
         'dropout': (0, 0.5, 5),
         'weight_regulizer': [None],
         'emb_output_dims':  [None],
         'shapes': ['brick', 'triangle', 0.2],
         'optimizer': [Adam, Nadam],
         'losses': [logcosh, categorical_crossentropy],
         'activation': [relu, elu],
         'last_activation': [softmax]}

    return p 
開發者ID:autonomio,項目名稱:talos,代碼行數:24,代碼來源:params.py

示例6: create_model

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def create_model(input_shape: tuple, nb_classes: int, init_with_imagenet: bool = False, learning_rate: float = 0.01):
    weights = None
    if init_with_imagenet:
        weights = "imagenet"

    model = VGG16(input_shape=input_shape,
                  classes=nb_classes,
                  weights=weights,
                  include_top=False)
    # "Shallow" VGG for Cifar10
    x = model.get_layer('block3_pool').output
    x = layers.Flatten(name='Flatten')(x)
    x = layers.Dense(512, activation='relu')(x)
    x = layers.Dense(nb_classes)(x)
    x = layers.Softmax()(x)
    model = models.Model(model.input, x)

    loss = losses.categorical_crossentropy
    optimizer = optimizers.SGD(lr=learning_rate, decay=0.99)

    model.compile(optimizer, loss, metrics=["accuracy"])
    return model 
開發者ID:gaborvecsei,項目名稱:Federated-Learning-Mini-Framework,代碼行數:24,代碼來源:models.py

示例7: _init_model

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def _init_model(self):
        """
        Initialize base model from Keras and add top layers to match number of training emotions labels.
        :return:
        """
        base_model = self._get_base_model()

        top_layer_model = base_model.output
        top_layer_model = GlobalAveragePooling2D()(top_layer_model)
        top_layer_model = Dense(1024, activation='relu')(top_layer_model)
        prediction_layer = Dense(output_dim=len(self.emotion_map.keys()), activation='softmax')(top_layer_model)

        model = Model(input=base_model.input, output=prediction_layer)
        print(model.summary())
        for layer in base_model.layers:
            layer.trainable = False
        model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

        self.model = model 
開發者ID:thoughtworksarts,項目名稱:EmoPy,代碼行數:21,代碼來源:neuralnets.py

示例8: fit

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def fit(self, features, labels, validation_split, epochs=50):
        """
        Trains the neural net on the data provided.

        :param features: Numpy array of training data.
        :param labels: Numpy array of target (label) data.
        :param validation_split: Float between 0 and 1. Percentage of training data to use for validation
        :param epochs: Max number of times to train over dataset.
        """
        self.model.fit(x=features, y=labels, epochs=epochs, verbose=1,
                       callbacks=[ReduceLROnPlateau(), EarlyStopping(patience=3)], validation_split=validation_split,
                       shuffle=True)

        for layer in self.model.layers[:self._NUM_BOTTOM_LAYERS_TO_RETRAIN]:
            layer.trainable = False
        for layer in self.model.layers[self._NUM_BOTTOM_LAYERS_TO_RETRAIN:]:
            layer.trainable = True

        self.model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
        self.model.fit(x=features, y=labels, epochs=50, verbose=1,
                       callbacks=[ReduceLROnPlateau(), EarlyStopping(patience=3)], validation_split=validation_split,
                       shuffle=True) 
開發者ID:thoughtworksarts,項目名稱:EmoPy,代碼行數:24,代碼來源:neuralnets.py

示例9: cnn_model

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def cnn_model():
    (x_train, y_train), _ = mnist.load_data()
    # 歸一化
    x_train = x_train.reshape(-1, 28, 28, 1) / 255.
    # one-hot
    y_train = np_utils.to_categorical(y=y_train, num_classes=10)

    model = Sequential([
        # input_shape:輸入平麵,就在第一個位置設置
        # filters:卷積核、濾波器
        # kernel_size:卷積核大小
        # strides:步長
        # padding有兩種方式:same/valid
        # activation:激活函數
        Convolution2D(input_shape=(28, 28, 1), filters=32, kernel_size=5, strides=1, padding='same', activation=relu),
        MaxPool2D(pool_size=2, strides=2, padding='same'),
        Convolution2D(filters=64, kernel_size=5, padding='same', activation=relu),
        MaxPool2D(pool_size=2, trainable=2, padding='same'),
        Flatten(),  # 扁平化
        Dense(units=1024, activation=relu),
        Dropout(0.5),
        Dense(units=10, activation=softmax),
    ])
    opt = Adam(lr=1e-4)
    model.compile(optimizer=opt, loss=categorical_crossentropy, metrics=['accuracy'])
    model.fit(x=x_train, y=y_train, batch_size=64, epochs=20, callbacks=[RemoteMonitor()])
    model_save(model, './model.h5') 
開發者ID:jtyoui,項目名稱:Jtyoui,代碼行數:29,代碼來源:HandWritingRecognition.py

示例10: rnn_model

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def rnn_model():
    (x_train, y_train), _ = mnist.load_data()
    # 歸一化
    x_train = x_train / 255.
    # one-hot
    y_train = np_utils.to_categorical(y=y_train, num_classes=10)

    model = Sequential([
        SimpleRNN(units=50, input_shape=(28, 28)),
        Dense(units=10, activation=softmax),
    ])
    opt = RMSprop(lr=1e-4)
    model.compile(optimizer=opt, loss=categorical_crossentropy, metrics=['accuracy'])
    model.fit(x=x_train, y=y_train, batch_size=64, epochs=20, callbacks=[RemoteMonitor()])
    model_save(model, './model.h5') 
開發者ID:jtyoui,項目名稱:Jtyoui,代碼行數:17,代碼來源:HandWritingRecognition.py

示例11: masked_categorical_crossentropy

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def masked_categorical_crossentropy(gt, pr):
    from keras.losses import categorical_crossentropy
    mask = 1 - gt[:, :, 0]
    return categorical_crossentropy(gt, pr) * mask 
開發者ID:divamgupta,項目名稱:image-segmentation-keras,代碼行數:6,代碼來源:train.py

示例12: test_functional_model_saving

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def test_functional_model_saving():
    img_rows, img_cols = 32, 32
    img_channels = 3

    # Parameters for the DenseNet model builder
    img_dim = (img_channels, img_rows, img_cols) if K.image_data_format() == 'channels_first' else (
        img_rows, img_cols, img_channels)
    depth = 40
    nb_dense_block = 3
    growth_rate = 3  # number of z2 maps equals growth_rate * group_size, so keep this small.
    nb_filter = 16
    dropout_rate = 0.0  # 0.0 for data augmentation
    conv_group = 'D4'  # C4 includes 90 degree rotations, D4 additionally includes reflections in x and y axis.
    use_gcnn = True

    # Create the model (without loading weights)
    model = GDenseNet(mc_dropout=False, padding='same', nb_dense_block=nb_dense_block, growth_rate=growth_rate,
                      nb_filter=nb_filter, dropout_rate=dropout_rate, weights=None, input_shape=img_dim, depth=depth,
                      use_gcnn=use_gcnn, conv_group=conv_group)
    model.compile(loss=losses.categorical_crossentropy,
                  optimizer=optimizers.Adam(),
                  metrics=[metrics.categorical_accuracy])
    x = np.random.random((1, 32, 32, 3))
    y = np.random.randint(0, 10, 1)
    y = np_utils.to_categorical(y, 10)
    model.train_on_batch(x, y)

    out = model.predict(x)
    _, fname = tempfile.mkstemp('.h5')
    save_model(model, fname)

    model = load_model(fname)
    os.remove(fname)

    out2 = model.predict(x)
    assert_allclose(out, out2, atol=1e-05) 
開發者ID:basveeling,項目名稱:keras-gcnn,代碼行數:38,代碼來源:test_model_saving.py

示例13: get_model_compiled

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def get_model_compiled(shapeinput, num_class, w_decay=0):
    clf = Sequential()
    clf.add(Conv2D(50, kernel_size=(5, 5), input_shape=shapeinput))
    clf.add(Activation('relu'))
    clf.add(Conv2D(100, (5, 5)))
    clf.add(Activation('relu'))
    clf.add(MaxPooling2D(pool_size=(2, 2)))
    clf.add(Flatten())
    clf.add(Dense(100, kernel_regularizer=regularizers.l2(w_decay)))
    clf.add(Activation('relu'))
    clf.add(Dense(num_class, activation='softmax'))
    clf.compile(loss=categorical_crossentropy, optimizer=Adam(), metrics=['accuracy'])
    return clf 
開發者ID:mhaut,項目名稱:hyperspectral_deeplearning_review,代碼行數:15,代碼來源:cnn2d.py

示例14: get_model_compiled

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def get_model_compiled(bands, num_class):
    clf = Sequential()
    clf.add(Conv1D(20, (24), activation='relu', input_shape=(bands,1)))
    clf.add(MaxPooling1D(pool_size=5))
    clf.add(Flatten())
    clf.add(Dense(100))
    clf.add(BatchNormalization())
    clf.add(Activation('relu'))
    clf.add(Dense(num_class, activation='softmax'))
    clf.compile(loss=categorical_crossentropy, optimizer=Adam(), metrics=['accuracy'])
    return clf 
開發者ID:mhaut,項目名稱:hyperspectral_deeplearning_review,代碼行數:13,代碼來源:cnn1d.py

示例15: get_model_compiled

# 需要導入模塊: from keras import losses [as 別名]
# 或者: from keras.losses import categorical_crossentropy [as 別名]
def get_model_compiled(feat_size, seq_len, num_class, type_func):
    if type_func == "RNN": func = SimpleRNN
    elif type_func == "GRU": func = CuDNNGRU
    elif type_func == "LSTM": func = CuDNNLSTM
    else: print("NOT RECURRENT FUNC")
    clf = Sequential()
    clf.add(func(64, return_sequences=True, input_shape=(feat_size, seq_len)))
    clf.add(func(64, return_sequences=True))
    clf.add(Flatten())
    clf.add(Dense(num_class, activation='softmax'))
    clf.compile(loss=categorical_crossentropy, optimizer=Adam(), metrics=['accuracy'])
    return clf 
開發者ID:mhaut,項目名稱:hyperspectral_deeplearning_review,代碼行數:14,代碼來源:recurrent.py


注:本文中的keras.losses.categorical_crossentropy方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。