只是一个练习而已
这个练习是基于鸢尾花数据集——IRIS的单层神经网络。
主要使用梯度下降法
来得到训练这个数据集的损失值和精确度
获取数据集 – IRIS
从官网下载数据,并使用pandas库读取其csv文件
# 获取数据 训练集
TRAIN_URL = "http://download.tensorflow.org/data/iris_training.csv"
train_path = tf.keras.utils.get_file(TRAIN_URL.split('/')[-1], TRAIN_URL)
# 获取数据 测试集
TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"
test_path = tf.keras.utils.get_file(TEST_URL.split('/')[-1], TEST_URL)
# 读取数据
df_iris_train = pd.read_csv(train_path, header=0)
df_iris_test = pd.read_csv(test_path, header=0)
Whole Code:
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# import os
# os.environ['CUDA_VISIBLE_DEVICES'] = "0"
# gpus = tf.config.experimental.list_logical_devices('GPU')
# tf.config.experimental.set_memory_growth(gpus[0], True)
# 数据可视化
def show():
plt.figure(figsize=(10, 3))
plt.subplot(121)
plt.plot(loss_train, color = "blue", label = "Train")
plt.plot(loss_test, color="pink", label = "Test")
plt.xlabel("Iteration")
plt.ylabel("Loss")
plt.legend()
plt.subplot(122)
plt.plot(acc_train, color="blue", label="Train")
plt.plot(acc_test, color="pink", label="Test")
plt.xlabel("Iteration")
plt.ylabel("Accuracy")
plt.legend()
plt.show()
# 获取数据 训练集
TRAIN_URL = "http://download.tensorflow.org/data/iris_training.csv"
train_path = tf.keras.utils.get_file(TRAIN_URL.split('/')[-1], TRAIN_URL)
# 获取数据 测试集
TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"
test_path = tf.keras.utils.get_file(TEST_URL.split('/')[-1], TEST_URL)
# 读取数据
df_iris_train = pd.read_csv(train_path, header=0)
df_iris_test = pd.read_csv(test_path, header=0)
iris_train = np.array(df_iris_train)
iris_test = np.array(df_iris_test)
# 数据划分, 0 ~ 3 的四个样本为鸢尾花的属性
# 第 4 个样本为鸢尾花的种类,三种分别为 0 1 2
x_train = iris_train[:, 0:4]
y_train = iris_train[:, 4]
x_test = iris_test[:, 0:4]
y_test = iris_test[:, 4]
# 将数据归一化
x_train = x_train - np.mean(x_train, axis=0)
x_test = x_test - np.mean(x_test, axis=0)
# 转换数据类型,并将花的种类用独热编码表示
X_train = tf.cast(x_train, tf.float32)
Y_train = tf.one_hot(tf.constant(y_train, dtype=tf.int32), 3)
# print(Y_train.shape, X_train.shape)
X_test = tf.cast(x_test, tf.float32)
Y_test = tf.one_hot(tf.constant(y_test,dtype=tf.int32), 3)
# Set hyperparameters
learn_rate = 0.5
iters = 50
# 设置随机种子,并且将数据W设为随机, B为0
np.random.seed(612)
W = tf.Variable(np.random.randn(4, 3), dtype=tf.float32)
B = tf.Variable(np.zeros([3]), dtype=tf.float32)
acc_train = []
loss_train = []
acc_test = []
loss_test = []
# 进行单层神经网络
for i in range(0, iters + 1):
with tf.GradientTape() as tape:
PRED_train = tf.nn.softmax(tf.matmul(X_train, W) + B)
Loss_train = tf.reduce_mean(tf.keras.losses.categorical_crossentropy(y_true=Y_train, y_pred=PRED_train))
PRED_test = tf.nn.softmax(tf.matmul(X_test, W) + B)
Loss_test = tf.reduce_mean(tf.keras.losses.categorical_crossentropy(y_true=Y_test, y_pred=PRED_test))
accuracy_train = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(PRED_train.numpy(), axis=1), y_train), tf.float32))
accuracy_test = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(PRED_test.numpy(), axis=1), y_test), tf.float32))
acc_train.append(accuracy_train)
acc_test.append(accuracy_test)
loss_train.append(Loss_train)
loss_test.append(Loss_test)
grads = tape.gradient(Loss_train, [W, B])
W.assign_sub(learn_rate * grads[0])
B.assign_sub(learn_rate * grads[1])
print("i: %i, TrainAcc: %f, Trainloss: %f, TestAcc: %f, Testloss: %f" %(i, accuracy_train, Loss_train, accuracy_test, Loss_test))
# 数据可视化
show()
运行结果
输出的分别为迭代次数、训练准确率、训练交叉熵损失 、测试准确率、测试交叉熵损失,
下面的俩个图分别是训练集和测试集的损失值和精确度的对比,不难发现,单层神经网络随着迭代次数的增加,准确率可以到达%97