0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## 机器学习实战篇——用卷积神经网络算法在Kaggle上跑个分

hongtao2018 2018-06-18 19:58:00 浏览381

#### 1、导入库文件

``````import pandas as pd
import math
import numpy as np
import matplotlib.pyplot as plt, matplotlib.image as mpimg
from sklearn.model_selection import train_test_split
import tensorflow as tf

%matplotlib inline
``````

#### 2、准备数据

``````labeled_images = pd.read_csv('train.csv')
images = labeled_images.iloc[:,1:]
labels = labeled_images.iloc[:,:1]
train_images, test_images,train_labels, test_labels = train_test_split(images, labels, test_size=0.02)
``````

#### 3、创建帮助函数

##### One Hot Encode

``````def one_hot_encode(vec, vals=10):
'''
For use to one-hot encode the 10- possible labels
'''
n = len(vec)
out = np.zeros((n, vals))
out[range(n), vec] = 1
return out
``````
##### 帮助类

AI学习笔记——卷积神经网络（CNN）的文章中我们知道，一张图片有三个维度——长，宽，颜色通道。对于本文中的黑色图片，第三个维度为1。在加上样本的个数(n)，整个训练样本应该是一个(nx28x28x1)的四维Tensor(张量)。set_up_images(self)函数就是将图片转换成这样的Tensor。next_batch（）函数则是n个训练样本分成若干个batch, 一个一个地送给模型(这个叫mini batch)。

``````class CifarHelper():

def __init__(self):
self.i = 0

# Intialize some empty variables for later on
self.training_images = None
self.training_labels = None

self.test_images = None
self.test_labels = None

def set_up_images(self):

print("Setting Up Training Images and Labels")

# Vertically stacks the training images
self.training_images = train_images.as_matrix()
train_len = self.training_images.shape[0]

# Reshapes and normalizes training images
self.training_images = self.training_images.reshape(train_len,28,28,1)/255
# One hot Encodes the training labels (e.g. [0,0,0,1,0,0,0,0,0,0])
self.training_labels = one_hot_encode(train_labels.as_matrix().reshape(-1), 10)

print("Setting Up Test Images and Labels")

# Vertically stacks the test images
self.test_images = test_images.as_matrix()
test_len = self.test_images.shape[0]

# Reshapes and normalizes test images
self.test_images = self.test_images.reshape(test_len,28,28,1)/255
# One hot Encodes the test labels (e.g. [0,0,0,1,0,0,0,0,0,0])
self.test_labels = one_hot_encode(test_labels.as_matrix().reshape(-1), 10)

def next_batch(self, batch_size):
# Note that the 100 dimension in the reshape call is set by an assumed batch size of 100
x = self.training_images[self.i:self.i+batch_size]
y = self.training_labels[self.i:self.i+batch_size]
self.i = (self.i + batch_size) % len(self.training_images)
return x, y
``````

``````# Before Your tf.Session run these two lines
ch = CifarHelper()
ch.set_up_images()

# During your session to grab the next batch use this line
# (Just like we did for mnist.train.next_batch)
# batch = ch.next_batch(100)
``````

#### 4、创建模型

##### Placeholder

x 输入，y输出，hold_prob用于dropout(不多解释，主要用于随机丢弃神经元的一种正则化的方法)

``````x = tf.placeholder(tf.float32, shape=[None,28,28,1])
y_true = tf.placeholder(tf.float32, shape=[None,10])
hold_prob = tf.placeholder(tf.float32)
``````
##### Help functions

``````def init_weights(shape):
init_random_dist = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(init_random_dist)

def init_bias(shape):
init_bias_vals = tf.constant(0.1, shape=shape)
return tf.Variable(init_bias_vals)

def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

# x -->[batch, in_height, in_width, in_channels]
# W --> [filter_height, filter_width, in_channels, out_channels]

def max_pool_2by2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
def convolutional_layer(input_x, shape):
W = init_weights(shape)
b = init_bias([shape[3]])
return tf.nn.relu(conv2d(input_x, W) + b)

def normal_full_layer(input_layer, size):
input_size = int(input_layer.get_shape()[1])
W = init_weights([input_size, size])
b = init_bias([size])
return tf.matmul(input_layer, W) + b
``````
##### 搭建神经网络

``````convo_1 = convolutional_layer(x,shape=[6,6,1,32])
convo_1_pooling = max_pool_2by2(convo_1)
``````

``````convo_2 = convolutional_layer(convo_1_pooling,shape=[6,6,32,64])
convo_2_pooling = max_pool_2by2(convo_2)
``````

``````convo_2_flat = tf.reshape(convo_2_pooling,[-1,7*7*64])
full_layer_one = tf.nn.relu(normal_full_layer(convo_2_flat,1024))
``````

Dropout 和 输出

``````full_one_dropout = tf.nn.dropout(full_layer_one,keep_prob=hold_prob)
y_pred = normal_full_layer(full_one_dropout,10)
``````
##### 定义损失函数，和优化函数，初始化

Loss Function

``````cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true,logits=y_pred))

``````

Optimizer

``````optimizer = tf.train.AdamOptimizer(learning_rate=0.00002)
train = optimizer.minimize(cross_entropy)
``````

Intialize Variables

``````init = tf.global_variables_initializer()
``````

#### 5、训练模型

``````with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
``````

`````` for i in range(50000):
batch = ch.next_batch(100)
sess.run(train, feed_dict={x: batch[0], y_true: batch[1], hold_prob: 0.5})
``````

``````with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

for i in range(50000):
batch = ch.next_batch(100)
sess.run(train, feed_dict={x: batch[0], y_true: batch[1], hold_prob: 0.5})

# PRINT OUT A MESSAGE EVERY 100 STEPS
if i%100 == 0:

print('Currently on step {}'.format(i))
print('Accuracy is:')
# Test the Train Model
matches = tf.equal(tf.argmax(y_pred,1),tf.argmax(y_true,1))

acc = tf.reduce_mean(tf.cast(matches,tf.float32))

print(sess.run(acc,feed_dict={x:ch.test_images,y_true:ch.test_labels,hold_prob:1.0}))
print('\n')

saver.save(sess,'models_saving/my_model.ckpt')
``````

``````Currently on step 0
Accuracy is:
0.179762

Currently on step 100
Accuracy is:
0.584524

....
....
....

Currently on step 49900
Accuracy is:
0.983333

``````

————

AI学习笔记——神经网络和深度学习
AI学习笔记——卷积神经网络1（CNN）
————

hongtao2018
+ 关注

corcosa 12151人浏览