简体   繁体   English

TensorFlow中的一切都是Tensor,包括操作吗?

[英]Is everything a Tensor in TensorFlow, including operations?

I've been reading the documentation on core graph structures and it seems there is a disagreement with what TensorFlow actually does and the docs (unless I have a misunderstanding, which I assume I do). 我一直在阅读关于核心图结构的文档似乎对TensorFlow实际上做的和文档有什么不同(除非我有一个误解,我认为我这样做)。

The documentation says there are Operation objects and Tensor objects . 文档说有Operation对象Tensor对象 It gives examples of such and thus I tried creating some and asking python what types they are. 它提供了这样的例子,因此我尝试创建一些并询问python它们是什么类型。 First lets do a constant: 首先让我们做一个常数:

c = tf.constant(1.0)

print c #Tensor("Const_1:0", shape=(), dtype=float32)
print type(c) #<class 'tensorflow.python.framework.ops.Tensor'>

it says its a Tensor. 它说它是一个Tensor。 Great! 大! Makes sense and it even gives me information about its contents. 有意义,它甚至给我有关其内容的信息。

I did the same experiment with what I expected to be an operation: 我做了一个与我期望的操作相同的实验:

W = tf.Variable(tf.truncated_normal([784, 10], mean=0.0, stddev=0.1))
b = tf.Variable(tf.constant(0.1, shape=[10]))
Wx = tf.matmul(x, W)
print Wx #Tensor("MatMul:0", shape=(?, 10), dtype=float32)
print type(Wx) #<class 'tensorflow.python.framework.ops.Tensor'>

However, as you can see, Tensor flow said that both Wx and c are the same type. 但是,正如您所看到的,Tensor流表示Wx和c都是相同的类型。 Does this mean that there are no operation objects or am I doing something wrong? 这是否意味着没有操作对象或者我做错了什么?

tf.Varible is an tensor, and then you assign a value to it, that is an operation, an assign-operation. tf.Varible是一个张量,然后你为它赋值,即一个操作,一个赋值操作。 Or you use tf.mul() , that also an operation 或者你使用tf.mul(),这也是一个操作

There are operations. 有手术。 You can get a list of all operations in the graph by graph.get_operations() (where you could get graph via tf.get_default_graph() or sess.graph or whatever is appropriate in your situation). 您可以通过graph.get_operations()获取graph中所有操作的列表(您可以通过tf.get_default_graph()sess.graph或适合您情况的任何内容获取graph )。

It's just that, in Python, things like tf.mul return the tensor that the multiplication operation produces (everything else would be annoying since it's tensors that you use as input in further operation). 只是在Python中,像tf.mul这样的东西tf.mul返回乘法运算产生的张量(其他一切都会令人讨厌,因为它是你在进一步操作中用作输入的张量)。

I am not an expert, but maybe this will clear things up a bit. 我不是专家,但也许这会让事情变得清晰起来。

x = tf.constant(1, shape=[10, 10])
y = tf.constant(1, shape=[10, 10])
z = tf.matmul(x, y, name='operation')
# print(z)
# tf.Tensor 'operation:0' shape=(10, 10) dtype=int32
# z is a placeholder for the result of multiplication of x and y
# but it has an operation attached to it
# print(z.op)
# tensorflow.python.framework.ops.Operation at 0x10bfe40f0
# and so do x and y
# print(x.op)
# 
ses = tf.InteractiveSession()
# now that we are in a session, we have an operation graph
ses.graph.get_operation_by_name('operation')
# tensorflow.python.framework.ops.Operation at 0x10bfe40f0
# same operation that we saw attached to z
ses.graph.get_operations()
# shows a list of three operations, just as expected
# this way you can define the graph first and then run all the operations in a session

I'm not very familiar with TensorFlow, but it seems to be a basic concept that Wx is a symbolic handle for the output of tf.matmul(x, W) . 我对TensorFlow不太熟悉,但它似乎是一个基本概念, Wxtf.matmul(x, W)输出的符号句柄 So you actually created an Operation, but accessing Wx will give you a representation of the result (even though it's not calculated until you run a session). 所以你实际创建了一个Operation,但是访问Wx会给你一个结果的表示(即使它在你运行一个会话之前没有计算)。

Take a look at the TensorFlow FAQ for a more detailed explanation. 请查看TensorFlow常见问题解答以获取更详细的说明。

Think about this outside the context of tensorflow, and just in base python. 在tensorflow的上下文之外考虑这个,只是在基础python中。 Let's say you do this: 假设你这样做:

def f(x):
    return(x+1)

x = 0

print(type(x))
print(type(f(x)))

You get int in both cases, right? 在这两种情况下你都得到int ,对吗? But what if you do 但是如果你这样做会怎样

type(f)

In this case, you get a function . 在这种情况下,您将获得一个function Same with tensorflow: the type of the result of an operation is a new tensor, but the type of the operation itself is not a tensor. 与tensorflow相同:操作结果的类型是新的张量,但操作本身的类型不是张量。

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

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