神经网络对鸢尾花分类

单层三神经元网络
无激活函数
损失函数为交叉熵
优化器为Adam

import numpy as np
from sklearn import datasets
import tensorflow as tf
from matplotlib import pyplot as plt

iris = datasets.load_iris()

data = iris.data
target = iris.target

# shuffling dataset
seed = 116  # using same seed in order to data match target
np.random.seed(seed)
np.random.shuffle(data)
np.random.seed(seed)
np.random.shuffle(target)
tf.random.set_seed(seed)

# partitioning dataset
train_data = data[:-30]
train_target = target[:-30]
test_data = data[-30:]
test_target = target[-30:]

# casting
train_data = tf.cast(train_data, tf.float32)
test_data = tf.cast(test_data, tf.float32)

# one hot
train_target = tf.one_hot(train_target, depth=3)

# packaging data
batch = 32
train_slices = tf.data.Dataset.from_tensor_slices((train_data, train_target)).batch(batch)
test_slices = tf.data.Dataset.from_tensor_slices((test_data, test_target)).batch(batch)

# defining trainable variable
stddev = np.sqrt(np.divide(1., 4.))
weight = tf.Variable(tf.random.truncated_normal((4, 3), stddev=stddev))
biases = tf.Variable(tf.random.truncated_normal((3, ), stddev=stddev))
variables = [weight, biases]

# iteration
epoch = 50
learning_rate = .1
recorded_loss = []
recorded_accuracy = []
counter = 0
alpha = 0.9
beta = 0.999
first_pro = []
second_pro = []
for variable in variables:
    first_pro.append(np.zeros(variable.shape))
    second_pro.append(np.zeros(variable.shape))
for epoch in range(epoch):
    # training
    total_loss = 0
    for data, target in train_slices:
        counter += 1
        with tf.GradientTape() as tape:
            prediction = tf.matmul(data, weight) + biases  # forward propagation in the network
            loss = tf.reduce_mean(tf.losses.categorical_crossentropy(target, tf.nn.softmax(prediction)))
            total_loss += float(loss)
        grads = tape.gradient(loss, variables)
        for index, variable in enumerate(variables):
            first_pro[index] = alpha * first_pro[index] + (1-alpha) * grads[index]
            second_pro[index] = beta * second_pro[index] + (1-beta) * tf.square(grads[index])
            first = first_pro[index] / (1-tf.pow(alpha, counter))
            second = second_pro[index] / (1-tf.pow(beta, counter))
            variable.assign_sub(learning_rate * first / tf.sqrt(second))
    recorded_loss.append(total_loss)

    # testing
    total_correct, total_number = 0, 0
    for data, target in test_slices:
        prediction = tf.matmul(data, weight) + biases
        prediction = tf.argmax(prediction, axis=1)  # get the index of result with the highest probability in each row
        prediction = tf.cast(prediction, dtype=target.dtype)
        correct = tf.cast(tf.equal(prediction, target), dtype=tf.int32)
        correct = tf.reduce_sum(correct)
        total_number += data.shape[0]
        total_correct += int(correct)
    accuracy = total_correct/total_number
    recorded_accuracy.append(accuracy)

    print('Epoch: {}, Loss: {}, Accuracy: {}'.format(epoch, total_loss, accuracy))

# output
plt.subplot(121)
plt.title('Loss Function Curve')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.plot(recorded_loss, label='$Loss$')
plt.legend()
plt.subplot(122)
plt.title('Accuracy Curve')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.plot(recorded_accuracy, label='$Accuracy$')
plt.legend()
plt.show()

### 使用神经网络进行鸢尾花数据集分类 #### 导入必要的库并加载数据集 为了使用神经网络鸢尾花数据集进行分类,首先需要导入所需的Python库,并加载鸢尾花数据集。该数据集中包含了150条关于三种不同种类的鸢尾花的数据,即山鸢尾(Iris-setosa)、变色鸢尾(Iris-versicolor)以及维吉尼亚鸢尾(Iris-virginica),每种各有50个样本[^3]。 ```python from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, OneHotEncoder import numpy as np import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense # 加载鸢尾花数据集 iris_dataset = load_iris() X = iris_dataset.data # 特征变量 y = iris_dataset.target.reshape(-1, 1) # 目标标签 ``` #### 数据预处理 在构建模型之前,先要对原始数据做一些预处理工作。这里采用标准化方法来缩放特征值到相近范围;另外由于目标变量是多分类问题,因此还需要对其进行独热编码(one-hot encoding),以便于后续训练过程中计算损失函数。 ```python # 对特征向量做标准差标准化 scaler = StandardScaler().fit(X) X_scaled = scaler.transform(X) # 对类别标签执行onehot编码转换 encoder = OneHotEncoder(sparse=False).fit(y) y_encoded = encoder.transform(y) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split( X_scaled, y_encoded, test_size=0.2, random_state=42) ``` #### 构建BP神经网络模型 接下来定义一个多层感知机(Multilayer Perceptron, MLP), 即前馈型人工神经网络(feedforward artificial neural network),它由多个全连接层组成。每一层中的节点都只与前后相邻两层相连,而不会形成环路结构。此案例中选用两个隐藏层作为基础架构来进行实验尝试。 ```python model = Sequential([ Dense(10, input_dim=X.shape[1], activation='relu'), # 输入层->第一个隐含层 Dense(8, activation='relu'), # 第一个隐含层->第二个隐含层 Dense(3, activation='softmax') # 输出层 (对应三个可能的结果类别) ]) # 编译模型 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) ``` #### 训练模型 完成上述准备工作之后就可以调用`fit()`接口开始正式训练过程了,在这个阶段会不断调整权重参数使得预测误差最小化直至收敛为止。 ```python history = model.fit(X_train, y_train, epochs=50, batch_size=10, validation_data=(X_test, y_test)) ``` #### 测试评估性能 最后一步则是通过验证集上的表现情况来衡量整个系统的泛化能力究竟如何。一般情况下我们会关注准确率(Accuracy)指标的变化趋势,当然也可以进一步考察其他类型的评价准则比如精确度(Precision)、召回率(Recall)等等。 ```python loss, accuracy = model.evaluate(X_test, y_test) print(f'Test Accuracy: {accuracy:.4f}') ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值