classification-softmax

softmax分类

import tensorflow as tf
import numpy as npfrom input_data import read_data_sets


mnist = read_data_sets('MNIST_data', one_hot=True)

def add_layer(inputs, in_size, out_size, active_function=None):
    """
    :param inputs:
    :param in_size: 行
    :param out_size: 列 , [行, 列] =矩阵
    :param active_function:
    :return:
    """
    with tf.name_scope('layer'):
        with tf.name_scope('weights'):
            W = tf.Variable(tf.random_normal([in_size, out_size]), name='W')  #
        with tf.name_scope('bias'):
            b = tf.Variable(tf.zeros([1, out_size]) + 0.1)  # b是代表每一行数据,对应out_size列个数据
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.matmul(inputs, W) + b
        if active_function is None:
            outputs = Wx_plus_b
        else:
            outputs = active_function(Wx_plus_b)
        return outputs


def compute_accuracy(v_xs, v_ys):
    """ 计算的准确率 """
    global prediction  # prediction value
    y_pre = sess.run(prediction, feed_dict={xs: v_xs})
    # 与期望的值比较 bool
    correct_pre = tf.equal(tf.argmax(y_pre, 1), tf.argmax(ys, 1))
    # 将bools转化为数字
    accuracy = tf.reduce_mean(tf.cast(correct_pre, tf.float32))
    result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
    return result


# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10])

# softmax + cross_entropy = classification
# add output layer
prediction = add_layer(xs, 784, 10, active_function=tf.nn.softmax)  # softmax分类

# the loss between prediction and really
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),
                                              reduction_indices=[1]))

# training
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.Session()
sess.run(tf.initialize_all_variables())

# start training
for i in range(1000):
    batch_x, batch_y = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={xs: batch_x, ys: batch_y})
    if i % 50 == 0:
        print(compute_accuracy(mnist.test.images, mnist.test.labels))

猜你喜欢

转载自www.cnblogs.com/tangpg/p/9212370.html
今日推荐