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))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值