tensorflow入门变量常量

tensorflow入门
import tensorflow as tf
hello = tf.constant('Hello!this is my first tensorflow1.14.0,')
sess = tf.Session()
print(sess.run(hello))
  • 结果:b’Hello!this is my first tensorflow1.14.0,’
tf变量常量
  1. 定义常量变量
#opencv tensorflow
#类比 语法 api 原理 
#基础数据类型 运算符 流程 字典 数组 
import tensorflow as tf
#定义常量
data1 = tf.constant(2,dtype=tf.int32)
#定义变量
data2 = tf.Variable(10,name='var')
print(data1)
print(data2)
#只打印出数据信息没有打印内容tensor张量
  • 结果:

    tf.Tensor(2, shape=(), dtype=int32) <tf.Variable ‘var:0’ shape=() dtype=int32, numpy=10>

  1. 定义常量并输出tensorflow所有操作都需要session会话
import tensorflow as tf
#定义常量并输出
data1 = tf.constant(2,dtype=tf.int32)#第二个参数可选,指定类型
print(data1)
sess = tf.Session()#定义Session()
print(sess.run(data1))
#结果应该是2
  • 结果:

    tf.Tensor(2, shape=(), dtype=int32)
    <tf.Variable 'var:0' shape=() dtype=int32, numpy=10>
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-2-1ac0b5480770> in <module>
          6 print(data1)
          7 print(data2)
    ----> 8 sess = tf.Session()
          9 print(sess.run(data1))
    
    AttributeError: module 'tensorflow' has no attribute 'Session'
    
  • 版本问题:我目前是tensorflow2.1.0

  • 解决重新安装

    #先卸载掉Anaconda里面已经安装好的tensorflow2.1.0的版本
    #CMD命令窗口操作
    #输入:conda install -n tensorflow tensorflow==1.14.0
    #安装记录我放在了我的另一个博客
    
  • 安装后输出结果

    Tensor("Const_5:0", shape=(), dtype=int32)
    2#正常输出2
    
  1. 定义变量并输出(注意与常量的区别)
  • #错误代码
    import tensorflow as tf
    #定义变量并输出
    data2 = tf.Variable(10,name='var')
    print(data2)
    sess = tf.Session()#定义Session()
    print(sess.run(data2))
    

    结果问题:

    FailedPreconditionError: Attempting to use uninitialized value var_3
    	 [[{{node _retval_var_3_0_0}}]]
    
  • 原因:tensorflow所有使用的变量都需要使用Session进行初始化

    #改进程序
    import tensorflow as tf
    #定义变量
    data2 = tf.Variable(10,name='var')
    print(data2)
    sess = tf.Session()#定义Session()
    init = tf.global_variables_initializer()#初始化变量
    sess.run(init)#初始化变量也要放入run
    print(sess.run(data2))
    

    正确结果:

    <tf.Variable 'var_4:0' shape=() dtype=int32_ref>
    10#正常打印出10
    
    tensorflow运算原理
  1. tensorflow实质:张量tensor+计算图grahps
    在这里插入图片描述
  • 正常操作需要关闭sess.close()

另一方法:

with sess:
    sess.run(init)
    print(sess.run(data2))
  • ‘’'多行注释
    jupyter 操作添加行标

View–>Line Number
*

#opencv tensorflow
#类比 语法 api 原理 
#基础数据类型 运算符 流程 字典 数组 
import tensorflow as tf
#定义常量
data1 = tf.constant(2,dtype=tf.int32)
#定义变量
data2 = tf.Variable(10,name='var')
print(data1)
print(data2)
'''
sess = tf.Session()
print(sess.run(data1))
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(data2))
sess.close()
# 本质 tf = tensor + 计算图
# tensor 数据
# op 
# graphs 数据操作
# session执行核心,运算的交互环境
'''
init = tf.global_variables_initializer()
sess = tf.Session()
with sess:
    sess.run(init)
    print(sess.run(data2))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值