python constant_python-tf.constant和tf.placeholder的行为不同

我想将tf.metrics封装在Sonnet模块上以测量每个批次的性能,以下是我所做的工作:

import tensorflow as tf

import sonnet as snt

class Metrics(snt.AbstractModule):

def __init__(self, indicator, summaries = None, name = "metrics"):

super(Metrics, self).__init__(name = name)

self._indicator = indicator

self._summaries = summaries

def _build(self, labels, logits):

if self._indicator == "accuracy":

metric, metric_update = tf.metrics.accuracy(labels, logits)

with tf.control_dependencies([metric_update]):

outputs = tf.identity(metric)

elif self._indicator == "precision":

metric, metric_update = tf.metrics.precision(labels, logits)

with tf.control_dependencies([metric_update]):

outputs = tf.identity(metric)

elif self._indicator == "recall":

metric, metric_update = tf.metrics.recall(labels, logits)

with tf.control_dependencies([metric_update]):

outputs = tf.identity(metric)

elif self._indicator == "f1_score":

metric_recall, metric_update_recall = tf.metrics.recall(labels, logits)

metric_precision, metric_update_precision = tf.metrics.precision(labels, logits)

with tf.control_dependencies([metric_update_recall, metric_update_precision]):

outputs = 2.0 / (1.0 / metric_recall + 1.0 / metric_precision)

else:

raise ValueError("unsupported metrics")

if type(self._summaries) == list:

self._summaries.append(tf.summary.scalar(self._indicator, outputs))

return outputs

但是,当我想测试模块时,以下代码可以工作:

def test3():

import numpy as np

labels = tf.constant([1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], tf.int32)

logits = tf.constant([1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], tf.int32)

metrics = Metrics("accuracy")

accuracy = metrics(labels, logits)

metrics2 = Metrics("f1_score")

f1_score = metrics2(labels, logits)

writer = tf.summary.FileWriter("utils-const", tf.get_default_graph())

with tf.Session() as sess:

sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])

accu, f1 = sess.run([accuracy, f1_score])

print(accu)

print(f1)

writer.close()

但是,以下代码不起作用:

def test4():

from tensorflow.python import debug as tf_debug

import numpy as np

tf_labels = tf.placeholder(dtype=tf.int32, shape=[None])

tf_logits = tf.placeholder(dtype=tf.int32, shape=[None])

labels = np.array([1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], np.int32)

logits = np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], np.int32)

metrics = Metrics("accuracy")

accuracy = metrics(tf_labels, tf_logits)

metrics2 = Metrics("f1_score")

f1_score = metrics2(tf_labels, tf_logits)

writer = tf.summary.FileWriter("utils-feed", tf.get_default_graph())

with tf.Session() as sess:

sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])

sess = tf_debug.LocalCLIDebugWrapperSession(sess)

accu, f1 = sess.run([accuracy, f1_score], feed_dict = {tf_labels: labels, tf_logits: logits})

print(accu)

print(f1)

writer.close()

test3()的输出正确,为0.88. test4()的输出错误,为0.0.但是,它们应该等效.

有人知道吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值