tensorflow 实现softmax

利用tensorflow 实现多分类问题,数据为mnist

import tensorflow as tf
import numpy as np
from sklearn import datasets
from  tensorflow.examples.tutorials.mnist import input_data


def read_file():
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    train_x, train_y, test_x, test_y = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
    print ("downloading")
    return train_x, train_y, test_x, test_y

def weights_biases_placeholder(n_dim, n_classes):
    X = tf.placeholder(tf.float32, [None, n_dim])
    Y = tf.placeholder(tf.float32, [None, n_classes])
    w = tf.Variable(tf.random_normal([n_dim, n_classes], stddev=0.01), name='weights')
    b = tf.Variable(tf.random_normal([n_classes]), name='biases')
    return X, Y, w, b

def forward_pass(X, w, b):
    out = tf.matmul(X, w) + b
    return out

def multiclass_cost(out, Y):
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out, labels=Y))
    return cost

def init():
    return tf.global_variables_initializer()

def train_op(learning_rate, cost):
    op_train = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
    return op_train


train_x, train_y, test_x, test_y = read_file()
X, Y, w, b = weights_biases_placeholder(train_x.shape[1], train_y.shape[1])
out = forward_pass(X, w, b)
cost = multiclass_cost(out, Y)
learning_rate, epochs = 0.01, 1000
op_train = train_op(learning_rate, cost)
init = init()
loss_trace = []
accuracy_trace = []
with tf.Session() as sess:
    sess.run(init)
    for i in range(epochs):
        sess.run(op_train, feed_dict={X: train_x, Y: train_y})
        loss_ = sess.run(cost, feed_dict={X: train_x, Y: train_y})
        accuracy_ = np.mean(np.argmax(sess.run(out, feed_dict={X: train_x, Y: train_y}), axis=1) == np.argmax(train_y, axis=1))
        loss_trace.append(loss_)
        accuracy_trace.append(accuracy_)
        if (((i+1)>100) and ((i+1)%100==0)):
            print ("Epoch: ",(i+1)," loss: ", loss_, " accuracy:", accuracy_)
    print ("Final training result----- ", "loss:", loss_, " accuracy: ", accuracy_)
    loss_test = sess.run(cost,feed_dict={X: test_x, Y: test_y})
    test_pred = np.argmax(sess.run(out, feed_dict={X: test_x, Y: test_y}), axis=1)
    accuracy_test = np.mean(test_pred == np.argmax(test_y, axis=1))
    print ("Result on test data-----", "loss:", loss_test, " accuracy:", accuracy_test)

推荐阅读更多精彩内容