初识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.        ]]
相关推荐
创意锦囊9 分钟前
ChatGPT推出Canvas功能
人工智能·chatgpt
知来者逆18 分钟前
V3D——从单一图像生成 3D 物体
人工智能·计算机视觉·3d·图像生成
碳苯1 小时前
【rCore OS 开源操作系统】Rust 枚举与模式匹配
开发语言·人工智能·后端·rust·操作系统·os
杰哥在此1 小时前
Python知识点:如何使用Multiprocessing进行并行任务管理
linux·开发语言·python·面试·编程
whaosoft-1431 小时前
51c视觉~CV~合集3
人工智能
zaim13 小时前
计算机的错误计算(一百一十四)
java·c++·python·rust·go·c·多项式
网络研究院3 小时前
如何安全地大规模部署 GenAI 应用程序
网络·人工智能·安全·ai·部署·观点
凭栏落花侧3 小时前
决策树:简单易懂的预测模型
人工智能·算法·决策树·机器学习·信息可视化·数据挖掘·数据分析
xiandong206 小时前
240929-CGAN条件生成对抗网络
图像处理·人工智能·深度学习·神经网络·生成对抗网络·计算机视觉
innutritious7 小时前
车辆重识别(2020NIPS去噪扩散概率模型)论文阅读2024/9/27
人工智能·深度学习·计算机视觉