初识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.        ]]
相关推荐
池央22 分钟前
AI性能极致体验:通过阿里云平台高效调用满血版DeepSeek-R1模型
人工智能·阿里云·云计算
我们的五年23 分钟前
DeepSeek 和 ChatGPT 在特定任务中的表现:逻辑推理与创意生成
人工智能·chatgpt·ai作画·deepseek
Yan-英杰24 分钟前
百度搜索和文心智能体接入DeepSeek满血版——AI搜索的新纪元
图像处理·人工智能·python·深度学习·deepseek
Fuweizn26 分钟前
富唯智能可重构柔性装配产线:以智能协同赋能制造业升级
人工智能·智能机器人·复合机器人
weixin_307779131 小时前
Azure上基于OpenAI GPT-4模型验证行政区域数据的设计方案
数据仓库·python·云计算·aws
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
taoqick2 小时前
对PosWiseFFN的改进: MoE、PKM、UltraMem
人工智能·pytorch·深度学习
suibian52352 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
预测模型的开发与应用研究3 小时前
数据分析的AI+流程(个人经验)
人工智能·数据挖掘·数据分析
源大模型3 小时前
OS-Genesis:基于逆向任务合成的 GUI 代理轨迹自动化生成
人工智能·gpt·智能体