MLP实现fashion_mnist数据集分类(2)-函数式API构建模型(tensorflow)

使用函数式API构建模型,使得模型可以处理多输入多输出。

1、查看tensorflow版本

import tensorflow as tf

print('Tensorflow Version:{}'.format(tf.__version__))
print(tf.config.list_physical_devices())

在这里插入图片描述

2、fashion_mnist数据集分类模型

2.1 使用Sequential构建模型

from keras import Sequential
from keras.layers import Flatten,Dense,Dropout
from keras import Input

model = Sequential()
model.add(Input(shape=(28,28)))
model.add(Flatten())
model.add(Dense(units=256,kernel_initializer='normal',activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(units=64,kernel_initializer='normal',activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(units=10,kernel_initializer='normal',activation='softmax'))
model.summary()

在这里插入图片描述

2.2 使用函数式API构建模型

from keras.layers import Flatten,Dense,Dropout
from keras import Input,Model

input = Input(shape=(28,28))
x = Flatten()(input)
x = Dense(units=256,kernel_initializer='normal',activation='relu')(x)
x = Dropout(rate=0.1)(x)
x = Dense(units=64,kernel_initializer='normal',activation='relu')(x)
x = Dropout(rate=0.1)(x)
output = Dense(units=10,kernel_initializer='normal',activation='softmax')(x)
model = Model(inputs=input, outputs=output)
model.summary()

在这里插入图片描述
可以看到两个模型的结构是一样的,编译和训练也是一样的。

3、使用函数式API搭建多输入多输出模型

两个输入一个输出,对比两个图片是否一样。

from keras.layers import Flatten,Dense,Dropout
from keras import Input,Model
import keras

input1 = Input(shape=(28,28))
input2 = Input(shape=(28,28))
x1 = Flatten()(input1)
x2 = Flatten()(input2)
x = keras.layers.concatenate([x1,x2])
x = Dense(units=256,kernel_initializer='normal',activation='relu')(x)
x = Dropout(rate=0.1)(x)
x = Dense(units=64,kernel_initializer='normal',activation='relu')(x)
x = Dropout(rate=0.1)(x)
output = Dense(units=1,kernel_initializer='normal',activation='sigmoid')(x)
model = Model(inputs=[input1,input2], outputs=output) # 两个输入,一个输出
model.summary()

在这里插入图片描述

  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
MLP-MNIST是指使用多层感知机(Multilayer Perceptron,简称MLP模型对MNIST数据集进行分类的任务。MNIST数据集是一个常用于机器学习领域的手写数字识别数据集。它包含了60,000个训练样本和10,000个测试样本,每个样本都是一个28x28的灰度图像,图像上的数字标签表示该图像对应的数字。 MLP是一种经典的前馈神经网络模型,它由多个全连接层组成,每个层都包含了多个神经元。该模型可以通过学习来建立输入图像与对应数字之间的映射关系,从而实现对手写数字的分类任务。 要进行MLP-MNIST数据集的分类任务,可以按照以下步骤进行: 1. 读取数据集:首先,需要将MNIST数据集加载到程序中,可以使用适当的数据读取函数,如TensorFlow中的tf.keras.datasets模块中的load_data()函数。 2. 数据预处理:对于MLP模型,通常需要将图像数据进行平铺(flatten)操作,将二维的图像数据转换为一维的向量作为模型的输入。同时,还需要对图像数据进行归一化处理,将像素值缩放到0到1之间。 3. 初始化模型参数:根据需要选择合适的MLP模型结构,并对模型的参数进行初始化,如权重和偏置。 4. 定义激活函数MLP模型中的每个神经元通常都会使用激活函数对其输出进行非线性变换,常见的激活函数包括ReLU、sigmoid和tanh等。 5. 防止过拟合:在MLP模型中,为了防止过拟合现象的发生,可以采用一些正则化技术,如权重衰减(weight decay)。 6. 训练模型:使用训练集对MLP模型进行训练,通过反向传播算法不断优化模型参数,使其能够更好地拟合训练数据。 7. 模型评估:使用测试集对训练好的模型进行评估,计算分类准确率等指标,以评估模型的性能。 综上所述,MLP-MNIST数据集是指使用多层感知机模型对MNIST数据集进行分类任务的过程。通过适当的数据预处理、模型参数初始化、激活函数定义和防止过拟合等步骤,可以构建出一个能够对手写数字进行准确分类MLP模型

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值