码迷,mamicode.com
首页 > 其他好文 > 详细

6.图和会话

时间:2018-07-20 11:32:58      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:run   print   pytho   operation   ant   import   imp   flow   查看   

import tensorflow as tf

# 之前也说过,直接打印,只能得到状态,是得不到值的
c = tf.constant([[1,2],[2,3]],name=‘const_1‘,dtype=tf.int64)
print(c)  # Tensor("const_1:0", shape=(2, 2), dtype=int64)

# 必须创建会话,然后执行
with tf.Session() as sess:
    print(sess.run(c))
    ‘‘‘
    [[1 2]
    [2 3]]
    ‘‘‘

# 之前说过,tensor和operation组成graph,交给session执行
# 在这里我们并没有显式地创建graph,为什么可以执行呢?
# 因为TensorFlow默认帮我们创建一张图,可以通过tf.get_default_graph()查看
print(c.graph is tf.get_default_graph())  # True
const1 = tf.constant([[2, 2]])
const2 = tf.constant([[4], [4]])

# matmul -->matrix,multiple,矩阵间的相乘,相当于np.dot()
multiple = tf.matmul(const1, const2)
print(multiple)  # Tensor("MatMul:0", shape=(1, 1), dtype=int32)

with tf.Session() as sess:
    print(sess.run(multiple))  # [[16]]

  

6.图和会话

标签:run   print   pytho   operation   ant   import   imp   flow   查看   

原文地址:https://www.cnblogs.com/traditional/p/9339386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!