Why do I have different results calculating True Positive rate in Keras Neural Network?
up vote
2
down vote
favorite
I'm training a neural network using Python Keras package. I care about the True Positive rate, so I added it to Callbacks and Metrics. Surprisingly, I'm getting different results using the same formula (Callbacks shows 81%, which is correct: I can see the same manually after I join Labels and Predictions; Metrics shows higher, around 86%). What is the matter? Any comment on the code is also appreciated
def sensitivity(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true, 0, 1)) * K.round(K.clip(y_pred, 0, 1)))
possible_positives = K.sum(y_true)
return true_positives / (possible_positives + K.epsilon())
....
def calculate_rates(model, data, label):
num_positive_prediction = np.sum(label)
prediction = np.round(np.clip(model.predict(data, batch_size = 1024)[:,1], 0, 1))
true_positive = np.sum(np.multiply(prediction, label)) / num_positive_prediction
return(true_positive)
....
class TestCallback(keras.callbacks.Callback):
def __init__(self, is_train, data, labels):
self.data = data
self.labels = labels[:,1]
self.is_train = is_train
def on_epoch_end(self, epoch, logs=):
true_positive = calculate_rates(model, self.data, self.labels)
if (epoch + 1) % 10 == 0 or epoch == 0:
if self.is_train:
print("Epoch: %d" % epoch + 1)
print("Training Set:")
else:
print("Testing Set:")
print("True Positive Rate: %4g" % true_positive)
....
model = keras.Sequential()
my_init = keras.initializers.RandomNormal(stddev=0.1)
model.add(keras.layers.Dense(units=200, activation='relu', input_dim=num_variables))
model.add(keras.layers.Dropout(dropout_rate))
model.add(keras.layers.Dense(units = 200, activation = 'relu', kernel_initializer=my_init, bias_initializer=my_init))
model.add(keras.layers.Dropout(dropout_rate))
model.add(keras.layers.Dense(units=num_outputs, activation='softmax', kernel_initializer=my_init, bias_initializer=my_init))
model.compile(loss='binary_crossentropy',
optimizer=keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-8),
metrics=[sensitivity]
)
history = model.fit(train_data, train_labels, epochs=num_epochs, batch_size=1024, class_weight=0: 1, 1: weight,
callbacks=[TestCallback(1, train_data, train_labels), TestCallback(0, test_data, test_labels)], verbose=1)
python callback keras neural-network metrics
add a comment |
up vote
2
down vote
favorite
I'm training a neural network using Python Keras package. I care about the True Positive rate, so I added it to Callbacks and Metrics. Surprisingly, I'm getting different results using the same formula (Callbacks shows 81%, which is correct: I can see the same manually after I join Labels and Predictions; Metrics shows higher, around 86%). What is the matter? Any comment on the code is also appreciated
def sensitivity(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true, 0, 1)) * K.round(K.clip(y_pred, 0, 1)))
possible_positives = K.sum(y_true)
return true_positives / (possible_positives + K.epsilon())
....
def calculate_rates(model, data, label):
num_positive_prediction = np.sum(label)
prediction = np.round(np.clip(model.predict(data, batch_size = 1024)[:,1], 0, 1))
true_positive = np.sum(np.multiply(prediction, label)) / num_positive_prediction
return(true_positive)
....
class TestCallback(keras.callbacks.Callback):
def __init__(self, is_train, data, labels):
self.data = data
self.labels = labels[:,1]
self.is_train = is_train
def on_epoch_end(self, epoch, logs=):
true_positive = calculate_rates(model, self.data, self.labels)
if (epoch + 1) % 10 == 0 or epoch == 0:
if self.is_train:
print("Epoch: %d" % epoch + 1)
print("Training Set:")
else:
print("Testing Set:")
print("True Positive Rate: %4g" % true_positive)
....
model = keras.Sequential()
my_init = keras.initializers.RandomNormal(stddev=0.1)
model.add(keras.layers.Dense(units=200, activation='relu', input_dim=num_variables))
model.add(keras.layers.Dropout(dropout_rate))
model.add(keras.layers.Dense(units = 200, activation = 'relu', kernel_initializer=my_init, bias_initializer=my_init))
model.add(keras.layers.Dropout(dropout_rate))
model.add(keras.layers.Dense(units=num_outputs, activation='softmax', kernel_initializer=my_init, bias_initializer=my_init))
model.compile(loss='binary_crossentropy',
optimizer=keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-8),
metrics=[sensitivity]
)
history = model.fit(train_data, train_labels, epochs=num_epochs, batch_size=1024, class_weight=0: 1, 1: weight,
callbacks=[TestCallback(1, train_data, train_labels), TestCallback(0, test_data, test_labels)], verbose=1)
python callback keras neural-network metrics
1
It might be becausesensitivityis computed for a single batch of samples, whereascalculate_ratesis computed for the full dataset.
– rvinas
Nov 8 at 16:38
The batch size is definitely affecting thesensitivityfunction: when I use values close to the dataset dimension, the two function give similar results. I found one more:y_trueandy_predare nx2 matrices, so I should usey_true[:,1]and[y_pred[:,1]instead.
– Alexandr Kapshuk
yesterday
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm training a neural network using Python Keras package. I care about the True Positive rate, so I added it to Callbacks and Metrics. Surprisingly, I'm getting different results using the same formula (Callbacks shows 81%, which is correct: I can see the same manually after I join Labels and Predictions; Metrics shows higher, around 86%). What is the matter? Any comment on the code is also appreciated
def sensitivity(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true, 0, 1)) * K.round(K.clip(y_pred, 0, 1)))
possible_positives = K.sum(y_true)
return true_positives / (possible_positives + K.epsilon())
....
def calculate_rates(model, data, label):
num_positive_prediction = np.sum(label)
prediction = np.round(np.clip(model.predict(data, batch_size = 1024)[:,1], 0, 1))
true_positive = np.sum(np.multiply(prediction, label)) / num_positive_prediction
return(true_positive)
....
class TestCallback(keras.callbacks.Callback):
def __init__(self, is_train, data, labels):
self.data = data
self.labels = labels[:,1]
self.is_train = is_train
def on_epoch_end(self, epoch, logs=):
true_positive = calculate_rates(model, self.data, self.labels)
if (epoch + 1) % 10 == 0 or epoch == 0:
if self.is_train:
print("Epoch: %d" % epoch + 1)
print("Training Set:")
else:
print("Testing Set:")
print("True Positive Rate: %4g" % true_positive)
....
model = keras.Sequential()
my_init = keras.initializers.RandomNormal(stddev=0.1)
model.add(keras.layers.Dense(units=200, activation='relu', input_dim=num_variables))
model.add(keras.layers.Dropout(dropout_rate))
model.add(keras.layers.Dense(units = 200, activation = 'relu', kernel_initializer=my_init, bias_initializer=my_init))
model.add(keras.layers.Dropout(dropout_rate))
model.add(keras.layers.Dense(units=num_outputs, activation='softmax', kernel_initializer=my_init, bias_initializer=my_init))
model.compile(loss='binary_crossentropy',
optimizer=keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-8),
metrics=[sensitivity]
)
history = model.fit(train_data, train_labels, epochs=num_epochs, batch_size=1024, class_weight=0: 1, 1: weight,
callbacks=[TestCallback(1, train_data, train_labels), TestCallback(0, test_data, test_labels)], verbose=1)
python callback keras neural-network metrics
I'm training a neural network using Python Keras package. I care about the True Positive rate, so I added it to Callbacks and Metrics. Surprisingly, I'm getting different results using the same formula (Callbacks shows 81%, which is correct: I can see the same manually after I join Labels and Predictions; Metrics shows higher, around 86%). What is the matter? Any comment on the code is also appreciated
def sensitivity(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true, 0, 1)) * K.round(K.clip(y_pred, 0, 1)))
possible_positives = K.sum(y_true)
return true_positives / (possible_positives + K.epsilon())
....
def calculate_rates(model, data, label):
num_positive_prediction = np.sum(label)
prediction = np.round(np.clip(model.predict(data, batch_size = 1024)[:,1], 0, 1))
true_positive = np.sum(np.multiply(prediction, label)) / num_positive_prediction
return(true_positive)
....
class TestCallback(keras.callbacks.Callback):
def __init__(self, is_train, data, labels):
self.data = data
self.labels = labels[:,1]
self.is_train = is_train
def on_epoch_end(self, epoch, logs=):
true_positive = calculate_rates(model, self.data, self.labels)
if (epoch + 1) % 10 == 0 or epoch == 0:
if self.is_train:
print("Epoch: %d" % epoch + 1)
print("Training Set:")
else:
print("Testing Set:")
print("True Positive Rate: %4g" % true_positive)
....
model = keras.Sequential()
my_init = keras.initializers.RandomNormal(stddev=0.1)
model.add(keras.layers.Dense(units=200, activation='relu', input_dim=num_variables))
model.add(keras.layers.Dropout(dropout_rate))
model.add(keras.layers.Dense(units = 200, activation = 'relu', kernel_initializer=my_init, bias_initializer=my_init))
model.add(keras.layers.Dropout(dropout_rate))
model.add(keras.layers.Dense(units=num_outputs, activation='softmax', kernel_initializer=my_init, bias_initializer=my_init))
model.compile(loss='binary_crossentropy',
optimizer=keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-8),
metrics=[sensitivity]
)
history = model.fit(train_data, train_labels, epochs=num_epochs, batch_size=1024, class_weight=0: 1, 1: weight,
callbacks=[TestCallback(1, train_data, train_labels), TestCallback(0, test_data, test_labels)], verbose=1)
python callback keras neural-network metrics
python callback keras neural-network metrics
asked Nov 8 at 13:22
Alexandr Kapshuk
31111
31111
1
It might be becausesensitivityis computed for a single batch of samples, whereascalculate_ratesis computed for the full dataset.
– rvinas
Nov 8 at 16:38
The batch size is definitely affecting thesensitivityfunction: when I use values close to the dataset dimension, the two function give similar results. I found one more:y_trueandy_predare nx2 matrices, so I should usey_true[:,1]and[y_pred[:,1]instead.
– Alexandr Kapshuk
yesterday
add a comment |
1
It might be becausesensitivityis computed for a single batch of samples, whereascalculate_ratesis computed for the full dataset.
– rvinas
Nov 8 at 16:38
The batch size is definitely affecting thesensitivityfunction: when I use values close to the dataset dimension, the two function give similar results. I found one more:y_trueandy_predare nx2 matrices, so I should usey_true[:,1]and[y_pred[:,1]instead.
– Alexandr Kapshuk
yesterday
1
1
It might be because
sensitivity is computed for a single batch of samples, whereas calculate_rates is computed for the full dataset.– rvinas
Nov 8 at 16:38
It might be because
sensitivity is computed for a single batch of samples, whereas calculate_rates is computed for the full dataset.– rvinas
Nov 8 at 16:38
The batch size is definitely affecting the
sensitivity function: when I use values close to the dataset dimension, the two function give similar results. I found one more: y_true and y_pred are nx2 matrices, so I should use y_true[:,1] and [y_pred[:,1] instead.– Alexandr Kapshuk
yesterday
The batch size is definitely affecting the
sensitivity function: when I use values close to the dataset dimension, the two function give similar results. I found one more: y_true and y_pred are nx2 matrices, so I should use y_true[:,1] and [y_pred[:,1] instead.– Alexandr Kapshuk
yesterday
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208646%2fwhy-do-i-have-different-results-calculating-true-positive-rate-in-keras-neural-n%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It might be because
sensitivityis computed for a single batch of samples, whereascalculate_ratesis computed for the full dataset.– rvinas
Nov 8 at 16:38
The batch size is definitely affecting the
sensitivityfunction: when I use values close to the dataset dimension, the two function give similar results. I found one more:y_trueandy_predare nx2 matrices, so I should usey_true[:,1]and[y_pred[:,1]instead.– Alexandr Kapshuk
yesterday