人工神经网络实现鸢尾花分类

本文介绍了使用神经网络对鸢尾花进行分类的方法,采用单层前馈神经网络,激活函数为softmax,损失函数为交叉熵。通过TensorFlow实现自动求导,训练模型并用训练集和测试集评估性能。文章详细探讨了数据集特征及分类过程。
摘要由CSDN通过智能技术生成

鸢尾花的类别包括:Setosa Iris(狗尾鸢尾),Versicolor Iris(杂色鸢尾),Virginica(弗吉尼亚鸢尾)。我们可以根据花萼长,花萼宽,花瓣长,花瓣宽这四个特征值对鸢尾花进行分类。

神经网络设计

神经网络结构:单层前馈型神经网络
激活函数:softmax函数
损失函数:交叉熵损失函数

Y=WX+B
共有四个特征值,结果分为三类


自动求导
with tf.GradientTape() as tape:
函数表达式
grad=tape.gradient(函数,自变量)


训练集: 训练模型
测试集: 评估模型性能


import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 

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)
#将前四列存入X中,结果存入Y中
x_train=iris_train[:,0:4
  • 0
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值