tensorflow:iris-softmax
2018-01-11 本文已影响45人
Kean_L_C
测试准确率:95%
5
# encoding: utf-8
"""
@version: ??
@author: kaenlee @contact: lichaolfm@163.com
@software: PyCharm Community Edition
@time: 2018/1/9 17:15
purpose:softmax 实现多分类
"""
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from random import shuffle
plt.style.use('ggplot')
class Softmax:
def __init__(self, n_input, n_output, optimizer=tf.train.AdamOptimizer()):
self.n_input = n_input # 输入信号维度
self.n_output = n_output # 输出信号维度
init_bias = tf.zeros(shape=(self.n_output), name='bias', dtype=tf.float32)
self.bias = tf.Variable(initial_value=init_bias) # 初始偏置
init_weight = self.init_weight()
self.weight = tf.Variable(init_weight) # 初始化权重
self.X = tf.placeholder(shape=[None, self.n_input], dtype=tf.float32, name='X') # 输入信号:x
self.Y_ = tf.placeholder(shape=[None, self.n_output], dtype=tf.float32, name='Y') # 输入信号:y
# sorfmax 函数
self.Y = tf.nn.softmax(
tf.add(tf.matmul(a=self.X, b=self.weight), self.bias)
)
# 交叉损失熵函数
self.cross_entropy = tf.reduce_mean(- tf.reduce_sum(input_tensor=self.Y_ * tf.log(self.Y), axis=1),
name='cross_entropy')
# 优化器
self.optimizer = optimizer.minimize(self.cross_entropy)
# 初始化变量并启动session
init = tf.global_variables_initializer()
self.sess = tf.InteractiveSession()
self.sess.run(init)
# self.sess.close()
def init_weight(self):
# 初始化权重
total = self.n_input + self.n_output
boundary_uniform = tf.sqrt(6.0 / total)
weight = tf.random_uniform(shape=[self.n_input, self.n_output], minval=-boundary_uniform,
maxval=boundary_uniform, dtype=tf.float32, name='weight')
# sess = tf.Session()
# print(sess.run(weight))
return weight
def train(self, X_train, Y_train, n_iter, size_batch, name):
# 迭代训练
assert isinstance(X_train, np.ndarray)
assert isinstance(Y_train, np.ndarray)
size_sample = len(X_train)
partition = int(size_sample / size_batch)
cost_iter = []
accuracy = []
for i in range(n_iter):
# 打乱数据的顺序
indx = np.arange(size_sample)
shuffle(indx)
X_train = X_train[indx]
Y_train = Y_train[indx]
cost_batch = 0
for j in range(partition):
# 运行一个batch 更新权重
X_batch = X_train[(j * size_batch):((j + 1) * size_batch)]
Y_batch = Y_train[(j * size_batch):((j + 1) * size_batch)]
# print(X_batch)
opt, cost = self.sess.run(fetches=(self.optimizer, self.cross_entropy),
feed_dict={self.X: X_batch, self.Y_: Y_batch})
cost_batch += cost / partition
cost_iter.append(cost_batch)
Y = self.sess.run(fetches=self.Y, feed_dict={self.X: X_train})
Y = np.array(Y)
accuracy.append(self.accuracy(Y_train, Y))
plt.figure()
plt.plot(range(n_iter), cost_iter, color='green', label='cost')
plt.plot(range(n_iter), accuracy, color='gray', label='accuracy')
plt.title("model train result")
plt.legend()
plt.savefig('plot_train_softmax_%s.png' % name)
def accuracy(self, Y_, Y):
assert isinstance(Y_, np.ndarray)
assert isinstance(Y, np.ndarray)
boolean = tf.equal(x=tf.argmax(Y_, axis=1), y=tf.argmax(Y, axis=1))
accu = tf.reduce_mean(tf.cast(x=boolean, dtype=tf.float32))
return self.sess.run(accu)
def evaluate(self, X_test, Y_test):
assert isinstance(X_test, np.ndarray)
assert isinstance(Y_test, np.ndarray)
Y = self.sess.run(fetches=self.Y, feed_dict={self.X: X_test})
Y = np.array(Y)
return self.accuracy(Y_test, Y)
if __name__ == '__main__':
# iris
X = []
Y = []
with open("D:/Data/iris.txt", mode='r', encoding='utf-8') as f:
f.readline()
while 1:
row = f.readline().strip().split(',')
if len(row) <= 2:
break
X.append([float(i) for i in row[:-1]])
Y.append(row[-1])
X = np.array(X)
# one-hot
kind = np.unique(Y)
size = len(Y)
tmp = np.zeros(shape=(size, len(kind)))
for indx in range(size):
index_one, = np.where(kind == Y[indx])
tmp[indx][index_one] = 1
Y = np.array(tmp);
del tmp
# print(Y)
# print(X)
model = Softmax(n_input=len(X[0]), n_output=len(Y[0]))
model.train(X_train=X, Y_train=Y, n_iter=200, size_batch=10, name='iris')
print(model.evaluate(X_test=X, Y_test=Y))