测试数据准确率:98%
采用一个隐藏层,30个节点(iris数据量并不大)
plot_train_BP_Netsiris.png
# encoding: utf-8
"""
@version: ??
@author: kaenlee @contact: lichaolfm@163.com
@software: PyCharm Community Edition
@time: 2018/1/10 16:22
purpose: BP神经网络
"""
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from random import shuffle
class BP_Nets:
def __init__(self, layers, optimizer=tf.train.AdamOptimizer()):
assert len(layers) >= 3
self.layers = layers
self.X = tf.placeholder(dtype=tf.float32, shape=[None, layers[0]])
self.Y_ = tf.placeholder(dtype=tf.float32, shape=[None, layers[-1]])
count = 1
tmp = self.X
# 定义隐藏层
for n_input, n_output in zip(layers[:-2], layers[1:-1]):
tmp = self.__add_layer(tmp, n_input, n_output, name='Layer%d' % count)
# 定义输出层
self.Y = self.__add_layer(tmp, self.layers[-2], self.layers[-1], name='output_layer',
acativefunction=tf.nn.softmax)
# 交叉损失熵函数
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, n_input, n_output):
# 初始化权重
total = n_input + n_output
boundary_uniform = tf.sqrt(6.0 / total)
weight = tf.random_uniform(shape=[n_input, n_output], minval=-boundary_uniform,
maxval=boundary_uniform, dtype=tf.float32, name='weight')
return weight
def __add_layer(self, input, n_input, n_output, name, acativefunction=tf.nn.sigmoid):
weight = self.__init_weight(n_input, n_output)
weight = tf.Variable(initial_value=weight, name='W_' + name)
bias = tf.Variable(tf.zeros(shape=(n_output), dtype=tf.float32), name='b_' + name)
# print(input.shape, n_input, n_output)
output = acativefunction(
tf.add(tf.matmul(a=input, b=weight), bias), name='result' + name
)
return output
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_BP_Nets%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)
layers = [len(X[0]), 30, len(Y[0])]
model = BP_Nets(layers)
model.train(X_train=X, Y_train=Y, n_iter=300, size_batch=10, name='iris')
print(model.evaluate(X_test=X, Y_test=Y))
网友评论