【TensorFlow笔记】

  • 基本结构(图 + 对话)
  • 图的演示
  • 对话的演示
  • 张量的演示
  • 变量
  • 一个线性回归的案例

这是博主的一篇笔记性质的博客,随着学习会不断更新。

基本结构(图 + 对话)

TensorFlow的基本结构是 图 + 对话。

import tensorflow as tf
def tensorflow_demo():'''Tensorflow的基本结构'''a_t = tf.constant(2)b_t = tf.constant(3)c_t = tf.add(a_t,b_t)with tf.Session() as sess:c_t_value = sess.run(c_t)print("c_t_value = ",c_t_value)

图的演示

包括默认图和在新建图,这两者互不打扰,

def graph_demo():'''图的演示'''a_t = tf.constant(2)b_t = tf.constant(3)c_t = tf.add(a_t,b_t)#默认图,如果不定义,则数据都在默认图中default_g = tf.get_default_graph()print("default_g = \n",default_g)print("a_t的默认图属性 \n",a_t.graph)       #新建图   new_g = tf.Graph()with new_g.as_default():a_new = tf.constant(20,name = "a_new")print(a_new)b_new = tf.constant(30,name = "b_new")c_new = tf.add(a_new,b_new,name = "c_new")print("a_new的自定义图属性 \n",a_new.graph)with tf.Session() as sess1:#c_t_value = sess1.run(c_t)print("c_t_value = ",c_t.eval())print("默认图属性 \n",sess1.graph)with tf.Session(graph = new_g) as sess2:c_new_value = sess2.run(c_new)print("c_new_value = ",c_new_value)print("自定义图属性 \n",sess2.graph)path = "./tmp/summary"shutil.rmtree(path)tf.summary.FileWriter(path,graph = sess2.graph)

对话的演示

对话主要掌握 run 和 feed_dict

def session_demo():#session\run\feed_dicta_t = tf.constant(2)b_t = tf.constant(3)c_t = tf.add(a_t,b_t)a = tf.placeholder(tf.float32,name = "a")print(a)b = tf.placeholder(tf.float32,name = "b")print(b)c = tf.multiply(a,b)#默认图,如果不定义,则数据都在默认图中#default_g = tf.get_default_graph()#print("default_g = \n",default_g)#print("a_t的默认图属性 \n",a_t.graph)with tf.Session(config = tf.ConfigProto(allow_soft_placement=True,log_device_placement=True)) as sess1:a_t_value,b_t_value,c_t_value = sess1.run([a_t,b_t,c_t])print("a_t_value = ",a_t_value,",b_t_value = ",b_t_value,",c_t_value = ",c_t_value)c_value = sess1.run(c, feed_dict = {a:2.5,b:3.3})print("c_value = ",c_value)

张量的演示

tensor这里要掌握初始化,数据类型的转换(tf.cast)、张量形状的改变。对于静态形状已经确定了的,则不能再改变,对于未确定的,可以利用something.set_shape()来改变。对于动态形状,可以利用tf.reshape()来改变,但是要注意,要保证前后元素的个数一致。

def tensor_demo():#张量的初始化x = tf.constant(2)y = tf.constant([1,2,3,4])linear_squares = tf.constant([[4],[9],[16],[25]],dtype = tf.int32)z = tf.zeros(shape = (3,4),dtype=tf.float32)d= tf.random_normal(shape = (2,3),mean=0,stddev=1,dtype=tf.float32)print("x  ===",x)print("y  ===",y)print("linear_squares  ===",linear_squares)print("z  ===",z)with tf.Session() as sess:print("z: ",sess.run(z),type(sess.run(z)))print("d: ",sess.run(d),type(sess.run(d)))#张量类型的修改x_cast = tf.cast(x,dtype=tf.float32)print("x_cast  ===",x_cast)#张量形状的修改a_p = tf.placeholder(shape=[None,None],dtype=tf.float32)b_p = tf.placeholder(shape=[None,10],dtype=tf.float32)c_p = tf.placeholder(shape=[4,2],dtype=tf.float32)#只能修改动态形状,而且要保证元素个数不变print("a_p  ===",a_p)print("b_p  ===",b_p)print("c_p  ===",c_p)#动态形状的修改:对于未确定的静态形状,才可以修改静态形状a_p.set_shape([2,3])b_p.set_shape([2,10])print("a_p  ===",a_p)print("b_p  ===",b_p)#动态形状的修改a_p_reshape = tf.reshape(a_p,[2,3,1])print("a_p_reshape  ===",a_p_reshape)c_p_reshape = tf.reshape(c_p,[2,4])#修改动态形状print("c_p_reshape  ===",c_p_reshape)c_p_reshape = tf.reshape(c_p,[2,2,2])print("c_p_reshape  ===",c_p_reshape)

变量

变量是用来保存模型参数的。

import tensorflow as tf#创建变量
#变量通过tf.Variable()创建,变量的特点是:
#存储持久化
#可修改值
#可指定被训练
def variable_demo():#修改命名空间,使结构更加清晰with tf.variable_scope('my_scope'):a = tf.Variable(initial_value = 50)b = tf.Variable(initial_value = 40)with tf.variable_scope('your_scope'):c = tf.add(a,b)print(a)print(b)print(c)#一定一定要全局变量初始化!!!init = tf.global_variables_initializer()with tf.Session() as sess:sess.run(init)a_value,b_value,c_value = sess.run([a,b,c])print(a_value)print(b_value)print(c_value)#tensorboard可视化,生成统计日志path = "./tmp/summary"tf.summary.FileWriter(path,graph = sess.graph)if __name__ == "__main__":variable_demo()

一个线性回归的案例

这是一个关于LR的实例,包括Tensorboard可视化,添加命名空间,事件文件数据的收集以及写入,代码都有注释。

要注意一下tf.summary.Filewriter()的返回值,也要注意tf.reduce_mean的用法,因为一个loss是一个100维的列向量,要注意一下。

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 11 19:03:16 2020@author: Administrator
"""'''
原理:
①构建模型:假设函数(y = w1*x1+w2*x2+……)
②构架损失函数:loss = 均方误差
③优化方法:梯度下降真实数据(100个):y_true = 0.8*x + 1
特征值  x  (100,1) 标签值 y_true  (100,1)
y_predict = x * weights(1,1) + bias(1,1)
loss = (y_predict - y_true)^2
'''import tensorflow as tf
import numpy as npDATA_SIZE = 100
ECOPE = 1000def linear_regression():#数据准备with tf.variable_scope("Data_preparation"):x = tf.random_normal(shape=[DATA_SIZE,1],dtype=tf.float32)y_true = tf.matmul(x,[[2.0]]) + 0.7#构建模型with tf.variable_scope("model"):weights = tf.Variable(initial_value=tf.random_normal(shape=[1,1]))bias = tf.Variable(initial_value=tf.random_normal(shape=[1,1]))y_predict = tf.matmul(x,weights) + bias#损失函数,注意reduce_meanwith tf.variable_scope("loss"):loss = tf.reduce_mean(tf.square(y_predict - y_true))#优化损失with tf.variable_scope("train"):optimization = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss)#全局变量初始化init = tf.global_variables_initializer()#收集事件文件中的变量tf.summary.scalar('loss',loss)##收集标量用scalartf.summary.histogram('weights',weights)#收集高维用histogramtf.summary.histogram('bias',bias)#收集高维用histogram#合并所收集到的变量merged = tf.summary.merge_all()with tf.Session() as sess:sess.run(init)#查看训练前的模型参数print("weights_before = ",weights.eval(),"bias_before = ",bias.eval())#创建事件文件path = "./tmp/summary"file_writer = tf.summary.FileWriter(path,graph = sess.graph)#开始训练for i in range(ECOPE):sess.run(optimization)print("average_loss =",loss.eval())#将每次收集的数据写入事件文件summary = sess.run(merged)file_writer.add_summary(summary,i)print("weights_after = ",weights.eval(),"bias_after = ",bias.eval())if __name__ == '__main__':linear_regression()

可视化结果:
loss:

模型结构:

bias:

weights:

【TensorFlow基础操作笔记】图+对话+张量+变量+线性回归实例相关推荐

  1. 深度学习(11)TensorFlow基础操作七: 向前传播(张量)实战

    深度学习(11)TensorFlow基础操作七: 向前传播(张量)实战 1. 导包 2. 加载数据集 3. 转换数据类型 4. 查看x.shape, y.shape, x.dtype, y.dtype ...

  2. python基础操作笔记

    python基础操作笔记 第二章 变量和简单的数据类型 #2.1输出数据hello world print('--------------------------------------------- ...

  3. tensorFlow基础操作及常用函数

    tensorFlow基础操作及常用函数 1. 安装Tensorflow 2. TensorFlow基本操作 3. TensorFlow常用函数 3.1 常用矩阵创建方式 3.2 高斯初始化及洗牌操作 ...

  4. 深度学习(10)TensorFlow基础操作六: 数学运算

    深度学习(10)TensorFlow基础操作六: 数学运算 1. Operation type 2. + - * / % // 3. tf.math.log & tf.exp 4. log2, ...

  5. 深度学习(9)TensorFlow基础操作五: Broadcasting

    深度学习(9)TensorFlow基础操作五: Broadcasting 1. 操作思想 2. 具体例子 3. 理解 (1) How to understand? (2) Why Broadcasti ...

  6. 深度学习(8)TensorFlow基础操作四: 维度变换

    深度学习(8)TensorFlow基础操作四: 维度变换 1. View 2. 示例 3. Reshape操作可能会导致潜在的bug 4. tf.transpose 5. Squeeze VS Exp ...

  7. 深度学习(7)TensorFlow基础操作三: 索引与切片

    深度学习(7)TensorFlow基础操作三: 索引与切片 一. 基础索引 1. Basic indexing 2. Numpy-style indexing 3. start : end 4. 切片 ...

  8. 深度学习(6)TensorFlow基础操作二: 创建Tensor

    深度学习(6)TensorFlow基础操作二: 创建Tensor 一. 创建方式 1. From Numpy,List 2. zeros,ones (1) tf.zeros() (2) tf.zero ...

  9. 深度学习(5)TensorFlow基础操作一: TensorFlow数据类型

    深度学习(5)TensorFlow基础操作一: TensorFlow数据类型 Data Container(数据载体) What's Tensor TF is a computing lib(科学计算 ...

最新文章

  1. metaq的简单封装dataChange解读
  2. linux @webserviceclient 访问超时_Linux系统调优
  3. Linux 环境下的高级隐藏技术
  4. BZOJ 1924 [Sdoi2010]所驼门王的宝藏
  5. 关于java类型数据组的调用
  6. 解决goldengate复制进程应用缓慢一例
  7. 写在这个公众号关注者达到7000之际,Jerry有话对大家说
  8. 黑鲨会升级鸿蒙吗,买华为别乱选!这3款才是“最佳选择”,未来能升级鸿蒙系统...
  9. C# 事件和Unity3D
  10. Rust :PhantomData、PhantomPinned
  11. 【初学者必知必会】【电子技术:数电 模电 单片机】【基础概念和小知识点】详解
  12. oracle jdbc流式读取,在 Oracle Database 适配器中流式传输大型对象数据类型 - BizTalk Server | Microsoft Docs...
  13. I Sold Out for the Cash - 2022/8/10
  14. 不是python中文件操作的相关函数是_以下选项中,不是Python中文件操作的相关函数是:...
  15. 趁我们还年轻,就应该奋斗
  16. 查看已知WiFi网络的密码
  17. 判断一个树是否为二叉查找树
  18. 3步轻松申请邮箱账号,申请163vip邮箱
  19. 如何利用软件绘制数学图像中的箭头坐标轴图像?
  20. 百度地图html演示,百度地图.html

热门文章

  1. 中国招聘网站调研报告
  2. 微信小程序开发者工具真机调试和预览连接本地服务器
  3. 重写equals方法一定要重写hashcode方法吗
  4. 让单身狗犹如过情人节的“网易云日推”原来是这样生成的
  5. 关于Android终端机串口的理解
  6. five 安卓应用|five 1.0.1 for android,Five小视频安卓版
  7. 【AWS】一、如何在AWS免费撸一年的服务器
  8. Mac下解决v2端口被占用,shadowsocket(ss)程序残留问题
  9. linux c python,Python 不是 C
  10. 你可能不知道的关于Oracle Rac的事...