输入相关的库

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
print("TensorFlow version:", tf.__version__)
TensorFlow version: 2.6.4

导入数据

fashion_mnist = tf.keras.datasets.fashion_mnist(x_train, y_train),(x_test, y_test) = fashion_mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train[..., tf.newaxis].astype("float32")
x_test = x_test[..., tf.newaxis].astype("float32")train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(128)test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(128)
2022-10-17 02:24:49.792549: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:24:49.895220: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:24:49.896028: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:24:49.902715: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-10-17 02:24:49.903033: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:24:49.903894: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:24:49.904866: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:24:52.155655: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:24:52.156422: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:24:52.157115: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:24:52.157726: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 15401 MB memory:  -> device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0
inputs = keras.Input(shape=(28,28,1))
x = inputs
for size in [32,64,128]:x = layers.Conv2D(size, 3, strides=1, padding="same")(x)x = layers.BatchNormalization()(x)x = layers.Activation("relu")(x)x = layers.MaxPooling2D((2, 2))(x)x = layers.Conv2D(256, 3, strides=1, padding="same")(x)
x = layers.BatchNormalization()(x)
x = layers.Activation("relu")(x)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(10, activation='softmax')(x)
model = keras.Model(inputs, outputs)
model.summary()
2022-10-17 02:39:04.385252: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:39:04.503544: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:39:04.504713: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:39:04.506969: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-10-17 02:39:04.507300: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:39:04.508303: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:39:04.509206: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:39:07.153982: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:39:07.155140: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:39:07.156582: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-17 02:39:07.157460: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 15401 MB memory:  -> device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         [(None, 28, 28, 1)]       0
_________________________________________________________________
conv2d (Conv2D)              (None, 28, 28, 32)        320
_________________________________________________________________
batch_normalization (BatchNo (None, 28, 28, 32)        128
_________________________________________________________________
activation (Activation)      (None, 28, 28, 32)        0
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 32)        0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 14, 14, 64)        18496
_________________________________________________________________
batch_normalization_1 (Batch (None, 14, 14, 64)        256
_________________________________________________________________
activation_1 (Activation)    (None, 14, 14, 64)        0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 64)          0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 7, 7, 128)         73856
_________________________________________________________________
batch_normalization_2 (Batch (None, 7, 7, 128)         512
_________________________________________________________________
activation_2 (Activation)    (None, 7, 7, 128)         0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 3, 3, 128)         0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 3, 3, 256)         295168
_________________________________________________________________
batch_normalization_3 (Batch (None, 3, 3, 256)         1024
_________________________________________________________________
activation_3 (Activation)    (None, 3, 3, 256)         0
_________________________________________________________________
global_average_pooling2d (Gl (None, 256)               0
_________________________________________________________________
dropout (Dropout)            (None, 256)               0
_________________________________________________________________
dense (Dense)                (None, 10)                2570
=================================================================
Total params: 392,330
Trainable params: 391,370
Non-trainable params: 960
_________________________________________________________________
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])model.fit(train_ds, epochs=10)
Epoch 1/10
2022-10-17 02:32:49.166692: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
2022-10-17 02:32:50.227935: I tensorflow/stream_executor/cuda/cuda_dnn.cc:369] Loaded cuDNN version 8005
469/469 [==============================] - 10s 6ms/step - loss: 0.4292 - accuracy: 0.8470
Epoch 2/10
469/469 [==============================] - 3s 6ms/step - loss: 0.2820 - accuracy: 0.8995
Epoch 3/10
469/469 [==============================] - 3s 6ms/step - loss: 0.2367 - accuracy: 0.9129
Epoch 4/10
469/469 [==============================] - 3s 6ms/step - loss: 0.2112 - accuracy: 0.9231
Epoch 5/10
469/469 [==============================] - 3s 7ms/step - loss: 0.1950 - accuracy: 0.9294
Epoch 6/10
469/469 [==============================] - 3s 6ms/step - loss: 0.1708 - accuracy: 0.9377
Epoch 7/10
469/469 [==============================] - 3s 6ms/step - loss: 0.1561 - accuracy: 0.9434
Epoch 8/10
469/469 [==============================] - 3s 6ms/step - loss: 0.1398 - accuracy: 0.9481
Epoch 9/10
469/469 [==============================] - 3s 6ms/step - loss: 0.1300 - accuracy: 0.9518
Epoch 10/10
469/469 [==============================] - 3s 6ms/step - loss: 0.1137 - accuracy: 0.9586
model.evaluate(test_ds)
79/79 [==============================] - 0s 3ms/step - loss: 0.2961 - accuracy: 0.9086

keras构建卷积神经网络相关推荐

  1. keras构建卷积神经网络(CNN(Convolutional Neural Networks))进行图像分类模型构建和学习

    keras构建卷积神经网络(CNN(Convolutional Neural Networks))进行图像分类模型构建和学习 全连接神经网络(Fully connected neural networ ...

  2. keras构建卷积神经网络_在Keras中构建,加载和保存卷积神经网络

    keras构建卷积神经网络 This article is aimed at people who want to learn or review how to build a basic Convo ...

  3. keras构建卷积神经网络_通过此简单教程学习在网络上构建卷积神经网络

    keras构建卷积神经网络 by John David Chibuk 约翰·大卫·奇布克(John David Chibuk) 通过此简单教程学习在网络上构建卷积神经网络 (Learn to buil ...

  4. keras构建卷积神经网络_在python中使用tensorflow s keras api构建卷积神经网络的初学者指南...

    keras构建卷积神经网络 初学者的深度学习 (DEEP LEARNING FOR BEGINNERS) Welcome to Part 2 of the Neural Network series! ...

  5. cnn神经网络可以用于数据拟合吗_使用Keras搭建卷积神经网络进行手写识别的入门(包含代码解读)...

    本文是发在Medium上的一篇博客:<Handwritten Equation Solver using Convolutional Neural Network>.本文是原文的翻译.这篇 ...

  6. 通过 Tensorflow 的基础类,构建卷积神经网络,用于花朵图片的分类

    实验目的 通过 Tensorflow 的基础类,构建卷积神经网络,用于花朵图片的分类. 实验环境 import tensorflow as tfprint(tf.__version__) output ...

  7. PyTorch基础与简单应用:构建卷积神经网络实现MNIST手写数字分类

    文章目录 (一) 问题描述 (二) 设计简要描述 (三) 程序清单 (四) 结果分析 (五) 调试报告 (六) 实验小结 (七) 参考资料 (一) 问题描述 构建卷积神经网络实现MNIST手写数字分类 ...

  8. keras构建前馈神经网络(feedforward neural network)进行分类模型构建基于早停法(Early stopping)

    keras构建前馈神经网络(feedforward neural network)进行分类模型构建基于早停法(Early stopping) 当我们训练深度学习神经网络的时候通常希望能获得最好的泛化性 ...

  9. keras构建前馈神经网络(feedforward neural network)进行多分类模型训练学习

    keras构建前馈神经网络(feedforward neural network)进行多分类模型训练学习 前馈神经网络(feedforward neural network)是一种最简单的神经网络,各 ...

最新文章

  1. C++知识点17——使用C++标准库(顺序容器vector常用操作)
  2. python编程语言继承_python应用:学习笔记(Python继承)
  3. 在Android App中集成Google登录
  4. thinkcmf 横向排列数据_利用python进行数据分析之数据清洗规整
  5. SQL取最大值编码(自动编码)
  6. LeetCode 1799. N 次操作后的最大分数和(回溯 / 状态压缩DP)
  7. 动态规划算法实验报告_强化学习之动态规划算法
  8. 【面向对象】面向对象程序设计测试题4-Java中的类和对象测试
  9. Sublime Text快捷键去除空白行
  10. C# switch语句中,可以放哪些类型
  11. curl只能抓取页面的部分内容的原因
  12. shell开启飞行模式_手机飞行模式,太神奇了,今天总算明白了,不用真是太浪费了...
  13. 软件测试之图覆盖(作业3)
  14. 前端开发工程化探讨--基础篇
  15. bzoj1088: [SCOI2005]扫雷Mine
  16. FR跨SHEET条件汇总
  17. gitlab使用教程
  18. 数独认识、规则及玩法技巧(python实现自动数独算法之二)
  19. ai-tb.cn 这里面的数据就该都是淘宝的,现在去哪抓取?
  20. Java——(九)IO流

热门文章

  1. 外派公司或者外包公司,真的适合选择吗?
  2. IaaS PaaS SaaS 读音及含义
  3. minHash最小哈希
  4. request.getParameter()等类似概念理解
  5. 计算机三级网络几时出成绩,全国计算机等级考试成绩查询时间是什么时候?
  6. Java学习集合源码分析
  7. android 静态工厂方法,Android 源码中的静态工厂方法
  8. 【大数据入门核心技术-Tez】(四)Tez与Hdfs整合
  9. 阐述清楚浮动的几种方法
  10. 【分享】光模块PPT