初识tensorflow程序设计模式

文章目录

github地址https://github.com/fz861062923/TensorFlow

建立'计算图'

python 复制代码
#建立'计算图'
import tensorflow as tf
x=tf.constant(2,name='x')#建立常量,有点像C
y=tf.Variable(x+5,name='y')#建立变量
python 复制代码
#执行'计算图'
with tf.Session() as sess:
    init=tf.global_variables_initializer()#初始化global变量
    sess.run(init)
    print('x=',sess.run(x))
    print('y=',sess.run(y))
复制代码
x= 2
y= 7
python 复制代码
x
复制代码
<tf.Tensor 'x:0' shape=() dtype=int32>

tensorflow placeholder

正如这个名字一样,hold on,hold on,告诉计算机等等在把值传给你,嘻嘻嘻嘻

python 复制代码
a=tf.placeholder('int32')
b=tf.placeholder('int32')
c=tf.multiply(a,b)
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print('c=',sess.run(c,feed_dict={a:6,b:7}))
复制代码
c= 42
tensorflow数值运算常用的方法
  • tf.add(x,y)
  • tf.subtract(x,y)#减法
  • tf.multiply(x,y)
  • tf.divide(x,y)
  • tf.mod(x,y)#余数
  • tf.sqrt(x,name=None)
  • tf.abs(x,name=None)

tensorboard

正如其名,可视化已经建立的计算图

python 复制代码
#承接上面的session
#下面代码将显示在tensorboard的数据写在log文件中
tf.summary.merge_all()#将显示在board的数据整合
train_writer=tf.summary.FileWriter('log/c',sess.graph)#写入log文件中
启动tensorboard的方法
  • activate tensorflow(虚拟环境名称)
  • tensorboard --logdir=c:\python\log\c
  • 用浏览器打开http://lacalhost:6006/

建立一维与二维张量

建立一维张量
python 复制代码
ts_x=tf.Variable([0.4,0.2,0.4])
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    x=sess.run(ts_x)
    print(x)
复制代码
[0.4 0.2 0.4]
python 复制代码
x.shape
复制代码
(3,)
建立二维张量
python 复制代码
ts_x=tf.Variable([[0.4,0.2,0.4]])
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    x=sess.run(ts_x)
    print(x)
复制代码
[[0.4 0.2 0.4]]
python 复制代码
x.shape
复制代码
(1, 3)
建立新的二维张量
python 复制代码
ts_x=tf.Variable([[0.4,0.2],
                 [0.3,0.4],
                 [-0.5,0.2]])
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    x=sess.run(ts_x)
    print(x)
复制代码
[[ 0.4  0.2]
 [ 0.3  0.4]
 [-0.5  0.2]]
python 复制代码
x.shape
复制代码
(3, 2)

矩阵的基本运算

矩阵的加法
python 复制代码
x=tf.Variable([[1.,1.,1.]])
w=tf.Variable([[-0.1,-0.2],
              [-0.3,0.4],
              [0.5,0.6]])
xw=tf.matmul(x,w)

with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(xw))
复制代码
[[0.09999999 0.8       ]]
矩阵乘法与加法
python 复制代码
x=tf.Variable([[1.,1.,1.]])
w=tf.Variable([[-0.1,-0.2],
              [-0.3,0.4],
              [0.5,0.6]])
b=tf.Variable([[0.1,0.2]])
xwb=tf.matmul(x,w)+b

with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    print(sess.run(xwb))
复制代码
[[0.19999999 1.        ]]
相关推荐
卡奥斯开源社区官方40 分钟前
NVIDIA Blackwell架构深度解析:2080亿晶体管如何重构AI算力规则?
人工智能·重构·架构
百锦再1 小时前
第11章 泛型、trait与生命周期
android·网络·人工智能·python·golang·rust·go
zbhbbedp282793cl3 小时前
如何在VSCode中安装Python扩展?
ide·vscode·python
数新网络4 小时前
The Life of a Read/Write Query for Apache Iceberg Tables
人工智能·apache·知识图谱
Yangy_Jiaojiao4 小时前
开源视觉-语言-动作(VLA)机器人项目全景图(截至 2025 年)
人工智能·机器人
gorgeous(๑>؂<๑)5 小时前
【ICLR26匿名投稿】OneTrackerV2:统一多模态目标跟踪的“通才”模型
人工智能·机器学习·计算机视觉·目标跟踪
坠星不坠5 小时前
pycharm如何导入ai大语言模型的api-key
人工智能·语言模型·自然语言处理
周杰伦_Jay5 小时前
【智能体(Agent)技术深度解析】从架构到实现细节,核心是实现“感知环境→处理信息→决策行动→影响环境”的闭环
人工智能·机器学习·微服务·架构·golang·数据挖掘
Python私教5 小时前
Python 开发环境安装与配置全指南(2025版)
开发语言·python
百锦再5 小时前
第12章 测试编写
android·java·开发语言·python·rust·go·erlang