Tensorflow 学习笔记
# 在用tensorflow前先打开
$ source ~/tensorflow/bin/activate
# 激活后会有标识
(tensorflow)$
#用完后关闭
(tensorflow)$ deactivate
# 删除tensorflow
$ rm -r ~/tensorflow
1. Getting Started With Tensorflow
Tensorflow的基本单元是张量(tensor),张量的阶(rank)是它的维度(the number of dimension)。
Tensorflow由一系列运算图(computational graph)组成。点有多种类型,包括常数(constant)、变量(variable)。
要操作computational graph中的node,要用到session。
sess = tf.Session() print(sess.run([node1, node2]))
可视化: TensorBoard可以用来表现computational graph。
用placeholder可以完成类似函数(function)的功能
a = tf.placeholder(tf.float32) b = tf.placeholder(tf.float32) adder_node = a + b # + provides a shortcut for tf.add(a, b)
print(sess.run(adder_node, {a: 3, b: 4.5}))
利用变量(variables)给节点添加参数
2. Deep MNIST for Experts
InteractiveSession class is more convenient and flexible.
import tensorflow as tf sess = tf.InteractiveSession()
Placeholders with shape argument can automatically catch bugs regarding on manipulation on inconsistent tensor shape.
x = tf.placeholder(tf.float32, shape=[None, 784]) # None means that the number of sample is variable and the length of each sample is 784 (28 * 28)
包含Deep convolutional neural networks的使用教程。
3. Read Data
batchsize: 每次训练在训练集中取得样本数
iteration: 使用batchsize个样本训练一次
epoch: 使用训练集中的全部样本训练一次
# 读入的数据以列表存储
filename = []
# tf.train.string_input_producer() 产生一个文件名队列
filename_queue = tf.train.string_input_producer(filename, shuffle=False, num_epochs=5)# reader从文件名队列中读数据。对应的方法是reader.read
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
# tf.train.string_input_producer定义了一个epoch变量,要对它进行初始化
tf.local_variables_initializer().run()
# 使用start_queue_runners之后,才会开始填充队列
threads = tf.train.start_queue_runners(sess=sess)
详见: http://blog.csdn.net/buptgshengod/article/details/72956846
numpy
import numpy as np np.loadtext(c) # return ndarray
np.ones() # return a new array of given shape and type, filled with ones
i = 1 * 10 ** (-10) # ** means power, and its priority is higher than *
np.eye(3, dtype = int) # diagonal matrix, consist of 0 and 1
vector = np.hstack((vector, temp)) # concatenation along the second axis (horizontally in 2 dimension)
np.save(filename, array, fmt = '%d') # save the data of int format
4. make hand dirty
numpy一位数组转置只需要操作shape
array_1d = np.array([1, 2]) array_1d.shape = (2, 1) # finished array_2d.transpose() # 二维数组的转置方法
python assert断言是声明其布尔值必须为真的判定
assert len(lists) >=5 # 若为假则触发异常
使用名称前的单下划线,用于指定该名称属性为“私有”,其只供内部使用。 (_spam)
collections是一个集合模块,提供许多有用的集合类,如namedtuple(提供一个列表的集合类)
>>> from collections import namedtuple >>> Point = namedtuple('Point', ['x', 'y']) >>> p = Point(1, 2) >>> p.x 1 >>> p.y 2
5. Model
strides 中的四个参数,第一个是 the number of images,第二个是 the height of images,第三个是 the width of images,第四个是 the number of channel
小白学Tensorflow之卷积神经网络 https://www.jianshu.com/p/70c8d6663b00
深入MNIST http://www.tensorfly.cn/tfdoc/tutorials/mnist_pros.html
Batch Normalization 批标准化 https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/5-13-BN/
martin-gorner/tensorflow-mnist-tutorial (batch normalization)https://github.com/martin-gorner/tensorflow-mnist-tutorial
6. 求AUC
import tensorflow as tf a = tf.Variable([0.1, 0.5]) b = tf.Variable([0.2, 0.6]) auc = tf.contrib.metrics.streaming_auc(a, b) sess = tf.Session() sess.run(tf.initialize_all_variables()) sess.run(tf.initialize_local_variables()) # try commenting this line and you'll get the error train_auc = sess.run(auc) print(train_auc)
https://stackoverflow.com/questions/39435341/how-to-calculate-auc-with-tensorflow
7. 可视化 (好)
Tensorflow的可视化工具Tensorboard的初步使用
https://blog.csdn.net/mao_feng/article/details/54731098
tensorflow + mnist + cnn + batch normalization
https://github.com/hwalsuklee/tensorflow-mnist-cnn
8. 过拟合情况及解决
dropout https://blog.csdn.net/smf0504/article/details/55254818
batch normalization https://blog.csdn.net/whitesilence/article/details/75667002
slim cnn 代码 https://www.2cto.com/kf/201706/649266.html
9.论文写法
https://blog.csdn.net/fengbingchun/article/details/50529500 cnn 结构