Python深度学习实战-基于Sequential方法搭建BP神经网络实现分类任务(附源码和实现效果)

实现功能

  1. 第一步:导入模块:import tensorflow as tf

  2. 第二步:制定输入网络的训练集和测试集

  3. 第三步:搭建网络结构:tf.keras.models.Sequential()

  4. 第四步:配置训练方法:model.compile():

  5. 第五步:执行训练过程:model.fit():

  6. 第六步:打印网络结构:model.summary()

  7. 第七步:执行验证过程:model.evaluate()

实现代码

python 复制代码
import tensorflow as tf
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target

# 数据预处理
scaler = StandardScaler()
X = scaler.fit_transform(X)

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 创建模型
model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(X.shape[1],)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(len(set(y)), activation='softmax')
])

# 编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 训练模型
model.fit(X_train, y_train, epochs=10, batch_size=32)
model.summary()
# 评估模型
test_loss, test_accuracy = model.evaluate(X_test, y_test)

实现效果

本人读研期间发表5篇SCI数据挖掘相关论文,现在某研究院从事数据挖掘相关科研工作,对数据挖掘有一定认知和理解,会结合自身科研实践经历不定期分享关于python、机器学习、深度学习基础知识与案例。

致力于 只做原创 ,以最简单的方式理解和学习,关注我一起交流成长。

邀请三个朋友关注本订阅号V:数据杂坛,即可在后台联系我 获取相关数据集和源码 ,送有关数据分析、数据挖掘、机器学习、深度学习相关的电子书籍。

相关推荐
xywww1681 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
月疯6 小时前
CNN卷积和反卷积输出的计算方法
深度学习·神经网络·cnn
夜雪一千6 小时前
Python enumerate() 函数完整详解:遍历同时获取索引,告别手动计数
服务器·windows·python
能有时光6 小时前
PyTorch KernelAgent 源码解读 ---(4)--- ExtractorAgent
人工智能·pytorch·python
直接冲冲冲6 小时前
鱼书-PH4-类的作用
深度学习
_Jimmy_7 小时前
Python 协程库如何使用以及有哪些使用场景
python
aqi007 小时前
15天学会AI应用开发(十七)使用LangGraph实现会话记忆功能
人工智能·python·大模型·ai编程·ai应用
第一程序员7 小时前
Rust Agent 子进程执行:Command 之前,先定义输入和超时
python·rust·github
skywalk81638 小时前
设计并实现段言的 C FFI 绑定机制 @Trae
c语言·开发语言·python·编程
weixin_BYSJ19878 小时前
SpringBoot + MySQL 乒乓球运动员信息管理系统项目实战--附源码04954
java·javascript·spring boot·python·django·flask·php