Tensorflow学习笔记:使用Softmax进行多类别分类

 

#coding:utf-8
'''
使用Softmax进行多类别分类
'''
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

x1_label0 = np.random.normal(1, 1, (100, 1)) 
x2_label0 = np.random.normal(1, 1, (100, 1)) 
x1_label1 = np.random.normal(5, 1, (100, 1)) 
x2_label1 = np.random.normal(4, 1, (100, 1)) 
x1_label2 = np.random.normal(8, 1, (100, 1)) 
x2_label2 = np.random.normal(0, 1, (100, 1)) 

xs_label0 = np.hstack((x1_label0, x2_label0)) 
xs_label1 = np.hstack((x1_label1, x2_label1)) 
xs_label2 = np.hstack((x1_label2, x2_label2)) 
xs = np.vstack((xs_label0, xs_label1, xs_label2)) 
labels = np.matrix([[1., 0., 0.]] * len(x1_label0) + [[0., 1., 0.]] * len(x1_label1) + [[0., 
                    0., 1.]] * len(x1_label2)) 
arr = np.arange(xs.shape[0]) 
np.random.shuffle(arr) 
xs = xs[arr, :] 

labels = labels[arr, :] 
test_x1_label0 = np.random.normal(1, 1, (10, 1))  
test_x2_label0 = np.random.normal(1, 1, (10, 1))
test_x1_label1 = np.random.normal(5, 1, (10, 1)) 
test_x2_label1 = np.random.normal(4, 1, (10, 1)) 
test_x1_label2 = np.random.normal(8, 1, (10, 1)) 
test_x2_label2 = np.random.normal(0, 1, (10, 1))
test_xs_label0 = np.hstack((test_x1_label0, test_x2_label0))  
test_xs_label1 = np.hstack((test_x1_label1, test_x2_label1))
test_xs_label2 = np.hstack((test_x1_label2, test_x2_label2))
test_xs = np.vstack((test_xs_label0, test_xs_label1, test_xs_label2))
test_labels = np.matrix([[1., 0., 0.]] * 10 + [[0., 1., 0.]] * 10 + [[0., 0., 1.]] * 10)
train_size, num_features = xs.shape 

learning_rate = 0.01 
training_epochs = 1000 
num_labels = 3 
batch_size = 100 
X = tf.placeholder("float", shape=[None, num_features])
Y = tf.placeholder("float", shape=[None, num_labels]) 
W = tf.Variable(tf.zeros([num_features, num_labels]))  
b = tf.Variable(tf.zeros([num_labels])) 
y_model = tf.nn.softmax(tf.matmul(X, W) + b) 
cost = -tf.reduce_sum(Y * tf.log(y_model)) 
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 
correct_prediction = tf.equal(tf.argmax(y_model, 1), tf.argmax(Y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    for step in range(training_epochs * train_size // batch_size):
        offset = (step * batch_size) % train_size
        batch_xs = xs[offset:(offset + batch_size), :] 
        batch_labels = labels[offset:(offset + batch_size)] 
        err, _ = sess.run([cost, train_op], feed_dict={X:batch_xs, Y:batch_labels}) 
        print (step, err) 
    W_val = sess.run(W) 
    print('w', W_val) 
    b_val = sess.run(b) 
    print('b', b_val) 
    print("accuracy", accuracy.eval(feed_dict={X:test_xs, Y:test_labels}))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

视觉&物联智能

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值