Python keras.losses.sparse_categorical_crossentropy() Examples
The following are 30
code examples of keras.losses.sparse_categorical_crossentropy().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keras.losses
, or try the search function
.
Example #1
Source File: models.py From EEG_classification with Apache License 2.0 | 6 votes |
def get_model_lstm(): nclass = 5 seq_input = Input(shape=(None, 3000, 1)) base_model = get_base_model() for layer in base_model.layers: layer.trainable = False encoded_sequence = TimeDistributed(base_model)(seq_input) encoded_sequence = Bidirectional(LSTM(100, return_sequences=True))(encoded_sequence) encoded_sequence = Dropout(rate=0.5)(encoded_sequence) encoded_sequence = Bidirectional(LSTM(100, return_sequences=True))(encoded_sequence) #out = TimeDistributed(Dense(nclass, activation="softmax"))(encoded_sequence) out = Convolution1D(nclass, kernel_size=1, activation="softmax", padding="same")(encoded_sequence) model = models.Model(seq_input, out) model.compile(optimizers.Adam(0.001), losses.sparse_categorical_crossentropy, metrics=['acc']) model.summary() return model
Example #2
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_sparse_categorical_crossentropy_4d(): y_pred = K.variable(np.array([[[[0.7, 0.1, 0.2], [0.0, 0.3, 0.7], [0.1, 0.1, 0.8]], [[0.3, 0.7, 0.0], [0.3, 0.4, 0.3], [0.2, 0.5, 0.3]], [[0.8, 0.1, 0.1], [1.0, 0.0, 0.0], [0.4, 0.3, 0.3]]]])) y_true = K.variable(np.array([[[0, 1, 0], [2, 1, 0], [2, 2, 1]]])) expected_loss = - (np.log(0.7) + np.log(0.3) + np.log(0.1) + np.log(K.epsilon()) + np.log(0.4) + np.log(0.2) + np.log(0.1) + np.log(K.epsilon()) + np.log(0.3)) / 9 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #3
Source File: train_audio.py From AudioEmotion with MIT License | 6 votes |
def train(): model = create_model() model.compile(optimizer='adam', loss=losses.sparse_categorical_crossentropy, metrics=['accuracy']) checkpointer = callbacks.ModelCheckpoint(filepath="../Output/checkpoint.hdf5", verbose=1, save_best_only=True) x_train, x_test, y_train, y_test = load_audio_data() model.fit(x_train, y_train, epochs=1000, batch_size=1000, validation_split=0.2, callbacks=[checkpointer]) results = model.evaluate(x_test, y_test) print('test_results: ', results) model.save(MODEL_FILE_PATH)
Example #4
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_sparse_categorical_crossentropy_4d(): y_pred = K.variable(np.array([[[[0.7, 0.1, 0.2], [0.0, 0.3, 0.7], [0.1, 0.1, 0.8]], [[0.3, 0.7, 0.0], [0.3, 0.4, 0.3], [0.2, 0.5, 0.3]], [[0.8, 0.1, 0.1], [1.0, 0.0, 0.0], [0.4, 0.3, 0.3]]]])) y_true = K.variable(np.array([[[0, 1, 0], [2, 1, 0], [2, 2, 1]]])) expected_loss = - (np.log(0.7) + np.log(0.3) + np.log(0.1) + np.log(K.epsilon()) + np.log(0.4) + np.log(0.2) + np.log(0.1) + np.log(K.epsilon()) + np.log(0.3)) / 9 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #5
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_sparse_categorical_crossentropy_4d(): y_pred = K.variable(np.array([[[[0.7, 0.1, 0.2], [0.0, 0.3, 0.7], [0.1, 0.1, 0.8]], [[0.3, 0.7, 0.0], [0.3, 0.4, 0.3], [0.2, 0.5, 0.3]], [[0.8, 0.1, 0.1], [1.0, 0.0, 0.0], [0.4, 0.3, 0.3]]]])) y_true = K.variable(np.array([[[0, 1, 0], [2, 1, 0], [2, 2, 1]]])) expected_loss = - (np.log(0.7) + np.log(0.3) + np.log(0.1) + np.log(K.epsilon()) + np.log(0.4) + np.log(0.2) + np.log(0.1) + np.log(K.epsilon()) + np.log(0.3)) / 9 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #6
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
def test_sparse_categorical_crossentropy_4d(): y_pred = K.variable(np.array([[[[0.7, 0.1, 0.2], [0.0, 0.3, 0.7], [0.1, 0.1, 0.8]], [[0.3, 0.7, 0.0], [0.3, 0.4, 0.3], [0.2, 0.5, 0.3]], [[0.8, 0.1, 0.1], [1.0, 0.0, 0.0], [0.4, 0.3, 0.3]]]])) y_true = K.variable(np.array([[[0, 1, 0], [2, 1, 0], [2, 2, 1]]])) expected_loss = - (np.log(0.7) + np.log(0.3) + np.log(0.1) + np.log(K.epsilon()) + np.log(0.4) + np.log(0.2) + np.log(0.1) + np.log(K.epsilon()) + np.log(0.3)) / 9 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #7
Source File: keras_bert_layer.py From nlp_xiaojiang with MIT License | 6 votes |
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
Example #8
Source File: crf_losses.py From keras-contrib with MIT License | 6 votes |
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)
Example #9
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_sparse_categorical_crossentropy(): y_pred = K.variable(np.array([[0.3, 0.6, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([1, 2])) expected_loss = - (np.log(0.6) + np.log(0.7)) / 2 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #10
Source File: models.py From EEG_classification with Apache License 2.0 | 5 votes |
def get_model(): nclass = 5 inp = Input(shape=(3000, 1)) img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(inp) img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(img_1) img_1 = MaxPool1D(pool_size=2)(img_1) img_1 = SpatialDropout1D(rate=0.01)(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = MaxPool1D(pool_size=2)(img_1) img_1 = SpatialDropout1D(rate=0.01)(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = MaxPool1D(pool_size=2)(img_1) img_1 = SpatialDropout1D(rate=0.01)(img_1) img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = GlobalMaxPool1D()(img_1) img_1 = Dropout(rate=0.01)(img_1) dense_1 = Dropout(rate=0.01)(Dense(64, activation=activations.relu, name="dense_1")(img_1)) dense_1 = Dropout(rate=0.05)(Dense(64, activation=activations.relu, name="dense_2")(dense_1)) dense_1 = Dense(nclass, activation=activations.softmax, name="dense_3")(dense_1) model = models.Model(inputs=inp, outputs=dense_1) opt = optimizers.Adam(0.001) model.compile(optimizer=opt, loss=losses.sparse_categorical_crossentropy, metrics=['acc']) model.summary() return model
Example #11
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = losses.sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(losses.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
Example #12
Source File: models.py From EEG_classification with Apache License 2.0 | 5 votes |
def get_base_model(): inp = Input(shape=(3000, 1)) img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(inp) img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(img_1) img_1 = MaxPool1D(pool_size=2)(img_1) img_1 = SpatialDropout1D(rate=0.01)(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = MaxPool1D(pool_size=2)(img_1) img_1 = SpatialDropout1D(rate=0.01)(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = MaxPool1D(pool_size=2)(img_1) img_1 = SpatialDropout1D(rate=0.01)(img_1) img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = GlobalMaxPool1D()(img_1) img_1 = Dropout(rate=0.01)(img_1) dense_1 = Dropout(0.01)(Dense(64, activation=activations.relu, name="dense_1")(img_1)) base_model = models.Model(inputs=inp, outputs=dense_1) opt = optimizers.Adam(0.001) base_model.compile(optimizer=opt, loss=losses.sparse_categorical_crossentropy, metrics=['acc']) #model.summary() return base_model
Example #13
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_sparse_categorical_crossentropy(): y_pred = K.variable(np.array([[0.3, 0.6, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([1, 2])) expected_loss = - (np.log(0.6) + np.log(0.7)) / 2 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #14
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = losses.sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(losses.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
Example #15
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_sparse_categorical_crossentropy(): y_pred = K.variable(np.array([[0.3, 0.6, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([1, 2])) expected_loss = - (np.log(0.6) + np.log(0.7)) / 2 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #16
Source File: models.py From EEG_classification with Apache License 2.0 | 5 votes |
def get_model_cnn(): nclass = 5 seq_input = Input(shape=(None, 3000, 1)) base_model = get_base_model() # for layer in base_model.layers: # layer.trainable = False encoded_sequence = TimeDistributed(base_model)(seq_input) encoded_sequence = SpatialDropout1D(rate=0.01)(Convolution1D(128, kernel_size=3, activation="relu", padding="same")(encoded_sequence)) encoded_sequence = Dropout(rate=0.05)(Convolution1D(128, kernel_size=3, activation="relu", padding="same")(encoded_sequence)) #out = TimeDistributed(Dense(nclass, activation="softmax"))(encoded_sequence) out = Convolution1D(nclass, kernel_size=3, activation="softmax", padding="same")(encoded_sequence) model = models.Model(seq_input, out) model.compile(optimizers.Adam(0.001), losses.sparse_categorical_crossentropy, metrics=['acc']) model.summary() return model
Example #17
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = losses.sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(losses.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
Example #18
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_sparse_categorical_crossentropy(): y_pred = K.variable(np.array([[0.3, 0.6, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([1, 2])) expected_loss = - (np.log(0.6) + np.log(0.7)) / 2 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #19
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_sparse_categorical_crossentropy(): y_pred = K.variable(np.array([[0.3, 0.6, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([1, 2])) expected_loss = - (np.log(0.6) + np.log(0.7)) / 2 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #20
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = losses.sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(losses.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
Example #21
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = losses.sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(losses.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
Example #22
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_sparse_categorical_crossentropy(): y_pred = K.variable(np.array([[0.3, 0.6, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([1, 2])) expected_loss = - (np.log(0.6) + np.log(0.7)) / 2 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #23
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = losses.sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(losses.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
Example #24
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_sparse_categorical_crossentropy(): y_pred = K.variable(np.array([[0.3, 0.6, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([1, 2])) expected_loss = - (np.log(0.6) + np.log(0.7)) / 2 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #25
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = losses.sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(losses.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
Example #26
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_sparse_categorical_crossentropy(): y_pred = K.variable(np.array([[0.3, 0.6, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([1, 2])) expected_loss = - (np.log(0.6) + np.log(0.7)) / 2 loss = K.eval(losses.sparse_categorical_crossentropy(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
Example #27
Source File: losses_test.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = losses.sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(losses.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
Example #28
Source File: baseline_mitbih.py From ECG_Heartbeat_Classification with MIT License | 5 votes |
def get_model(): nclass = 5 inp = Input(shape=(187, 1)) img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(inp) img_1 = Convolution1D(16, kernel_size=5, activation=activations.relu, padding="valid")(img_1) img_1 = MaxPool1D(pool_size=2)(img_1) img_1 = Dropout(rate=0.1)(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = MaxPool1D(pool_size=2)(img_1) img_1 = Dropout(rate=0.1)(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = Convolution1D(32, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = MaxPool1D(pool_size=2)(img_1) img_1 = Dropout(rate=0.1)(img_1) img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = Convolution1D(256, kernel_size=3, activation=activations.relu, padding="valid")(img_1) img_1 = GlobalMaxPool1D()(img_1) img_1 = Dropout(rate=0.2)(img_1) dense_1 = Dense(64, activation=activations.relu, name="dense_1")(img_1) dense_1 = Dense(64, activation=activations.relu, name="dense_2")(dense_1) dense_1 = Dense(nclass, activation=activations.softmax, name="dense_3_mitbih")(dense_1) model = models.Model(inputs=inp, outputs=dense_1) opt = optimizers.Adam(0.001) model.compile(optimizer=opt, loss=losses.sparse_categorical_crossentropy, metrics=['acc']) model.summary() return model
Example #29
Source File: dssim_test.py From keras-contrib with MIT License | 5 votes |
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
Example #30
Source File: Categorical.py From keras-pandas with MIT License | 5 votes |
def output_suggested_loss(self): self._check_output_support() suggested_loss = losses.sparse_categorical_crossentropy return suggested_loss