当前位置:网站首页>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的图像平均值 使用平均损失值代替某一维度上的损失矩阵
边栏推荐
- GKBillowNoiseSource
- 【栈的应用】--- 中缀表达式转后缀表达式
- Ueeditor v1.4.3 control file compression
- 13. Hash table - the first common node of two linked lists
- Codeforces Round #614 (Div. 2) B. JOE is on TV!
- (1) Summary of machine learning concepts
- GKCheckerboardNoiseSource
- GKObstacle
- Shortest path topic
- ZTE: 5nm 5g base station chip is being introduced!
猜你喜欢

生成对抗网络在DeepFake中的未来

15. Judge whether the target value exists in the two-dimensional array

SQL Server 2016 learning records - connection query

AP Autosar平台设计 3架构

Qt生成.exe文件 并 在无Qt环境下运行(Enigma Virtual Box进行绿色可执行软件封装)图文教程

安装office自定义项 安装期间出错 解决办法

SDUT Round 9 2020 Spring Festival campaign

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

Install mysql5.7 under centos7

第一篇:UniAPP的小程序跨端开发-----创建uniapp项目
随机推荐
字符串匹配
django-celery-redis异步发邮件
5、Window端实现Mapreduce程序完成wordcount功能
GKVoronoiNoiseSource
机器学习--手写英文字母2--导入与处理数据
Database security - create login user + configure permissions [notes]
【栈的应用】--- 中缀表达式转后缀表达式
机器人技术(RoboCup 2D)实验五十题及主要函数含义
ACM winter vacation training 4
Match file names from file paths using regular expressions
Codeforces Round #614 (Div. 2) B. JOE is on TV!
10. The penultimate node in the linked list
SQL Server 2016 学习记录 --- 数据更新
Problem summary file
Implement a queue with two stacks [C language]
Markdown to word or PDF
Yarn报错:Could not find any valid local directory for nmPrivate/
GKBillowNoiseSource
5. Dynamic programming -- Fibonacci series
Multithreading and high concurrency (III) -- source code analysis AQS principle