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)









share|improve this question

















  • 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











  • 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














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)









share|improve this question

















  • 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











  • 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












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)









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 13:22









Alexandr Kapshuk

31111




31111







  • 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











  • 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












  • 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











  • 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







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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ