tensorflow基础知识
tensorflow常量变量定义
#opencv tensorflow
#掌握tensorflow的语法,api的调用,api的原理
import tensorflow as tf
data1=tf.constant(2.5)#常量
data2=tf.Variable(10,name='var')
print(data1)
print(data2)
""" 结果: Tensor("Const:0", shape=(), dtype=float32) <tf.Variable 'var:0' shape=() dtype=int32_ref> """
#Tensor表示张量,Const:常量,Variable:变量,shape维度
#定义session显示结果
sess=tf.Session()
print(sess.run(data1))
#data1是浮点类型,我们将其定义成int类型
data1=tf.constant(2,dtype=tf.int32)
print(sess.run(data1))#2
print(data1)#Tensor("Const_6:0", shape=(), dtype=int32)
#打印变量
#print(sess.run(data2))#报错, Attempting to use uninitialized value
init=tf.global_variables_initializer()#初始化
sess.run(init)
print(sess.run(data2))#10
tensorflow运算原理
tensorflow实质:张量tensor+计算图graphs
tensor+op——>graphs
tensor本质是数据:变量或常量,一维或n维
op:operation,操作
计算图:数据+操作
session:可以理解为运算的交互环境
注意:tensorflow中所有变量要在初始化之后才能使用,init操作,init操作实质仍然是计算图,一般session使用后可以关闭
sess.close()
如果不想关闭sess可以使用with关键字:
import tensorflow as tf
data1=tf.Variable(10,name="var")
init=tf.global_variables_initializer()
sess=tf.Session()
with sess:
sess.run(init)
print(sess.run(data1))
还没有评论,来说两句吧...