tensorflow学习函数笔记

    xiaoxiao2021-04-15  242

    [TensorFlow教程资源](https://my.oschina.net/u/3787228/blog/1794868](https://my.oschina.net/u/3787228/blog/1794868 “TensorFlow教程资源”) 教程资源2 深度学习入门笔记系列 ( 八 ) ——基于 tensorflow 的手写数字的识别(进阶)

    函数一

    tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 除name参数指定操作的name,共五个参数:

    第一个参数input:指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一 第二个参数filter:相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维 第三个参数strides:卷积时在图像每一维的步长,这是一个一维的向量[ 1, strides, strides, 1],长度4。第一位和最后一位固定必须是1 第四个参数padding:string类型的量,只能是"SAME","VALID"其中之一,表示的是卷积的形式,是否考虑边界。"SAME"是考虑边界,不足的时候用0去填充周围,"VALID"则不考虑。这个值决定了不同的卷积方式(后面会介绍) 第五个参数:use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true 结果返回一个Tensor,这个输出,就是我们常说的feature map,shape仍然是[batch, height, width, channels]这种形式。

    code

    import tensorflow as tf # case 1 # 输入是1张 3*3 大小的图片,图像通道数是5,卷积核是 1*1 大小,数量是1 # 步长是[1,1,1,1]最后得到一个 3*3 的feature map # 1张图最后输出就是一个 shape为[1,3,3,1] 的张量 input = tf.Variable(tf.random_normal([1,3,3,5])) filter = tf.Variable(tf.random_normal([1,1,5,1])) op1 = tf.nn.conv2d(input, filter, strides=[1,1,1,1], padding='SAME') # case 2 # 输入是1张 3*3 大小的图片,图像通道数是5,卷积核是 2*2 大小,数量是1 # 步长是[1,1,1,1]最后得到一个 3*3 的feature map # 1张图最后输出就是一个 shape为[1,3,3,1] 的张量 input = tf.Variable(tf.random_normal([1,3,3,5])) filter = tf.Variable(tf.random_normal([2,2,5,1])) op2 = tf.nn.conv2d(input, filter, strides=[1,1,1,1], padding='SAME') # case 3 # 输入是1张 3*3 大小的图片,图像通道数是5,卷积核是 3*3 大小,数量是1 # 步长是[1,1,1,1]最后得到一个 1*1 的feature map (不考虑边界) # 1张图最后输出就是一个 shape为[1,1,1,1] 的张量 input = tf.Variable(tf.random_normal([1,3,3,5])) filter = tf.Variable(tf.random_normal([3,3,5,1])) op3 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID') # case 4 # 输入是1张 5*5 大小的图片,图像通道数是5,卷积核是 3*3 大小,数量是1 # 步长是[1,1,1,1]最后得到一个 3*3 的feature map (不考虑边界) # 1张图最后输出就是一个 shape为[1,3,3,1] 的张量 input = tf.Variable(tf.random_normal([1,5,5,5])) filter = tf.Variable(tf.random_normal([3,3,5,1])) op4 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID') # case 5 # 输入是1张 5*5 大小的图片,图像通道数是5,卷积核是 3*3 大小,数量是1 # 步长是[1,1,1,1]最后得到一个 5*5 的feature map (考虑边界) # 1张图最后输出就是一个 shape为[1,5,5,1] 的张量 input = tf.Variable(tf.random_normal([1,5,5,5])) filter = tf.Variable(tf.random_normal([3,3,5,1])) op5 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME') # case 6 # 输入是1张 5*5 大小的图片,图像通道数是5,卷积核是 3*3 大小,数量是7 # 步长是[1,1,1,1]最后得到一个 5*5 的feature map (考虑边界) # 1张图最后输出就是一个 shape为[1,5,5,7] 的张量 input = tf.Variable(tf.random_normal([1,5,5,5])) filter = tf.Variable(tf.random_normal([3,3,5,7])) op6 = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME') # case 7 # 输入是1张 5*5 大小的图片,图像通道数是5,卷积核是 3*3 大小,数量是7 # 步长是[1,2,2,1]最后得到7个 3*3 的feature map (考虑边界) # 1张图最后输出就是一个 shape为[1,3,3,7] 的张量 input = tf.Variable(tf.random_normal([1,5,5,5])) filter = tf.Variable(tf.random_normal([3,3,5,7])) op7 = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME') # case 8 # 输入是10 张 5*5 大小的图片,图像通道数是5,卷积核是 3*3 大小,数量是7 # 步长是[1,2,2,1]最后每张图得到7个 3*3 的feature map (考虑边界) # 10张图最后输出就是一个 shape为[10,3,3,7] 的张量 input = tf.Variable(tf.random_normal([10,5,5,5])) filter = tf.Variable(tf.random_normal([3,3,5,7])) op8 = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME') init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) print('*' * 20 + ' op1 ' + '*' * 20) print(sess.run(op1)) print('*' * 20 + ' op2 ' + '*' * 20) print(sess.run(op2)) print('*' * 20 + ' op3 ' + '*' * 20) print(sess.run(op3)) print('*' * 20 + ' op4 ' + '*' * 20) print(sess.run(op4)) print('*' * 20 + ' op5 ' + '*' * 20) print(sess.run(op5)) print('*' * 20 + ' op6 ' + '*' * 20) print(sess.run(op6)) print('*' * 20 + ' op7 ' + '*' * 20) print(sess.run(op7)) print('*' * 20 + ' op8 ' + '*' * 20) print(sess.run(op8))

    函数二

    tf.get_variable(name,  shape, initializer): name就是变量的名称,shape是变量的维度,initializer是变量初始化的方式,初始化的方式有以下几种: tf.constant_initializer:常量初始化函数 tf.random_normal_initializer:正态分布 tf.truncated_normal_initializer:截取的正态分布 tf.random_uniform_initializer:均匀分布 tf.zeros_initializer:全部是0 tf.ones_initializer:全是1 tf.uniform_unit_scaling_initializer:满足均匀分布,但不影响输出数量级的随机值

    code

    import tensorflow as tf; import numpy as np; import matplotlib.pyplot as p a1 = tf.get_variable(name='a1', shape=[2,3], initializer=tf.random_normal_initializer(mean=0, stddev=1)) a2 = tf.get_variable(name='a2', shape=[1], initializer=tf.constant_initializer(1)) a3 = tf.get_variable(name='a3', shape=[2,3], initializer=tf.ones_initializer()) with tf.Session() as sess: sess.run(tf.initialize_all_variables()) print sess.run(a1) print sess.run(a2) print sess.run(a3)

    函数三

    tf.nn.bias_add():一个叫bias的向量加到一个叫value的矩阵上,是向量与矩阵的每一行进行相加,得到的结果和value矩阵大小相同。 code

    import tensorflow as tf a=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32) b=tf.constant([1,-1],dtype=tf.float32) c=tf.constant([1],dtype=tf.float32) with tf.Session() as sess: print('bias_add:') print(sess.run(tf.nn.bias_add(a, b)))

    函数四

    tf.nn.relu(features, name = None):这个函数的作用是计算激活函数 relu,即 max(features, 0)。即将矩阵中每行的非大于0的置0。 code

    import tensorflow as tf a = tf.constant([-1.0, 2.0,1.0]) with tf.Session() as sess: b = tf.nn.relu(a) print(sess.run(b))

    函数五

    tf.nn.max_pool(value, ksize, strides, padding, name=None):具体参数函数一,链接

    参数是四个,和卷积很类似: 第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape 第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1 第三个参数strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1] 第四个参数padding:和卷积类似,可以取'VALID' 或者'SAME' 返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式

    函数六

    tf.nn.lrn(input,depth_radius=None,bias=None,alpha=None,beta=None,name=None):局部响应归一化,想了解原理的点这个 参考链接1 参考链接2

    函数七

    tf.matmul() 和tf.multiply() 的区别 链接

    函数八

    tf.argmax()和tf.arg_max()功能类似: 分为tf.argmax(array, 1)和tf.argmax(array, 0)两种,其中前者返回每行的最大数的下标,后者表示每列的最大值的下表 例如: test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]]) np.argmax(test, 0)   #输出:array([3, 3, 1] np.argmax(test, 1)   #输出:array([2, 2, 0, 0]

    函数九

    reduce_sum应该理解为压缩求和,用于降维 x=[[1, 1, 1] [1, 1, 1]] #求和 tf.reduce_sum(x) ==> 6 #按列求和 tf.reduce_sum(x, 0) ==> [2, 2, 2] #按行求和 tf.reduce_sum(x, 1) ==> [3, 3] #按照行的维度求和 tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]] #行列求和 tf.reduce_sum(x, [0, 1]) ==> 6

    函数十

    tf.nn.sparse_softmax_cross_entropy_with_logits(): 参考链接1 参考链接2 https://blog.csdn.net/mieleizhi0522/article/details/80200126 https://www.cnblogs.com/kyrieng/p/8694705.html code

    import tensorflow as tf # our NN's output logits = tf.constant([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]) # step1:do softmax y = tf.nn.softmax(logits) # true label # 注意这里标签必须是浮点数,不然在后面计算tf.multiply时就会因为类型不匹配tf_log的float32数据类型而出错 y_ = tf.constant([[0, 0, 1.0], [0, 0, 1.0], [0, 0, 1.0]]) # 这个是稀疏的标签 # step2:do log tf_log = tf.log(y) # step3:do mult pixel_wise_mult = tf.multiply(y_, tf_log) # step4:do cross_entropy cross_entropy = -tf.reduce_sum(pixel_wise_mult) # do cross_entropy just two step # 将标签稠密化 dense_y = tf.arg_max(y_, 1) cross_entropy2_step1 = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=dense_y, logits=logits) cross_entropy2_step2 = tf.reduce_sum(cross_entropy2_step1) # dont forget tf.reduce_sum()!! with tf.Session() as sess: y_value, tf_log_value, pixel_wise_mult_value, cross_entropy_value = sess.run( [y, tf_log, pixel_wise_mult, cross_entropy]) sparse_cross_entropy2_step1_value, sparse_cross_entropy2_step2_value = sess.run( [cross_entropy2_step1, cross_entropy2_step2]) print("step1:softmax result=\n%s\n" % (y_value)) print("step2:tf_log_result result=\n%s\n" % (tf_log_value)) print("step3:pixel_mult=\n%s\n" % (pixel_wise_mult_value)) print("step4:cross_entropy result=\n%s\n" % (cross_entropy_value)) print("Function(softmax_cross_entropy_with_logits) result=\n%s\n" % (sparse_cross_entropy2_step1_value)) print("Function(tf.reduce_sum) result=\n%s\n" % (sparse_cross_entropy2_step2_value))

    tf.nn.in_top_k()

    tf.nn.in_top_k组要是用于计算预测的结果和实际结果的是否相等,返回一个bool类型的张量,tf.nn.in_top_k(prediction, target, K):prediction就是表示你预测的结果,大小就是预测样本的数量乘以输出的维度,类型是tf.float32等。target就是实际样本类别的标签,大小就是样本数量的个数。K表示每个样本的预测结果的前K个最大的数里面是否含有target中的值。一般都是取1。

    import tensorflow as tf; A = [[0.8,0.6,0.3], [0.1,0.6,0.4]] B = [1, 1] out = tf.nn.in_top_k(A, B, 1) with tf.Session() as sess: sess.run(tf.initialize_all_variables()) print(sess.run(out)) 输出: [False True] ## 解释:因为A张量里面的第一个元素的最大值的标签是0,第二个元素的最大值的标签是1.。但是实际的确是1和1.所以输出就是False 和True。如果把K改成2,那么第一个元素的前面2个最大的元素的位置是0,1,第二个的就是1,2。实际结果是1和1。包含在里面,所以输出结果就是True 和True.如果K的值大于张量A的列,那就表示输出结果都是true

    tensorflow.placeholder()函数

    tensorflow.placeholder(dtype, shape=None, name=None) 参数:

    dtype:数据类型。常用的是tf.float32,tf.float64等数值类型

    shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定

    name:名称。 x = tf.placeholder(tf.float32, shape=(1024, 1024)) y = tf.matmul(x, x) with tf.Session() as sess: print(sess.run(y)) # ERROR: 此处x还没有赋值. rand_array = np.random.rand(1024, 1024) print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.

    #!/usr/bin/env python # _*_ coding: utf-8 _*_ import tensorflow as tf import numpy as np # 定义placeholder input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) # 定义乘法运算 output = tf.multiply(input1, input2) # 通过session执行乘法运行 with tf.Session() as sess: # 执行时要传入placeholder的值 print sess.run(output, feed_dict = {input1:[7.], input2: [2.]})

    tf.train.slice_input_producer

    参考博文1 参考博文2 参考博文3

    tf.nn.embedding_lookup

    参考博文1 参考博文2

    Python标准库——collections模块的Counter类

    sort的高级用法:sorted(l, key=lambda x:x[0])

    Python的Tqdm模块——进度条配置

    numpy库数组拼接np.concatenate官方文档详解与实例

    TensorFlow用expand_dim()来增加维度

    tf.nn.l2_loss()

    tf.where和tf.greater一起用

    tf.concat()详解

    FLAGS = tf.flags.FLAGS详解 tf.app.run()与命令行参数解析

    Tensorflow VocabularyProcessor API tf.ConfigProto()

    tf.device()指定tensorflow运行的GPU或CPU设备

    tensorflow中tf.nn.xw_plus_b compute_gradients1 compute_gradients2


    最新回复(0)