How to use a TF metric with a model?language modeling - model loss and accuracy not improving, model is underfittingTest Accuracy Increases Whilst Loss Increaseshow to randomly initialize weights in tensorflow?Tensorflow ReLu doesn't work?ANN regression model behaviourKeras metric based on output of an intermediate layerDoes tf.keras.layers.Conv2D as first layer in model truly need input_shape?MNIST Classification: mean_squared_error loss function and tanh activation functiontensorflow: distillation from resnet based model to VGG based modelMetrics values are equal while training and testing a model

When an imagined world resembles or has similarities with a famous world

How should I tell my manager I'm not paying for an optional after work event I'm not going to?

Prove that a definite integral is an infinite sum

Are sleeping system R-ratings additive?

Why is "breaking the mould" positively connoted?

Is it normal for gliders not to have attitude indicators?

Has the Hulk always been able to talk?

Can I use a Cat5e cable with an RJ45 and Cat6 port?

Why did WWI include Japan?

Should I mention being denied entry to UK due to a confusion in my Visa and Ticket bookings?

Why would a military not separate its forces into different branches?

How can Internet speed be 10 times slower without a router than when using the same connection with a router?

Why aren't nationalizations in Russia described as socialist?

Would you use "llamarse" for an animal's name?

As a GM, is it bad form to ask for a moment to think when improvising?

Is there a word for food that's gone 'bad', but is still edible?

Is 'contemporary' ambiguous and if so is there a better word?

To kill a cuckoo

Is an HNN extension of a virtually torsion-free group virtually torsion-free?

Install LibreOffice-Writer Only not LibreOffice whole package

Can there be a single technologically advanced nation, in a continent full of non-technologically advanced nations?

Handling Null values (and equivalents) routinely in Python

How do I calculate how many of an item I'll have in this inventory system?

Is the book wrong about the Nyquist Sampling Criterion?



How to use a TF metric with a model?


language modeling - model loss and accuracy not improving, model is underfittingTest Accuracy Increases Whilst Loss Increaseshow to randomly initialize weights in tensorflow?Tensorflow ReLu doesn't work?ANN regression model behaviourKeras metric based on output of an intermediate layerDoes tf.keras.layers.Conv2D as first layer in model truly need input_shape?MNIST Classification: mean_squared_error loss function and tanh activation functiontensorflow: distillation from resnet based model to VGG based modelMetrics values are equal while training and testing a model






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to compile a simple model and the following works just fine:



model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])


However, I would like to use this metric: tf.metrics.precision_at_k and I am not sure how I can get it to work. It requires two arguments and that is causing an issue as I don't know how to pass predictions as an argument. I tried a



model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=[tf.metrics.precision_at_k])


And certain variations but did not help.



Minimal working code:



import tensorflow as tf
from tensorflow.keras import layers
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu, input_shape=(5, 5, 1)),
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(2, activation=tf.nn.softmax)
])









share|improve this question

















  • 1





    Keras models only support metrics under tf.keras.metrics. For TensorFlow metrics you need to retrieve the tensors from the Keras model and use them.

    – Shubham Panchal
    Mar 23 at 1:57











  • "retrieve the tensors from the Keras model and use them" - Can you guide me on how this can be done?

    – ste_kwr
    Mar 23 at 2:01






  • 1





    For the output of the last layer : output _tensor = model.layers[last_layer_index].output

    – Shubham Panchal
    Mar 23 at 2:31











  • Thanks, this helps with the "retrieve" part. I'm also fairly clueless about the "use them" part. How do I construct an optimizer with this tensor?

    – ste_kwr
    Mar 23 at 2:35






  • 1





    The output_tensor could be used in tf.metrics to compute the desired metrics.

    – Shubham Panchal
    Mar 23 at 4:54

















0















I am trying to compile a simple model and the following works just fine:



model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])


However, I would like to use this metric: tf.metrics.precision_at_k and I am not sure how I can get it to work. It requires two arguments and that is causing an issue as I don't know how to pass predictions as an argument. I tried a



model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=[tf.metrics.precision_at_k])


And certain variations but did not help.



Minimal working code:



import tensorflow as tf
from tensorflow.keras import layers
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu, input_shape=(5, 5, 1)),
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(2, activation=tf.nn.softmax)
])









share|improve this question

















  • 1





    Keras models only support metrics under tf.keras.metrics. For TensorFlow metrics you need to retrieve the tensors from the Keras model and use them.

    – Shubham Panchal
    Mar 23 at 1:57











  • "retrieve the tensors from the Keras model and use them" - Can you guide me on how this can be done?

    – ste_kwr
    Mar 23 at 2:01






  • 1





    For the output of the last layer : output _tensor = model.layers[last_layer_index].output

    – Shubham Panchal
    Mar 23 at 2:31











  • Thanks, this helps with the "retrieve" part. I'm also fairly clueless about the "use them" part. How do I construct an optimizer with this tensor?

    – ste_kwr
    Mar 23 at 2:35






  • 1





    The output_tensor could be used in tf.metrics to compute the desired metrics.

    – Shubham Panchal
    Mar 23 at 4:54













0












0








0








I am trying to compile a simple model and the following works just fine:



model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])


However, I would like to use this metric: tf.metrics.precision_at_k and I am not sure how I can get it to work. It requires two arguments and that is causing an issue as I don't know how to pass predictions as an argument. I tried a



model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=[tf.metrics.precision_at_k])


And certain variations but did not help.



Minimal working code:



import tensorflow as tf
from tensorflow.keras import layers
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu, input_shape=(5, 5, 1)),
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(2, activation=tf.nn.softmax)
])









share|improve this question














I am trying to compile a simple model and the following works just fine:



model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])


However, I would like to use this metric: tf.metrics.precision_at_k and I am not sure how I can get it to work. It requires two arguments and that is causing an issue as I don't know how to pass predictions as an argument. I tried a



model.compile(optimizer=tf.train.AdamOptimizer(0.001),
loss='categorical_crossentropy',
metrics=[tf.metrics.precision_at_k])


And certain variations but did not help.



Minimal working code:



import tensorflow as tf
from tensorflow.keras import layers
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu, input_shape=(5, 5, 1)),
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(2, activation=tf.nn.softmax)
])






tensorflow






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 0:51









ste_kwrste_kwr

18511




18511







  • 1





    Keras models only support metrics under tf.keras.metrics. For TensorFlow metrics you need to retrieve the tensors from the Keras model and use them.

    – Shubham Panchal
    Mar 23 at 1:57











  • "retrieve the tensors from the Keras model and use them" - Can you guide me on how this can be done?

    – ste_kwr
    Mar 23 at 2:01






  • 1





    For the output of the last layer : output _tensor = model.layers[last_layer_index].output

    – Shubham Panchal
    Mar 23 at 2:31











  • Thanks, this helps with the "retrieve" part. I'm also fairly clueless about the "use them" part. How do I construct an optimizer with this tensor?

    – ste_kwr
    Mar 23 at 2:35






  • 1





    The output_tensor could be used in tf.metrics to compute the desired metrics.

    – Shubham Panchal
    Mar 23 at 4:54












  • 1





    Keras models only support metrics under tf.keras.metrics. For TensorFlow metrics you need to retrieve the tensors from the Keras model and use them.

    – Shubham Panchal
    Mar 23 at 1:57











  • "retrieve the tensors from the Keras model and use them" - Can you guide me on how this can be done?

    – ste_kwr
    Mar 23 at 2:01






  • 1





    For the output of the last layer : output _tensor = model.layers[last_layer_index].output

    – Shubham Panchal
    Mar 23 at 2:31











  • Thanks, this helps with the "retrieve" part. I'm also fairly clueless about the "use them" part. How do I construct an optimizer with this tensor?

    – ste_kwr
    Mar 23 at 2:35






  • 1





    The output_tensor could be used in tf.metrics to compute the desired metrics.

    – Shubham Panchal
    Mar 23 at 4:54







1




1





Keras models only support metrics under tf.keras.metrics. For TensorFlow metrics you need to retrieve the tensors from the Keras model and use them.

– Shubham Panchal
Mar 23 at 1:57





Keras models only support metrics under tf.keras.metrics. For TensorFlow metrics you need to retrieve the tensors from the Keras model and use them.

– Shubham Panchal
Mar 23 at 1:57













"retrieve the tensors from the Keras model and use them" - Can you guide me on how this can be done?

– ste_kwr
Mar 23 at 2:01





"retrieve the tensors from the Keras model and use them" - Can you guide me on how this can be done?

– ste_kwr
Mar 23 at 2:01




1




1





For the output of the last layer : output _tensor = model.layers[last_layer_index].output

– Shubham Panchal
Mar 23 at 2:31





For the output of the last layer : output _tensor = model.layers[last_layer_index].output

– Shubham Panchal
Mar 23 at 2:31













Thanks, this helps with the "retrieve" part. I'm also fairly clueless about the "use them" part. How do I construct an optimizer with this tensor?

– ste_kwr
Mar 23 at 2:35





Thanks, this helps with the "retrieve" part. I'm also fairly clueless about the "use them" part. How do I construct an optimizer with this tensor?

– ste_kwr
Mar 23 at 2:35




1




1





The output_tensor could be used in tf.metrics to compute the desired metrics.

– Shubham Panchal
Mar 23 at 4:54





The output_tensor could be used in tf.metrics to compute the desired metrics.

– Shubham Panchal
Mar 23 at 4:54












0






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',
autoActivateHeartbeat: false,
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%2f55309561%2fhow-to-use-a-tf-metric-with-a-model%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55309561%2fhow-to-use-a-tf-metric-with-a-model%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript