tensorflow session的用法

with session的用法

import tensorflow as tf
a=tf.constant(3)
b=tf.constant(4)
z=a*b

with tf.Session() as sess:
    print(sess.run(z))
12

使用注入机制的用法

import tensorflow as tf
a=tf.placeholder(tf.int16)
b=tf.placeholder(tf.int16)

mul=tf.multiply(a,b)
add=tf.add(a,b)

with tf.Session() as sess:
    print(sess.run(mul,feed_dict={a:4,b:3}))
    print(sess.run(add,feed_dict={a:4,b:3}))
12
7

利用session.run()实现多元节点同时运算

import tensorflow as tf
a=tf.placeholder(tf.int16)
b=tf.placeholder(tf.int16)

mul=tf.multiply(a,b)
add=tf.add(a,b)

with tf.Session() as sess:
    #同时以数组的形式获得运算节点的结果
    print(sess.run([mul,add],feed_dict={a:4,b:3}))
  
[12, 7]

你可能感兴趣的:(tensorflow session的用法)