我是靠谱客的博主 爱听歌鞋子,最近开发中收集的这篇文章主要介绍带你构建最基础的卷积神经网络(CNN),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras



'''查看版本号'''

print(tf.__version__)
print(sys.version_info)
for module in mpl,np,pd,sklearn,tf,keras:
    print(module.__name__,module.__version__)

'''load the data'''

fashion_mnist = keras.datasets.fashion_mnist
(x_train_all,y_train_all),(x_test,y_test)=fashion_mnist.load_data()
x_valid,x_train=x_train_all[:5000],x_train_all[5000:]
y_valid,y_train=y_train_all[:5000],y_train_all[5000:]

print(x_valid.shape,y_valid.shape)
print(x_train.shape,y_train.shape)
print(x_test.shape,y_test.shape)


'''数据处理'''
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
x_train_scaled=scaler.fit_transform(
    x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28,1)
x_valid_scaled=scaler.fit_transform(
    x_valid.astype(np.float32).reshape(-1,1)).reshape(-1,28,28,1)
x_test_scaled=scaler.fit_transform(
    x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28,1)


'''模型构建'''
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(filters=32,kernel_size=3,
                             padding='same',
                             activation='selu',
                             input_shape=(28,28,1)))
model.add(keras.layers.Conv2D(filters=32,kernel_size=3,
                             padding='same',
                             activation='selu'))
model.add(keras.layers.MaxPool2D(pool_size=2))
model.add(keras.layers.Conv2D(filters=64,kernel_size=3,
                             padding='same',
                             activation='selu'))
model.add(keras.layers.Conv2D(filters=64,kernel_size=3,
                             padding='same',
                             activation='selu'))
model.add(keras.layers.MaxPool2D(pool_size=2))
model.add(keras.layers.Conv2D(filters=128,kernel_size=3,
                             padding='same',
                             activation='selu'))
model.add(keras.layers.Conv2D(filters=128,kernel_size=3,
                             padding='same',
                             activation='selu'))
model.add(keras.layers.MaxPool2D(pool_size=2))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(128,activation='relu'))
model.add(keras.layers.Dense(10,activation="softmax"))



model.compile(loss="sparse_categorical_crossentropy",
             optimizer = "sgd",
             metrics = ["accuracy"])
logdir = './cnn-selu-callbacks'
if not os.path.exists(logdir):
    os.mkdir(logdir)
output_model_file = os.path.join(logdir,
                                "fashion mnist model.h5")

callbacks = [
    keras.callbacks.TensorBoard(logdir),
    keras.callbacks.ModelCheckpoint(output_model_file,
                                   save_best_only = True),
    keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3),

]

'''训练'''
history = model.fit(x_train_scaled,y_train,epochs=10,
                   validation_data=(x_valid_scaled,y_valid),
                   callbacks = callbacks)

'''画图看趋势'''
def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8,5))
    plt.grid(True)
    plt.gca().set_ylim(0,1)
    plt.show()

plot_learning_curves(history)
model.evaluate(x_test_scaled,y_test)

最后

以上就是爱听歌鞋子为你收集整理的带你构建最基础的卷积神经网络(CNN)的全部内容,希望文章能够帮你解决带你构建最基础的卷积神经网络(CNN)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部