【问题标题】:how is fix tf.contrib.metrics.cohen-kappa?如何修复 tf.contrib.metrics.cohen-kappa?
【发布时间】:2019-12-10 22:15:24
【问题描述】:

我正在尝试使用这个code,但它不起作用。

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)) 

错误

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.

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    您收到该错误是因为您没有在初始化 tf 变量的会话中调用损失函数。尝试这个。它应该工作:

    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))   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多