码迷,mamicode.com
首页 > 其他好文 > 详细

基于tensorflow的CNN卷积神经网络对Fasion-MNIST数据集的分类器

时间:2018-11-01 00:52:38      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:turn   dev   div   for   action   gpu   sof   dict   input   

写一个基于tensorflow的cnn,分类fasion-MNIST数据集

技术分享图片

这个就是fasion-mnist数据集了

先上代码,在分析:

import tensorflow as tf
import pandas as pd
import numpy as np

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.3

train_data = pd.read_csv(test.csv)
test_data = pd.read_csv(test.csv)


def Weight(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial, tf.float32)


def biases(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial, tf.float32)


def conv(inputs, w):
    return tf.nn.conv2d(inputs, w, strides=[1, 1, 1, 1], padding=SAME)


def pool(inputs):
    return tf.nn.max_pool(inputs, ksize=[1, 1, 1, 1], strides=[1, 2, 2, 1], padding=SAME)


x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.int64, [None])
x_image = tf.reshape(x, [-1, 28, 28, 1])

w1 = Weight([5, 5, 1, 32])
b1 = biases([32])
conv1 = tf.nn.relu(conv(x_image, w1) + b1)
p1 = pool(conv1)

w2 = Weight([5, 5, 32, 64])
b2 = biases([64])
conv2 = tf.nn.relu(conv(p1, w2) + b2)
p2 = pool(conv2)

flattended = tf.reshape(p2, [-1, 7 * 7 * 64])

w_fc1 = Weight([7 * 7 * 64, 1024])
b_fc1 = biases([1024])
fc1 = tf.matmul(flattended, w_fc1) + b_fc1
h_fc1 = tf.nn.relu(fc1)

w_fc2 = Weight([1024, 10])
b_fc2 = biases([10])
logits = tf.matmul(h_fc1, w_fc2) + b_fc2

cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)
correct_prediction = tf.equal(y, tf.argmax(logits, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

init = tf.global_variables_initializer()
sess = tf.Session(config=config)
sess.run(init)

for i in range(10000):
    choice = np.random.choice(6000, 100)
    batch = train_data.iloc[choice]
    labels = np.array(batch.iloc[:, 0])
    features = np.array(batch.iloc[:, 1:]).astype(np.float32)
    sess.run(train_step, feed_dict={x: features, y: labels})
    if i % 50 == 0:
        test_batch = test_data.iloc[0:1000, :]
        test_labes = np.array(test_batch.iloc[:, 0])
        test_features = np.array(test_batch.iloc[:, 1:]).astype(np.float32)
        print(sess.run(accuracy, feed_dict={x: test_features, y: test_labes}))
sess.close()

 

1.定义Weight, biases, conv层, pool层

def Weight(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial, tf.float32)


def biases(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial, tf.float32)


def conv(inputs, w):
    return tf.nn.conv2d(inputs, w, strides=[1, 1, 1, 1], padding=SAME)


def pool(inputs):
    return tf.nn.max_pool(inputs, ksize=[1, 1, 1, 1], strides=[1, 2, 2, 1], padding=SAME)

在这段代码中, 卷积层的,步幅都是1, 用SAME的padding方式,池化层的步幅是x y轴都是2, 这样,数据每次经过一次卷积和池化, 图像的宽和长都会变成原来的二分之一,也就是 原先 28x28 的图像 将会经过 14*14 到 7*7的变化

2. 定义placeholder

基于tensorflow的CNN卷积神经网络对Fasion-MNIST数据集的分类器

标签:turn   dev   div   for   action   gpu   sof   dict   input   

原文地址:https://www.cnblogs.com/francischeng/p/9886422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!