TensorFlow基础入门之图

#导入tenflow库
import tensorflow  as tf
#创建一个常量,常量值是“hello world”,类型是字符串,名字是“input”(名字可以随便起)
txt_tf=tf.constant("hellow world",dtype=tf.string,name="input")
#上面定义图,下面是启动图开始运算,tf.compat.v1.Session()是新版的,旧版的是tf.Session()
with tf.compat.v1.Session() as sess:
    txt=sess.run(txt_tf)
    print(txt.decode())
    

第一行代码总是那么简洁!
上面的代码就是执行以下图:
在这里插入图片描述

import tensorflow  as tf
A=[[1,2,3],[4,5,6]]
B=[[1,1],
   [1,1],
   [1,1]]
A_tf=tf.constant(A,dtype=tf.float32,name="A")
B_tf=tf.constant(B,dtype=tf.float32,name="B")
C_tf=tf.matmul(A_tf,B_tf)
with tf.compat.v1.Session() as sess:
    txt=sess.run(C_tf)
    print(txt)

这是一个这样的图:
在这里插入图片描述


图中可以包含操作,数据,和“集合”;
图可以被自己定义,也可以使用默认图,一开始就是自动使用默认图。所有的操作都是在图中进行的。

tensorflow主要使用静态图。

发布了75 篇原创文章 · 获赞 14 · 访问量 961

猜你喜欢

转载自blog.csdn.net/qq_41148461/article/details/102866239