Tensorflow 知识点

== 感觉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) 使用 其中与之类似还有:
    1. tf.nn.sparse_softmax_cross_entropy_with_logits(_sentinel=None,labels=None,logits=None, name=None)
    2. tf.nn.weighted_cross_entropy_with_logits(labels,logits, pos_weight, name=None)
    3. 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的图像平均值 使用平均损失值代替某一维度上的损失矩阵
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值