首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修正tf.contrib.emeics.cohen-kappa?

如何修正tf.contrib.emeics.cohen-kappa?
EN

Stack Overflow用户
提问于 2019-08-02 09:43:20
回答 1查看 274关注 0票数 0

我正试着用这个code做实验,但它不起作用。

代码语言:javascript
复制
import keras.backend as K
import tensorflow as tf
def _cohen_kappa(y_true, y_pred, num_classes, weights=None, metrics_collections=None, updates_collections=None, name=None):
    kappa, update_op = tf.contrib.metrics.cohen_kappa(y_true, y_pred, num_classes, weights, metrics_collections, updates_collections, name)
    K.get_session().run(tf.local_variables_initializer())
    with tf.control_dependencies([update_op]):
        kappa = tf.identity(kappa)
    return kappa

labels = tf.constant([1,0,0,1],name = 'labels')
predictions_idx = tf.constant([1,0,0,1],name = 'predictions_idx')

init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())


loss = _cohen_kappa(labels,predictions_idx,2)

with tf.Session() as session:                    # Create a session and print the output
    session.run(init)                            # Initializes the variables
    print(session.run(loss)) 

误差

代码语言:javascript
复制
FailedPreconditionError: 2 root error(s) found.
  (0) Failed precondition: Attempting to use uninitialized value cohen_kappa_1/po
     [[{{node cohen_kappa_1/po/read}}]]
     [[cohen_kappa_1/counts_in_table/Cast_3/_5]]
  (1) Failed precondition: Attempting to use uninitialized value cohen_kappa_1/po
     [[{{node cohen_kappa_1/po/read}}]]
0 successful operations.
0 derived errors ignored.
EN

回答 1

Stack Overflow用户

发布于 2019-08-09 03:26:12

您将得到该错误,因为您没有在初始化tf变量的会话中调用丢失函数。尝尝这个。它应该能发挥作用:

代码语言:javascript
复制
import keras.backend as K
import tensorflow as tf
def _cohen_kappa(y_true, y_pred, num_classes, weights=None, metrics_collections=None, updates_collections=None, name=None):
    kappa, update_op = tf.contrib.metrics.cohen_kappa(y_true, y_pred, num_classes, weights, metrics_collections, updates_collections, name)
    K.get_session().run(tf.local_variables_initializer())
    with tf.control_dependencies([update_op]):
        kappa = tf.identity(kappa)
    return kappa

labels = tf.constant([1,0,0,1],name = 'labels')
predictions_idx = tf.constant([1,0,0,1],name = 'predictions_idx')

with tf.Session() as session:                # Create a session and print the output
    loss = _cohen_kappa(labels,predictions_idx,2)
    init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    session.run(init)                            # Initializes the variables
    print(session.run(loss))   
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57323866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档