欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tensorflow中session和placeholder的理解

程序员文章站 2024-03-20 18:11:52
...

tensorflow中session和placeholder的理解

placeholder等价于函数的输入。sess.run()等价于函数的调用。例如:

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

sess = tf.Session()

a = tf.placeholder("float")
b = tf.placeholder("float")
c = tf.constant(6.0)
d = tf.mul(a, b)
y = tf.mul(d, c)
print(sess.run(y, feed_dict={a: 3, b: 3}))

A = [[1.1, 2.3], [3.4, 4.1]]
Y = tf.matrix_inverse(A)
print(sess.run(Y))
sess.close()

等价于

import tensorflow as tf
def y(a,b):
    c = tf.constant(6.0)
    d = tf.multiply(a, b)
    return tf.multiply(d, c)

def Y(A):
    return tf.matrix_inverse(A)

print(y(3, 3))

A = [[1.1,2.3],[3.4,4.1]]
print(Y(A))