欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单的卷积神经网络

程序员文章站 2024-03-14 11:14:22
...

一、建立一个简单的卷积神经网络

简单的卷积神经网络

如上图所示,一个简单的卷积神经网络由卷积层,池化层,**层,全连接层组成。设计一个好的卷积神经网络不是简单的事情,包括卷积核大小的选择,比如1,3,5,7等几个常用的大小,还有卷积核的个数的选择,比如1,2,4,16,32等等,还有就是全连接层的**函数的选择等。下面我们就来简单看看卷积核个数和**函数的选择。

二、卷积核个数的选择

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import matplotlib.pyplot as plt
import csv

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


def weight(shape):
    init = tf.random_normal(shape=shape, stddev=0.1)
    return tf.Variable(init)


def baise(shape):
    init = tf.random_normal(shape=shape, stddev=0.00001)
    return tf.Variable(init)


def conv(x, w, k, b):
    return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, w, strides=[1, k, k, 1], padding="VALID"), b))


def max_pool(x, k):
    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding="VALID")


X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, 10])
X_input = tf.reshape(X, [-1, 28, 28, 1])
w1 = weight([5, 5, 1,16])
b1 = baise([16])
con1 = conv(X_input, w1, 1, b1)
pool1 = max_pool(con1, 2)
pool1 = tf.reshape(pool1, [-1, 12 * 12 * 16])
wc1 = weight([12 * 12 * 16, 10])
bc1 = baise([10])
fc1 = tf.matmul(pool1, wc1) + bc1
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=fc1))
correct_pred = tf.equal(tf.argmax(Y, 1), tf.argmax(fc1, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
step = tf.train.AdamOptimizer(0.01).minimize(loss)
init = tf.global_variables_initializer()
result_list = list()
result_list.append(["train_step", 'train_loss', "train_accuracy"])
with tf.Session() as sess:
    sess.run(init)
    costs = []
    for i in range(10):
        batch_x, batch_y = mnist.train.next_batch(64)
        cost, right, _ = sess.run([loss, accuracy, step], feed_dict={X: batch_x, Y: batch_y})
        costs.append(cost)
        print("第", i, "次迭代的损失:", cost, "accuracy:", '{:.2%}'.format(right))
        result_list.append([i, cost, right])
    result_file = open('result.csv', 'w', newline='')
    csv_write = csv.writer(result_file, dialect='excel')
    for row in result_list:
        csv_write.writerow(row)

    print("validation accuracy:",
          '{:.2%}'.format(accuracy.eval({X: mnist.validation.images, Y: mnist.validation.labels})))
简单的卷积神经网络
利用MNIST数据集做了几个简单的实验,分别把卷积核的个数设置为1,2,4,8,16,32个,在选择mnist_batch为64,循环次数为10次的条件下得出如上数据,随着核数量的增加,训练集的准确率逐渐提高。随着核个数的增加,提取的特征也就越多,训练速度下降但准确率不断提高。

三、全连接层**函数的选择

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import matplotlib.pyplot as plt
import csv

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


def weight(shape):
    init = tf.random_normal(shape=shape, stddev=0.1)
    return tf.Variable(init)


def baise(shape):
    init = tf.random_normal(shape=shape, stddev=0.00001)
    return tf.Variable(init)


def conv(x, w, k, b):
    return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, w, strides=[1, k, k, 1], padding="VALID"), b))


def max_pool(x, k):
    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding="VALID")


X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, 10])
X_input = tf.reshape(X, [-1, 28, 28, 1])
w1 = weight([5, 5, 1,16])
b1 = baise([16])
con1 = conv(X_input, w1, 1, b1)
pool1 = max_pool(con1, 2)
pool1 = tf.reshape(pool1, [-1, 12 * 12 * 16])
wc1 = weight([12 * 12 * 16, 10])
bc1 = baise([10])
fc1 = tf.nn.elu(tf.matmul(pool1, wc1) + bc1)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=fc1))
correct_pred = tf.equal(tf.argmax(Y, 1), tf.argmax(fc1, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
step = tf.train.AdamOptimizer(0.01).minimize(loss)
init = tf.global_variables_initializer()
result_list = list()
result_list.append(["train_step", 'train_loss', "train_accuracy"])
with tf.Session() as sess:
    sess.run(init)
    costs = []
    for i in range(10):
        batch_x, batch_y = mnist.train.next_batch(64)
        cost, right, _ = sess.run([loss, accuracy, step], feed_dict={X: batch_x, Y: batch_y})
        costs.append(cost)
        print("第", i, "次迭代的损失:", cost, "accuracy:", '{:.2%}'.format(right))
        result_list.append([i, cost, right])
    result_file = open('result.csv', 'w', newline='')
    csv_write = csv.writer(result_file, dialect='excel')
    for row in result_list:
        csv_write.writerow(row)

    print("validation accuracy:",
          '{:.2%}'.format(accuracy.eval({X: mnist.validation.images, Y: mnist.validation.labels})))

fc1为全连接层,分别尝试了relu,sigmoid,sodtplus,linear,elu,tanh几个**函数,得出以下数据.

简单的卷积神经网络

以上每个数据都可以在excel中画出图表,准确率最高的是linear,也就是fca=tf.matmul(pool1,wc1)+bc1。综合上述的两个实验可以得知,一般情况下,卷积核个数越多网络的准确率也会提高,但是是以计算速度为代价的。在卷积之后往往用的**函数是relu,全连接层用的是linear,输出层则用的softmax连接。具体到自己设计一个卷积神经网络时,可以先找找网上的网络构造,选取几个网络,在数据集上跑一次,综合比较不同的网络的结果。

相关标签: 算法 神经网络