刷代码:LeetCode,github,kaggle, learning python hard way, 书,微信
python基础(hard way→python算法书(算法导论-难,看不懂再看算法第四版-易)→leetcode)→numpy, pandas(官方10 minutes to pandas), matplotlib等库,有书→机器学习基础算法(GitHub手写机器学习算法,siraj线性回归的视频),原理和代码→项目,paper
github:tensorflow example
课前环境准备:见论坛收藏的帖子(还需要将cudnn解压出的cuda/bin路径(C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\cudnn-8.0-windows10-x64-v5.1\cuda\bin)加入系统path)
tflearn课程中(env-tflearn),安装TensorFlow报错:ImportError: No module named 'setuptools.wheel'
解决:conda install -c https://conda.anaconda.org/anaconda setuptools,然后再装TensorFlow
numpy基础
叉乘:符号为*,元素级乘法,每个对应位置的元素相乘
矩阵点积:符号为.,np.matmul(a, b)),np.dot(a, b)或a.dot(b)(如果a, b是二维的,返回一个值不改变a)
矩阵转置:
b = a.T,修改b中的数据,则原始举证a中对应转置的位置的数据也会改变,因为它们共享相同的数据副本,将转置视为矩阵的不同视图,而不是完全不同的矩阵。(这个原理也适用于b = a.reshape())
神经网络
简易神经网络

感知器perceptron
权重weight
激活函数,最简单的激活函数(单位阶跃函数)

偏置项bias
感知器公式


常用的激活函数:单位阶跃函数,sigmoid,tanh,softmax
梯度下降
梯度是变化率或者斜率的另一个称呼,是对多变量函数导数的泛化
梯度下降数学推导








反向传播例子


实现反向传播

修正线性单元(ReLU),隐藏层
f(x) = max(x, 0)
softmax

输出经过压缩,输出和为1
softmax可用于任何数量的分类,是神经网络预测多分类问题的最佳输出激活函数
交叉熵cross entropy

tensorflow:
with tf.Session() as sess:
tf.constant()
# 得到输入
x = tf.placeholder(tf.string)
with tf.Session() as sess:
output = sess.run(x, feed_dict={x: 'hello, world'})
tf.Variable() # 可变tensor
tf.global_variables_initializer() # 初始化所有可变tensor
# 数学运算,数据必须是同一类型
tf.add(a, b)
tf.subtract(a, b)
tf.multiply(a, b)
tf.divide(a, b)
tf.cast(tf.constant(2.0), tf.int32) # 强制转换数据类型为int32
tf.truncated_normal((n, m)) # 从正态分布中生成随机数
tf.zeros()
tf.matmul(a, b) # 矩阵相乘
tf.nn.softmax() # 计算softmax
tf.nn.relu()
# One-Hot Encoding
import numpy as np
from sklearn import preprocessing
labels = np.array(['a', 'f', 'd'])
# create the encoder
lb = preprocessing.LabelBinarizer()
lb.fit(labels)
lb.transform(labels)
tf.reduce_sum() # 返回序列的和
tf.log() # 返回输入值的自然对数
tf.nn.dropout(layer, keep_prob) # 训练时keep_prob设为0.5,验证和测试时设为1
conv_layer = tf.nn.conv2d(input, weight, strides=[1, 2, 2, 1], padding='SAME')
conv_layer = tf.nn.bias_add(conv_layer, bias) # 对矩阵的最后一维加了偏置项
#weights 作为滤波器,[1, 2, 2, 1] 作为 strides。TensorFlow 对每一个 input 维度使用一个单独的 stride 参数,[batch, input_height, input_width, input_channels]。我们通常把 batch 和 input_channels (strides 序列中的第一个第四个)的 stride 设为 1。
conv_layer = tf.nn.max_pool(conv_layer, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# tf.nn.max_pool() 函数实现最大池化时, `ksize`参数是滤波器大小`strides`参数是步长。2x2 的滤波器配合 2x2 的步长是常用设定。
#`ksize` 和 `strides` 参数也被构建为四个元素的列表,每个元素对应 input tensor 的一个维度 (`[batch, height, width, channels]`),对 `ksize` 和 `strides` 来说,batch 和 channel 通常都设置成 `1`。
learning_rate 不宜太高,如果准确率太低,首先尝试调低学习率
超参数,ADAGRAD
batch_size一般取32,64,128,256等



网友评论