tensorflow(一)创建图启动图

创建图启动图

使用软件(jupyter notebook)

import tensorflow as tf(shift+enter键执行,看安装环境是否成功,若未成功,请看博主上一篇解决问题)

#创建俩个常量op  (快捷键:tab键补全)
m1=tf.constant([[3,3]])
m2=tf.constant([[2],[3]])
product=tf.matmul(m1,m2)

print(product)

(shift+enter键执行)结果:

Tensor("MatMul:0", shape=(1, 1), dtype=int32)  (说明需要在会话图中执行,现在只是定义了op)

#定义一个会画,启动默认图
sess=tf.Session()
#调用sess的run方法来执行矩阵乘法op
#run(product)触发了图中3个op
result=sess.run(product)

print(result)

(shift+enter键执行)结果:15

第二种简便方法

import tensorflow as tf
with tf.Session() as sess:
    result=sess.run(product)
    print(result)

猜你喜欢

转载自blog.csdn.net/qq_38798147/article/details/79968234