当前位置:网站首页>Tensorflow 知识点
Tensorflow 知识点
2022-07-28 10:22:00 【上后左爱】
== 感觉TensorFlow知识 很薄弱,每天花点时间对基础的TensorFlow 知识进行了解, 同时知道每个函数的作用和其中转换的数据 ==
参考 TensorFlow 2.0:https://github.com/czy36mengfei/tensorflow2_tutorials_chinese
现阶段 项目还在使用TensorFlow 1.5 的工程,现在学习tensorflow 1.5 基础知识
在tensorflow 采用静态图 将定义与运行相分离,先用程序搭建一个结构,让数据按照图的结构顺序计算,tensorflow2.0 默认动态图,可以在1.x 可以边调试边运算
## 随机产生batch_size 的 小矩阵数据
def batch_generator(X,y,batch_size):
size = X.shape[0]
X_copy = X.copy()
y_copy = y.copy()
indices = np.arange(size) # array([0,1,2,3...size])
X_copy = X_copy[indices]
print(X_copy)
y_copy = y_copy[indices]
i = 0;
while True:
if i + batch_size <= size:
yield X_copy[i:i+batch_size],y_copy[i:i+batch_size]
i += batch_size
else:
i = 0
indices = np.arange(size)
np.random.shuffle(indices) ## 随机的调换indices 的列顺序
X_copy = X_copy[indices]
y_copy = y_copy[indices]
continue
## tf.placeholder(dtype,shape=None,name= None)
/** dtype 数据类型,常用数据类型 tf.float32
* shape None 是一维数组 [None, 3] 列3 行不定
* feed_dict : 回馈矩阵中 输入初始化数据
example: compute 3*12
*/
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
print (sess.run(output, feed_dict={
input1: [3.], input2: [4.]}))
总结: feed_dict作用给使用placeholder 创建出对象赋予tensor 值,使用sess.run() 并不是计算整个张量,只计算第一个参数中函数值, [loss, output] 只计算这两部分值,知识计算与fetch_dict 的值相关部分
tf.name_scope 和 tf.variable_scope 在模型中开辟各自空间,其中变量在各自空间内进行管理
- tf.Vaiable 函数:
tf.Variable(initilaizer, name), initilaizer 参数 tf.random_normal, tf.constant等参数
## tf.random_uniform((4,4), minval= low,maxval=high,dtype=tf.float32): 返回一个在low-high 之间的均值分布的 4*4 矩阵
with tf.Session() as sess:
print(sess.run(tf.random_uniform(
(4, 4), minval=-0.5,
maxval=0.5, dtype=tf.float32)))
- tf.nn.embedding_lookup:
选取一个张量里面索引对应的元素
tf.nn.embedding_lookup(tensor, id):tensor就是输入张量,id就是张量对应的索引
如下 id =[1,3] 选取1,3 索引位置上张量
c = np.random.random([10, 1])
b = tf.nn.embedding_lookup(c, [1, 3])
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print (sess.run(b))
print(c)
结果:b: [[0.60539241]
[0.48570814]]
c: [[0.12925993]
[0.60539241]
[0.85519417]
[0.48570814]
[0.42811298]
[0.70807729]
[0.64743353]
[0.35472522]
[0.30595551]
[0.67203577]]
- tf.summary.histogram()
查看一个张量在训练过程中分布情况,可视化张量在不同时间点直方图显示分布随时间变化情况
- tensorflow 几种计算交叉熵方式,计算每个样本loss
tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,labels=None, logits=None, name=None)
最重要参数 logits,labels,过程中: 输入的logits 先通过sigmoid函数计算,在计算器交叉熵,但是对其交叉熵的计算方式进行优化,输出结果output 是一个batch 中每一个样本loss 与后面的tf.reduce_mean(loss) 使用其中与之类似还有:- tf.nn.sparse_softmax_cross_entropy_with_logits(_sentinel=None,labels=None,logits=None, name=None)
- tf.nn.weighted_cross_entropy_with_logits(labels,logits, pos_weight, name=None)
- tf.nn.softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, dim=-1, name=None)
- tf.reduce_mean:
tf.cast(A,tf.float32): 用于改变某个张量的数据类型
tf.reduce_mean():计算张量tensor 沿着某一维度上平均值,主要用作降温或者计算tensor的图像平均值 使用平均损失值代替某一维度上的损失矩阵
边栏推荐
- SQL Server 2016 learning record - Data Definition
- Use of Ogg parameter filter [urgent]
- Small knowledge in Oracle
- SQL Server 2016 learning records - View
- 数据库安全 --- 创建登录名 用户+配置权限【笔记】
- string matching
- Yarn报错:Exception message: /bin/bash: line 0: fg: no job control
- SQL Server 2016 learning records - data update
- Machine learning -- handwritten English alphabet 2 -- importing and processing data
- RoboCup (2D) experiment 50 questions and the meaning of main functions
猜你喜欢

机器学习--手写英文字母3--工程特点

ACM winter vacation training 5

最短路专题
![Implement a queue with two stacks [C language]](/img/8a/679575bb0a562eff7e4317e64b4790.png)
Implement a queue with two stacks [C language]

Chapter 1: cross end development of small programs of uniapp ----- create a uniapp project

5. Dynamic programming -- Fibonacci series

Machine learning -- handwritten English alphabet 3 -- engineering features

Install Office customization. Troubleshooting during installation

AP AUTOSAR platform design 3 architecture

14. Double pointer - the container that holds the most water
随机推荐
SQL Server 2016 学习记录 ---视图
AP AUTOSAR platform design 1-2 introduction, technical scope and method
20200229 training match L1 - 2 delete the substring in the string (20 points)
Codeforces Round #614 (Div. 2) A. ConneR and the A.R.C. Markland-N
机器学习--手写英文字母1--分类流程
The 11th Blue Bridge Cup Undergraduate group competition (20200321)
SQL Server 2016 learning records - data update
Install MySQL under centos7, and the online articles are not accurate
Can kingbasees v8r6 JDBC use VIP?
Shortest path topic
GKPerlinNoiseSource
Codeforces Round #614 (Div. 2) B. JOE is on TV!
GKNoise
ogg里用多个filter语法应该怎么写?
第11届蓝桥杯本科组校赛(20200321)
How to write Ogg with multiple filter syntax?
287. Find the Duplicate Number
7、MapReduce自定义排序实现
ACM winter vacation training 6
ogg参数filter的使用问题【急】