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

classification-softmax

时间:2018-06-22 13:27:49      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:cas   des   bsp   rect   func   rand   def   fine   net   

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))

 

classification-softmax

标签:cas   des   bsp   rect   func   rand   def   fine   net   

原文地址:https://www.cnblogs.com/tangpg/p/9212370.html

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