【问题标题】:How can I use tensorflow metric function within keras models?如何在 keras 模型中使用 tensorflow 度量函数?
【发布时间】:2017-04-01 15:34:27
【问题描述】:

使用 python 3.5.2 tensorflow rc 1.1

我正在尝试在 keras 中使用 tensorflow 度量函数。需要的函数接口好像是一样的,但是调用:

import pandas
import numpy
import tensorflow.contrib.keras as keras
import tensorflow


def target_function(row):
    return float(row[0] - row[1] < 0.5)

df = pandas.DataFrame(numpy.random.rand(10000,2))
label = df.apply(target_function, axis=1)

input_layer = keras.layers.Input(shape=(2,))
net = keras.layers.Dense(1)(input_layer)

model = keras.models.Model(inputs=[input_layer], outputs=[net])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[tensorflow.metrics.auc])

model.fit(df.as_matrix(), label.as_matrix(), epochs=10, validation_split=0.2, batch_size=100)

错误结果:

Using TensorFlow backend.
Traceback (most recent call last):
  File "/Users/ophir/dev/ophir/tf_keras_metrics.py", line 49, in <module>
    metrics=[precision, recall, tensorflow.metrics.auc]
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/keras/engine/training.py", line 956, in compile
    metric_result = masked_metric_fn(y_true, y_pred, mask=masks[i])
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/keras/engine/training.py", line 489, in masked
    return K.mean(score_array)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 1120, in mean
    axis = _normalize_axis(axis, ndim(x))
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 437, in ndim
    dims = x.get_shape()._dims
AttributeError: 'tuple' object has no attribute 'get_shape'

Process finished with exit code 1

编辑

在采纳 Marcin Możejko 的建议后

代码是:

import pandas
import numpy
import tensorflow.contrib.keras as keras
import tensorflow


def metric_function(y_true, y_pred):
    return tensorflow.metrics.precision(y_true,y_pred)[0]


def target_function(row):
    return float(row[0] - row[1] < 0.5)

df = pandas.DataFrame(numpy.random.rand(10000,2))
label = df.apply(target_function, axis=1)

input_layer = keras.layers.Input(shape=(2,))
net = keras.layers.Dense(1)(input_layer)

model = keras.models.Model(inputs=[input_layer], outputs=[net])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[metric_function])

model.fit(df.as_matrix(), label.as_matrix(), epochs=10, validation_split=0.2, batch_size=100)

错误是:

/Users/ophir/anaconda3/envs/p3/bin/python /Users/ophir/dev/ophir/tf_keras_metrics.py
Train on 8000 samples, validate on 2000 samples
Epoch 1/10
2017-04-04 16:05:30.959006: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-04-04 16:05:30.959022: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-04-04 16:05:30.959026: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-04-04 16:05:30.959031: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-04-04 16:05:31.124262: W tensorflow/core/framework/op_kernel.cc:1152] Failed precondition: Attempting to use uninitialized value precision/true_positives/count
     [[Node: precision/true_positives/count/read = Identity[T=DT_FLOAT, _class=["loc:@precision/true_positives/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](precision/true_positives/count)]]
Traceback (most recent call last):
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1051, in _do_call
    return fn(*args)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1033, in _run_fn
    status, run_metadata)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value precision/true_positives/count
     [[Node: precision/true_positives/count/read = Identity[T=DT_FLOAT, _class=["loc:@precision/true_positives/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](precision/true_positives/count)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ophir/dev/ophir/tf_keras_metrics.py", line 23, in <module>
    model.fit(df.as_matrix(), label.as_matrix(), epochs=10, validation_split=0.2, batch_size=100)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/contrib/keras/python/keras/engine/training.py", line 1494, in fit
    initial_epoch=initial_epoch)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/contrib/keras/python/keras/engine/training.py", line 1138, in _fit_loop
    outs = f(ins_batch)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/contrib/keras/python/keras/backend.py", line 2245, in __call__
    updated = session.run(self.outputs + [self.updates_op], feed_dict=feed_dict)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 786, in run
    run_metadata_ptr)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 994, in _run
    feed_dict_string, options, run_metadata)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1044, in _do_run
    target_list, options, run_metadata)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1064, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value precision/true_positives/count
     [[Node: precision/true_positives/count/read = Identity[T=DT_FLOAT, _class=["loc:@precision/true_positives/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](precision/true_positives/count)]]

Caused by op 'precision/true_positives/count/read', defined at:
  File "/Users/ophir/dev/ophir/tf_keras_metrics.py", line 21, in <module>
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[metric_function])
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/contrib/keras/python/keras/engine/training.py", line 958, in compile
    metric_result = masked_metric_fn(y_true, y_pred, mask=masks[i])
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/contrib/keras/python/keras/engine/training.py", line 487, in masked
    score_array = fn(y_true, y_pred)
  File "/Users/ophir/dev/ophir/tf_keras_metrics.py", line 8, in metric_function
    return tensorflow.metrics.precision(y_true,y_pred)[0]
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/ops/metrics_impl.py", line 1377, in precision
    updates_collections=None, name=None)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/ops/metrics_impl.py", line 1274, in true_positives
    updates_collections)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/ops/metrics_impl.py", line 1211, in _count_condition
    count = _create_local('count', shape=[])
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/ops/metrics_impl.py", line 197, in _create_local
    validate_shape=validate_shape)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 197, in __init__
    expected_shape=expected_shape)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/ops/variables.py", line 316, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1343, in identity
    result = _op_def_lib.apply_op("Identity", input=input, name=name)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
    self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value precision/true_positives/count
     [[Node: precision/true_positives/count/read = Identity[T=DT_FLOAT, _class=["loc:@precision/true_positives/count"], _device="/job:localhost/replica:0/task:0/cpu:0"](precision/true_positives/count)]]

Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x1140626d8>>
Traceback (most recent call last):
  File "/Users/ophir/anaconda3/envs/p3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 595, in __del__
AttributeError: 'NoneType' object has no attribute 'TF_NewStatus'

Process finished with exit code 1

【问题讨论】:

  • 您能提供您的model 定义吗?
  • 添加了一个可以重现问题的虚拟代码 sn-p
  • 你可以试试:model = keras.models.Model(inputs=input_layer, outputs=net)
  • 试过 - 同样的错误
  • 没有这个 auc 是否可以工作?

标签: python tensorflow machine-learning neural-network keras


【解决方案1】:

好的 - 我想我发现了一个错误。此函数返回 tuple(auc, other stuff)。你需要通过挑战你自己的auc 来克服这个问题:

def auc_tf_metric(y_true, y_pred):
    return tensorflow.metrics.auc(y_true, y_pred)[0]

【讨论】:

  • 现在我得到:FailedPreconditionError(参见上文的回溯):尝试使用未初始化的值 auc/true_positives [[Node: auc/true_positives/read = Identity[T=DT_FLOAT, _class=["loc: @auc/true_positives"], _device="/job:localhost/replica:0/task:0/cpu:0"](auc/true_positives)]]
  • 它失败了,但在不同的地方
  • 还没有 - 问题包含更新程序和错误代码
  • 如果我提供给您,您可以尝试一些非常简单的调整吗?
  • 检查我的答案。
【解决方案2】:

由 K.switch 编写 将 keras.backend 导入为 K

def k_mean_iou(NUM_CLASSES):
        """
        assume 0 is background for labels and prediction
        labels,prediction with shape of [batch,height,width,class_number]

        """
        def switch_mean_iou(labels, predictions):
            mean_iou = K.variable(0.0)
            seen_classes = K.variable(0.0)

            for c in range(1,NUM_CLASSES):
                labels_c = K.cast(K.equal(labels, c), K.floatx())
                pred_c = K.cast(K.equal(predictions, c), K.floatx())

                labels_c_sum = K.sum(labels_c)
                pred_c_sum = K.sum(pred_c)

                intersect = K.sum(labels_c*pred_c)
                union = labels_c_sum + pred_c_sum - intersect
                iou = intersect / union
                condition = K.equal(union, 0)
                mean_iou = K.switch(condition,
                                    mean_iou,
                                    mean_iou+iou)
                seen_classes = K.switch(condition,
                                        seen_classes,
                                        seen_classes+1)

            mean_iou = K.switch(K.equal(seen_classes, 0),
                                mean_iou,
                                mean_iou/seen_classes)
            return mean_iou

        return switch_mean_iou

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=k_mean_iou(class_number))

【讨论】:

    【解决方案3】:

    我认为你应该可以这样做:

    def tf_auc_score(y_true, y_pred):
        return tensorflow.metrics.auc(y_true, y_pred)[1]
    
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[tf_auc_score])
    
    from keras import backend as K
    K.get_session().run(tf.local_variables_initializer())
    
    model.fit(...)
    

    似乎它曾经是tf.global_variables_initializer(),但现在tf.local_variables_initializer() 有效,至少对我来说是这样。

    【讨论】:

      【解决方案4】:

      关于新的错误:通常,FailedPreconditionsErrors 在会话中运行tf.global_variables_initializer() 时发生。确保您没有遗漏它。

      【讨论】:

      猜你喜欢
      • 2018-06-02
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 2018-10-31
      • 2021-03-11
      • 2020-12-24
      相关资源
      最近更新 更多