MNIST3_tf2.keras训练预测

该博客展示了如何利用tf2.keras构建一个基于MyModel类的神经网络模型,用于MNIST数据集的手写数字识别。通过定义全连接层、批量归一化层和损失函数,模型在训练集上达到约97.3%的准确率,并在验证集上达到95.57%的准确率。最终模型在验证集上的AUC为95.1%。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

针对MNIST数据集进行j基于tf2.keras的nn模型训练和预测
部分脚本如下: 完整脚本见笔者github

import pandas as pd 
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, Sequential, regularizers, Model
from utils.utils_tools import clock, get_ministdata
import warnings
warnings.filterwarnings(action='ignore')


class MyModel(Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.fc0 = layers.Dense(256, activation='relu')
        self.bn1 = layers.BatchNormalization()
        self.fc1 = layers.Dense(32, activation='relu', kernel_regularizer = regularizers.l2(0.06))
        self.bn2 = layers.BatchNormalization()
        self.fc2 = layers.Dense(10, activation='sigmoid')

    def call(self, inputs_, training=None):
        x = self.fc0(inputs_)
        x = self.bn1(x)
        x = self.fc1(x)
        x = self.bn2(x)
        return self.fc2(x)



def build_nnmodel(input_shape=(None, 784)):
    nnmodel = MyModel()
    nnmodel.build(input_shape=input_shape)
    nnmodel.compile(
        loss='categorical_crossentropy',
        optimizer = 'adam',
        metrics = ['accuracy']
    )
    return nnmodel

if __name__ == '__main__':
    mnistdf = get_ministdata()
    te_index = mnistdf.sample(frac=0.75).index.tolist()
    mnist_te = mnistdf.loc[te_index, :]
    mnist_tr = mnistdf.loc[~mnistdf.index.isin(te_index), :]
    mnist_tr_x, mnist_tr_y = mnist_tr.iloc[:, :-1].values, tf.keras.utils.to_categorical(mnist_tr.iloc[:, -1].values)
    mnist_te_x, mnist_te_y = mnist_te.iloc[:, :-1].values, tf.keras.utils.to_categorical(mnist_te.iloc[:, -1].values)

    nnmodel = build_nnmodel()
    stop = tf.keras.callbacks.EarlyStopping(monitor = 'accuracy', min_delta=0.0001)
    history = nnmodel.fit(mnist_tr_x, mnist_tr_y
                        ,validation_data = (mnist_te_x, mnist_te_y)
                        ,epochs=10
                        ,callbacks = [stop])
    acc_final = round(max(history.history['accuracy']), 2)

    print(f"acc:{acc_final:.3f}")
    predict_ = np.argmax(nnmodel.predict(mnist_te_x), axis=1)
    te_y = np.argmax(mnist_te_y, axis=1)
    print(predict_)
    print(te_y)
    print(f'auc: {sum(predict_ == te_y)/te_y.shape[0]:.3f}')
"""
14000/14000 [==============================] - 10s 741us/sample - loss: 0.1472 - accuracy: 0.9730 - val_loss: 0.2162 - val_accuracy: 0.9557
Epoch 9/10
13952/14000 [============================>.] - ETA: 0s - loss: 0.1520 - accuracy: 0.9720
Epoch 00009: accuracy did not improve from 0.97300
14000/14000 [==============================] - 23s 2ms/sample - loss: 0.1522 - accuracy: 0.9719 - val_loss: 0.2390 - val_accuracy: 0.9506
acc:0.970
[2 2 8 ... 7 6 6]
[2 2 8 ... 7 6 6]
auc: 0.951, cost: 170.946

"""


``` from tensorflow.keras.datasets import mnist, usps from tensorflow.keras.models import Model from tensorflow.keras.layers import Dense, Input # 加载MNIST和USPS数据集 (mnist_train_images, mnist_train_labels), (mnist_test_images, mnist_test_labels) = mnist.load_data() (usps_train_images, usps_train_labels), (usps_test_images, usps_test_labels) = usps.load_data() # 数据预处理 mnist_train_images = mnist_train_images.reshape(-1, 28*28).astype('float32') / 255 mnist_test_images = mnist_test_images.reshape(-1, 28*28).astype('float32') / 255 usps_train_images = usps_train_images.reshape(-1, 28*28).astype('float32') / 255 usps_test_images = usps_test_images.reshape(-1, 28*28).astype('float32') / 255 mnist_train_labels = tf.keras.utils.to_categorical(mnist_train_labels, 10) mnist_test_labels = tf.keras.utils.to_categorical(mnist_test_labels, 10) usps_train_labels = tf.keras.utils.to_categorical(usps_train_labels, 10) usps_test_labels = tf.keras.utils.to_categorical(usps_test_labels, 10) # 定义源领域模型 input_tensor = Input(shape=(28*28,)) x = Dense(256, activation='relu')(input_tensor) x = Dense(256, activation='relu')(x) output_tensor = Dense(10, activation='softmax')(x) source_model = Model(inputs=input_tensor, outputs=output_tensor) source_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # 在MNIST数据集上训练源领域模型 source_model.fit(mnist_train_images, mnist_train_labels, epochs=10, batch_size=128, validation_data=(mnist_test_images, mnist_test_labels)) # 定义领域适应模型 feature_extractor = Model(inputs=source_model.input, outputs=source_model.layers[-2].output) target_input = Input(shape=(28*28,)) target_features = feature_extractor(target_input) target_output = Dense(10, activation='softmax')(target_features) domain_adapt_model = Model(inputs=target_input, outputs=target_output) domain_adapt_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # 在USPS数据集上微调领域适应模型 domain_adapt_model.fit(usps_train_images, usps_train_labels, epochs=10, batch_size=128, validation_data=(usps_test_images, usps_test_labels)) # 评估领域适应模型 test_loss, test_acc = domain_adapt_model.evaluate(usps_test_images, usps_test_labels) print(f'领域适应模型在USPS测试集上的准确率: {test_acc}')```解释代码
最新发布
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Scc_hy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值