# 深度学习-图片识别（上）

## 任务一：下载并整理数据

• 下载包含10个字母的两份图片数据，large与small

large与small不止数量大小区别，还在于large中的图片有没经过人工筛选，有噪点以及损坏图片，从中选取数据作为训练集与验证集，small中的图片经过人工筛选，数据相对干净，从中选取数据作为测试集

• 处理每个字母文件夹

• 选取训练集、验证集、测试集

• 再次打乱顺序，并序列化到文件

### 任务作业

import numpy as np
import pickle
from sklearn.linear_model import LogisticRegression

image_size = 28
train_size = 1000

train_features = data['train_dataset'][:train_size]
train_labels = data['train_labels'][:train_size]

test_features = data['test_dataset']
test_labels = data['test_labels']

train_features = train_features.reshape(len(train_features), image_size * image_size)

test_features = test_features.reshape(len(test_features), image_size * image_size)

print 'start train!'
classifier = LogisticRegression()
classifier.fit(train_features, train_labels)

print 'start predict!'
score = classifier.score(test_features,test_labels)

print 'The accuray score is %.3f' % score

start train!
start predict!
The accuray score is 0.828

## 任务二：TF逻辑回归训练模型

• 重新格式化特征与标签

• 构造计算图

• 训练

• 使用随机梯度下降算法训练模型

### 任务作业

batch_size = 128
hidden_layer_node_num = 1024

graph = tf.Graph()
with graph.as_default():
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(valid_dataset)
tf_test_dataset = tf.constant(test_dataset)

hidden_weights = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_layer_node_num]))
hidden_biases = tf.Variable(tf.zeros([hidden_layer_node_num]))

weights = tf.Variable(tf.truncated_normal([hidden_layer_node_num, num_labels]))
biases = tf.Variable(tf.zeros([num_labels]))

logits = tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases), weights) + biases
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))

train_prediction = tf.nn.softmax(logits)
valid_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, hidden_weights) + hidden_biases), weights) + biases)
test_prediction = tf.nn.softmax(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, hidden_weights) + hidden_biases), weights) + biases)

## 任务三：优化模型

### L2正则化

loss = loss + 0.001438 * (tf.nn.l2_loss(hidden_weights) + tf.nn.l2_loss(hidden_biases) + tf.nn.l2_loss(weights) + tf.nn.l2_loss(biases))

### 过度拟合

batch_size = 128
train_dataset = train_dataset[:batch_size * 5, :]
train_labels = train_labels[:batch_size * 5, :]

### 丢弃法

dropout会随机将输入值丢弃或缩放，可达到训练模型冗余性，防止过度拟合。

tmp = tf.nn.relu(tf.matmul(tf_train_dataset, hidden_weights) + hidden_biases)
tmp = tf.nn.dropout(tmp, 0.5)
logits = tf.matmul(tmp, weights) + biases

dropout第二个参数0.5，说明dropout的行为是随机将部分值置为0，另外一部分x2，这样，整体样本的平均值不会变化。

### 多层神经网络

hlnn1 = 4096
hlnn2 = 2048
hlnn3 = 128

hw1 = tf.Variable(tf.truncated_normal([image_size * image_size, hlnn1], stddev=np.sqrt(2.0 / (image_size * image_size))))
hb1 = tf.Variable(tf.zeros([hlnn1]))

hw2 = tf.Variable(tf.truncated_normal([hlnn1, hlnn2], stddev=np.sqrt(2.0 / hlnn1)))
hb2 = tf.Variable(tf.zeros([hlnn2]))

hw3 = tf.Variable(tf.truncated_normal([hlnn2, hlnn3], stddev=np.sqrt(2.0 / hlnn2)))
hb3 = tf.Variable(tf.zeros([hlnn3]))

weights = tf.Variable(tf.truncated_normal([hlnn3, num_labels], stddev=np.sqrt(2.0 / hlnn3)))
biases = tf.Variable(tf.zeros([num_labels]))

tmp = tf.nn.relu(tf.matmul(tf_train_dataset, hw1) + hb1)
tmp = tf.nn.relu(tf.matmul(tmp, hw2) + hb2)
tmp = tf.nn.relu(tf.matmul(tmp, hw3) + hb3)

logits = tf.matmul(tmp, weights) + biases

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))
loss = loss + tf_beta * (tf.nn.l2_loss(hw1) + tf.nn.l2_loss(hb1) + tf.nn.l2_loss(hw2) + tf.nn.l2_loss(hb2) +
tf.nn.l2_loss(hw3) + tf.nn.l2_loss(hb3) + tf.nn.l2_loss(weights) + tf.nn.l2_loss(biases))

learning_rate = tf.train.exponential_decay(0.5, global_step, 1000, 0.7, staircase=True)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)