当前位置:   article > 正文

CNN实例_cnn算法样例

cnn算法样例

机器学习算法完整版见fenghaootong-github

MINST

1 Data

1.1 get data

  • use panadas to read data from .csv file
import pandas as pd
  • 1
df = pd.read_csv('../DATA/train.csv')
  • 1
labels = df.as_matrix(columns=['label'])
  • 1
dataset = df.drop('label', axis=1).as_matrix()
  • 1
print('Got', len(dataset), 'training examples(with', len(labels), 'labels).')
  • 1
Got 4200 training examples(with 4200 labels).
  • 1
df = pd.read_csv('DATA/train.csv')
labels = df.as_matrix(columns=['label'])#find lable to transform to matrix
dataset = df.drop('label', axis=1).as_matrix()#transform dataset to matrxi without drop lable 
print('Got', len(dataset), 'training examples (with', len(labels), 'labels).')
  • 1
  • 2
  • 3
  • 4
Got 4200 training examples (with 4200 labels).
  • 1
  • split dataset into training and validation datasets
train_len = int(len(labels.ravel()) * 0.75)
train_dataset = dataset[:train_len]
train_labels = labels[:train_len]
valid_dataset = dataset[train_len:]
valid_labels = labels[train_len:]
  • 1
  • 2
  • 3
  • 4
  • 5

1.2 plot

  • we can use matplotlib to get better understanding of the data
import numpy as np
import matplotlib.pyplot as plt
  • 1
  • 2
def display_sample(dataset, rows=4, colunms=5):
    index = 1
    for image in dataset[:rows*colunms]:
        img = np.reshape(image, [28, 28])
        plt.subplot(rows, colunms, index)
        plt.axis('off')
        plt.imshow(img)
        index += 1
    plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
display_sample(train_dataset)
  • 1

这里写图片描述

1.3 Data validation

def count_each_label(labels):
    counters = np.zeros(10, int)
    for i in range(len(labels)):
        counters[labels[i]] += 1
    for i in range(10):
        print(i, ':', counters[i])
    print('\nmin:\t%d' % np.min(counters))
    print('mean:\t%d' % np.mean(counters))
    print('max:\t%d' % np.max(counters))
    print('stddev:\t%.2f' % np.std(counters))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
count_each_label(train_labels)
  • 1
0 : 319
1 : 354
2 : 343
3 : 286
4 : 310
5 : 301
6 : 320
7 : 318
8 : 295
9 : 304

min:	286
mean:	315
max:	354
stddev:	19.84
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2 Logistic Regression

  • First, using off-the-shelf classifier,it can give you a how result
def off_the_shelf():
    from sklearn.linear_model import LogisticRegression
    
    logreg = LogisticRegression(solver='sag', max_iter=256)
    %time logreg.fit(train_dataset, train_labels.ravel())
    
    print('Acc on train dataset: {:.2%}'.format(logreg.score(train_dataset, train_labels)))
    print('Acc on valid dataset: {:.2%}'.format(logreg.score(valid_dataset, valid_labels)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
off_the_shelf()
  • 1
C:\Users\htfenght\Anaconda3\lib\site-packages\sklearn\linear_model\sag.py:286: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge
  "the coef_ did not converge", ConvergenceWarning)


Wall time: 1min 11s
Acc on train dataset: 99.30%
Acc on valid dataset: 86.57%
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

**one-Hot encoding **

def one_hot_encode(labels, classes=10):
    one_hot = np.zeros([len(labels), 10])
    for i in range(len(labels)):
        one_hot[i, labels[i]] = 1.
    return one_hot
train_labels = one_hot_encode(train_labels)
valid_labels = one_hot_encode(valid_labels)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3 Multillayer Convolutional Network

3.1 Model

import tensorflow as tf
  • 1
class TFModel():
    def __init__(self):
        
        def weight_variable(shape):
            return tf.Variable(tf.truncated_normal(shape, stddev=0.1))
        
        def bias_variable(shape):
            return tf.Variable(tf.constant(0.1, shape=shape))
        
        def conv2d(x, W):
            return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
        
        def max_pool_2x2(x):
            return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                                  strides=[1, 2, 2, 1], padding='SAME')
        
        # feed dictionary entries needed
        self.x = tf.placeholder(tf.float32, shape=[None, 784])
        self.t = tf.placeholder(tf.float32, shape=[None, 10])
        self.keep_prob = tf.placeholder(tf.float32)
        
        # reshape inputs
        x_img = tf.reshape(self.x, [-1, 28, 28, 1])
        
        # first convolutional layer
        W_conv1 = weight_variable([5, 5, 1, 32])
        b_conv1 = bias_variable([32])
        
        h_conv1 = tf.nn.relu(conv2d(x_img, W_conv1) + b_conv1)
        h_pool1 = max_pool_2x2(h_conv1)
        
        # second convolutional layer
        W_conv2 = weight_variable([5, 5, 32, 64])
        b_conv2 = bias_variable([64])
        
        h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
        h_pool2 = max_pool_2x2(h_conv2)
        
        # fully connected layer
        W_fcl = weight_variable([7 * 7 * 64, 1024])
        b_fcl = bias_variable([1024])
        
        h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
        h_fcl = tf.nn.relu(tf.matmul(h_pool2_flat, W_fcl) + b_fcl)
        
        # dropout layer
        h_fcl_drop = tf.nn.dropout(h_fcl, self.keep_prob)
        
        # readout layer
        W_fc2 = weight_variable([1024, 10])
        b_fc2 = bias_variable([10])
        
        logits = tf.matmul(h_fcl_drop, W_fc2) + b_fc2
        
        # output
        self.y = tf.nn.softmax(logits)
        
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=self.t)
        cost = tf.reduce_mean(cross_entropy)
        self.train_step = tf.train.AdamOptimizer(1e-4).minimize(cost)
        
        correct_prediction = tf.equal(tf.argmax(self.y, 1), tf.argmax(self.t, 1))
        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

3.2 Training

def shuffle(a, b):
    p = np.random.permutation(len(a))
    return a[p], b[p]
  • 1
  • 2
  • 3
#help(np.random.permutation)
  • 1
import os

save_dir = 'save'
if not tf.gfile.Exists(save_dir):
    tf.gfile.MakeDirs(save_dir)
    print('save directory created')
else:
    print('save directory contains:', os.listdir(save_dir))

save_path = os.path.join(save_dir, 'model.ckpt')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
save directory created
  • 1

结果

batch_size = 50
max_step = 3000
keep_prob = 0.5

with tf.Session() as sess:
    m = TFModel()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()

    if tf.gfile.Exists(save_path):
        saver.restore(sess, save_path)
        print('model restored')
    else:
        print(save_path, 'doesn`t exist')

    p_acc = 0
    batch_start = 0
    batch_end = batch_start + batch_size
    for step in range(max_step):
        if step % 100 == 0:
            acc = sess.run(m.accuracy, {m.x : train_dataset[batch_start : batch_end],
                                        m.t : train_labels[batch_start : batch_end],
                                        m.keep_prob: 1.0})
            print('step {}, training accuracy: {:.0%}'.format(step, acc))

        """c
        if step % 1000 == 0:
            acc = sess.run(m.accuracy, {m.x : valid_dataset,
                                        m.t : valid_labels,
                                        m.keep_prob: 1.0})
            print('step {}, test accuracy: {:.2%}'.format(step, acc))
            if acc > p_acc:
                p_acc = acc
                saver.save(sess, save_path)
                print('model saved in', save_path)
        """

        sess.run(m.train_step, {m.x : train_dataset[batch_start : batch_end],
                                m.t : train_labels[batch_start : batch_end],
                                m.keep_prob: keep_prob})

        batch_start = batch_end
        batch_end += batch_size
        if batch_end > train_len:
            batch_start = 0
            batch_end = batch_start + batch_size
            train_dataset, train_labels = shuffle(train_dataset, train_labels)
    print('test accuracy: {:.2%}'.format(sess.run(m.accuracy, {m.x : valid_dataset,
                                                               m.t : valid_labels,
                                                               m.keep_prob: 1.0})))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
save\model.ckpt doesn`t exist
step 0, training accuracy: 6%
step 100, training accuracy: 92%
step 200, training accuracy: 92%
step 300, training accuracy: 98%
step 400, training accuracy: 98%
step 500, training accuracy: 98%
step 600, training accuracy: 96%
step 700, training accuracy: 100%
step 800, training accuracy: 100%
step 900, training accuracy: 98%
step 1000, training accuracy: 100%
step 1100, training accuracy: 100%
step 1200, training accuracy: 100%
step 1300, training accuracy: 100%
step 1400, training accuracy: 100%
step 1500, training accuracy: 100%
step 1600, training accuracy: 98%
step 1700, training accuracy: 100%
step 1800, training accuracy: 100%
step 1900, training accuracy: 100%
step 2000, training accuracy: 100%
step 2100, training accuracy: 100%
step 2200, training accuracy: 100%
step 2300, training accuracy: 100%
step 2400, training accuracy: 100%
step 2500, training accuracy: 100%
step 2600, training accuracy: 100%
step 2700, training accuracy: 100%
step 2800, training accuracy: 100%
step 2900, training accuracy: 100%
test accuracy: 94.67%
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/691168
推荐阅读
相关标签
  

闽ICP备14008679号