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