美文网首页
tensorflow session的用法

tensorflow session的用法

作者: 笨码农慢慢飞 | 来源:发表于2018-11-25 22:24 被阅读0次

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的用法

      本文链接:https://www.haomeiwen.com/subject/lmimqqtx.html