【问题标题】:How run multiple operation together in one line in tensor session?如何在张量会话中在一行中同时运行多个操作?
【发布时间】:2017-09-26 22:23:50
【问题描述】:

我正在学习 tensorflow,我首先有两个问题,是否需要在会话中运行每个操作?就像我创建一个简单的程序,其中有三个操作加减法和矩阵乘法,那么是否有必要在会话中运行所有这些操作?

import tensorflow as tf
import numpy as np
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
a=np.array([[1,2,3],[4,5,6],[5,6,7]],dtype="float32")
b=np.array([[7,8,9],[9,6,5],[6,7,8]],dtype="float32")
ab=tf.Variable(a)
bb=tf.constant(b)
inn=tf.global_variables_initializer()
add=ab+bb
sub=(ab-bb)

mul=tf.matmul(a,b)
with tf.Session() as rr:
    rr.run(inn)
    rr.run(ab)
    res=rr.run(mul)
    print(res)

所以现在我必须在会话中运行每个操作(add、sub 和 matmul)??

如果“是”我必须运行,那么我可以一起运行所有这些操作吗?我试过了,但我得到了错误:

首先我尝试了这个:

import tensorflow as tf
import numpy as np
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
a=np.array([[1,2,3],[4,5,6],[5,6,7]],dtype="float32")
b=np.array([[7,8,9],[9,6,5],[6,7,8]],dtype="float32")
ab=tf.Variable(a)
bb=tf.constant(b)
inn=tf.global_variables_initializer()
add=ab+bb
sub=(ab-bb)

mul=tf.matmul(a,b)
with tf.Session() as rr:
    rr.run(inn,ab,add,sub,mul)
    rr.run(ab)
    res=rr.run(mul)
    print(res)

我收到了这个错误:

TypeError: run() takes from 2 to 5 positional arguments but 6 were given

所以我从运行中删除了一个参数(mul)然后我得到了这个错误:

 raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
    TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

我怎样才能运行一次所有操作,或者我必须单独运行每个操作?

在编写此代码的第二个过程中,我尝试将两个形状为 [2,3] 的矩阵相乘,我的程序是:

import tensorflow as tf
import numpy as np
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
a=np.array([[1,2,3],[4,5,6]],dtype="float32")
b=np.array([[7,8,9],[9,6,5]],dtype="float32")
ab=tf.Variable(a)
bb=tf.constant(b)
inn=tf.global_variables_initializer()
add=ab+bb
sub=(ab-bb)

mul=tf.matmul(a,b)
with tf.Session() as rr:
    rr.run(inn)
    rr.run(ab)
    res=rr.run(mul)
    print(res)

我收到了这个错误:

ValueError: Dimensions must be equal, but are 3 and 2 for 'MatMul' (op: 'MatMul') with input shapes: [2,3], [2,3].

如何乘 a=([1,2,3],[4,5,6]) b= ([9,8,7],[6,4,3]) ??

【问题讨论】:

    标签: python python-2.7 python-3.x tensorflow tensorboard


    【解决方案1】:

    要回答第一个问题,是的,一切都在会话中运行。您定义一个图,然后会话是当图被编译、加载到 tensorflow 内部并执行时,只要会话打开,所有变量的状态就会保持。

    你的代码有点奇怪。您将其中一个输入定义为常量,而将另一个定义为变量,但变量永远不会改变。但在最基本的层面上,您可以通过将多个操作作为列表传递来在会话中运行多个操作。方法如下:

    a=np.array([[1,2,3],[4,5,6]],dtype="float32") b=np.array([[7,8,9],[9,6,5]],dtype="float32") tfa = tf.constant(a) tfb = tf.constant(b) 添加 = tfa + tfb 子 = tfa - tfb 使用 tf.Session() 作为 s: res_add, res_sub = s.run([add, sub]) 打印(res_add) 打印(res_sub)

    输出是

    [[ 8. 10. 12.] [13. 11. 11.]] [[-6。 -6。 -6.] [-5。 -1。 1.]]

    您的矩阵乘法不起作用,因为内部维度必须匹配。这是否回答了您的问题,还是您正在尝试做其他事情?

    【讨论】:

    • 嗨,感谢您的回答,我选择一个作为常量,第二个作为变量,因为我试图在一些步骤后为变量分配新值,我没有在这里显示该步骤,第二件事你怎么办意思是“内部维度”以及我如何将最后一行的 a 和 b 矩阵相乘?
    • 矩阵乘法很棘手,您不能将任何矩阵乘以任何矩阵。如果矩阵是正方形的,则它们必须具有相同的大小,如果它们不是正方形的,则第一个矩阵的第二维必须与第二个矩阵的第一维相同,反之亦然。但听起来您不想在这里进行实际的矩阵乘法,而是一次将它们乘以一个数字。为此,不要使用tf.matmul() 函数,只需定义mul = ab * bb,您应该会得到预期的结果。
    • 然后当我查看 tensorflow 官方文档时:tensorflow.org/api_docs/python/tf/matmul 那里他们只是将两个矩阵与大小 [2,3] 和 [3,2] 相乘?
    • 是的,他们有。你正在尝试多个 [2,3] 和 [2,3] :)
    猜你喜欢
    • 2016-10-05
    • 2013-11-24
    • 1970-01-01
    • 2018-03-18
    • 2020-01-04
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多