简体   繁体   English

在恒定张量上评估时,Tensorflow的Argmax函数不显示值

[英]Argmax function of Tensorflow does not print value when evaluated on a constant tensor

I'm new at Tensorflow. 我是Tensorflow的新手。 I am having a litte trouble at understanding its constants. 我在理解其常数方面遇到了小麻烦。 I have this simple code mentioned below: 我有下面提到的这个简单的代码:

import tensorflow as tf
vector = tf.constant([[1,2,3,4],[4,5,6,7],[8,9,1,2]],tf.int32,name="vector")
with tf.Session() as sess:
    v = sess.run(vector)
    argm = tf.argmax(v,1)
    print(argm)

I expect this to return something like [4,7,8] , as I understood from the documentation. 我从文档中了解到,我希望它返回类似[4,7,8]的内容。 Instead, I get this: 相反,我得到这个:

 Tensor("ArgMax:0", shape=(3,), dtype=int64). 

So, i don't know what am I doing wrong. 所以,我不知道我在做什么错。

Alternatively to the answer of @James, you might want to use tensorflow's eager execution , which behaves more like "standard" python: operations are executed as you type them, no more graphs and Session . 除了@James的答案,您可能想使用tensorflow的热切执行 ,其行为更像是“标准” python:在键入操作时执行操作,不再需要图形和Session

import tensorflow as tf

tf.enable_eager_execution()

vector = tf.constant([[1,2,3,4],[4,5,6,7],[8,9,1,2]],tf.int32,name="vector")
argm = tf.argmax(vector,1)
print(argm)

Tensorflow operations like tf.argmax somewhat unintuitively don't perform the operation that they're stating, but add an operation to a graph that will be performed. tf.argmax这样的Tensorflow操作tf.argmax某种程度上不直观地不执行它们要说明的操作,而是向要执行的图形添加操作。 When you run argm = tf.argmax(v,1) , the return value is tensor that isn't evaluated yet. 当您运行argm = tf.argmax(v,1) ,返回值是尚未评估的张量。

If you want the result of the argmax operation, you can run something like this: 如果您需要argmax操作的结果,则可以运行以下内容:

import tensorflow as tf
vector = tf.constant([[1,2,3,4],[4,5,6,7],[8,9,1,2]],tf.int32,name="vector")
argm = tf.argmax(vector,1)
with tf.Session() as sess:
    a = sess.run(argm)
    print(a)

With this code, we're explicitly asking Tensorflow to run the computations to compute the result of the tf.argmax operation. 使用此代码,我们明确要求Tensorflow运行计算以计算tf.argmax操作的结果。 With your previous code, we ran the computation to compute v (which is a constant, so that's pretty quick), then define a new graph operation to compute argmax on that - but never actually do the computation. 使用您先前的代码,我们运行计算以计算v(这是一个常数,因此非常快),然后定义一个新的图操作以在其上计算argmax-但实际上从未进行过计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM