在TF1.8之后Keras被当作为一个内置API:tf.keras.

并且之前的下载语句会报错。

 mnist = input_data.read_data_sets('MNIST_data',one_hot=True)

下面给出Keras和TensorFlow两种方式的训练代码(附验证代码):

Keras:

import numpy as np
import matplotlib.pyplot as plt import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(60000,784)
x_test = x_test.reshape(10000,784) # 归一化
x_train = x_train/255
x_test = x_test/255 y_train = keras.utils.to_categorical(y_train,10)
y_test = keras.utils.to_categorical(y_test,10) model = Sequential()
model.add(Dense(512,activation='relu',input_shape=(784,)))
model.add(Dense(256,activation="relu"))
model.add(Dense(10,activation="softmax")) # 显示网络结构
model.summary() model.compile(optimizer=SGD(),loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=64,epochs=5,validation_data=(x_test,y_test)) score = model.evaluate(x_test,y_test) # 输出 loss 和 accuracy
print("loss",score[0])
print("accu",score[1]) # 输入样本生成输出预测。
predictions = model.predict([x_test]) # 预测
print(np.argmax(predictions[23])) # 查看图片 检查是否预测正确
plt.imshow(x_test[23])
plt.show()

TensorFlow:

代码来自TensorFlow官网

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0 model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
]) model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']) model.fit(x_train, y_train, epochs=5) model.evaluate(x_test, y_test) #验证代码同上 略

另附Keras与tf.keras的区别(https://www.zhihu.com/question/313111229/answer/606660552)

【TensorFlow 3】mnist数据集:与Keras对比的更多相关文章

  1. 一个简单的TensorFlow可视化MNIST数据集识别程序

    下面是TensorFlow可视化MNIST数据集识别程序,可视化内容是,TensorFlow计算图,表(loss, 直方图, 标准差(stddev)) # -*- coding: utf-8 -*- ...

  2. 深度学习原理与框架-Tensorflow基本操作-mnist数据集的逻辑回归 1.tf.matmul(点乘操作) 2.tf.equal(对应位置是否相等) 3.tf.cast(将布尔类型转换为数值类型) 4.tf.argmax(返回最大值的索引) 5.tf.nn.softmax(计算softmax概率值) 6.tf.train.GradientDescentOptimizer(损失值梯度下降器)

    1. tf.matmul(X, w) # 进行点乘操作 参数说明:X,w都表示输入的数据, 2.tf.equal(x, y) # 比较两个数据对应位置的数是否相等,返回值为True,或者False 参 ...

  3. TensorFlow 训练MNIST数据集(2)—— 多层神经网络

    在我的上一篇随笔中,采用了单层神经网络来对MNIST进行训练,在测试集中只有约90%的正确率.这次换一种神经网络(多层神经网络)来进行训练和测试. 1.获取MNIST数据 MNIST数据集只要一行代码 ...

  4. TensorFlow训练MNIST数据集(1) —— softmax 单层神经网络

    1.MNIST数据集简介 首先通过下面两行代码获取到TensorFlow内置的MNIST数据集: from tensorflow.examples.tutorials.mnist import inp ...

  5. 基于 tensorflow 的 mnist 数据集预测

    1. tensorflow 基本使用方法 2. mnist 数据集简介与预处理 3. 聚类算法模型 4. 使用卷积神经网络进行特征生成 5. 训练网络模型生成结果 how to install ten ...

  6. TensorFlow 下 mnist 数据集的操作及可视化

    from tensorflow.examples.tutorials.mnist import input_data 首先需要连网下载数据集: mnsit = input_data.read_data ...

  7. 《Hands-On Machine Learning with Scikit-Learn&TensorFlow》mnist数据集错误及解决方案

    最近在看这本书看到Chapter 3.Classification,是关于mnist数据集的分类,里面有个代码是 from sklearn.datasets import fetch_mldata m ...

  8. Tensorflow基础-mnist数据集

    MNIST数据集,每张图片包含28*28个像素,把一个数组展开成向量,长度为28*28=784,故数据集中mnist.train.images是一个形状为[60000,784]的张量,第一个维度数字用 ...

  9. 基于TensorFlow的MNIST数据集的实验

    一.MNIST实验内容 MNIST的实验比较简单,可以直接通过下面的程序加上程序上的部分注释就能很好的理解了,后面在完善具体的相关的数学理论知识,先记录在这里: 代码如下所示: import tens ...

  10. TensorFlow训练MNIST数据集(3) —— 卷积神经网络

    前面两篇随笔实现的单层神经网络 和多层神经网络, 在MNIST测试集上的正确率分别约为90%和96%.在换用多层神经网络后,正确率已有很大的提升.这次将采用卷积神经网络继续进行测试. 1.模型基本结构 ...

随机推荐

  1. select下拉箭头样式重置

    select{ appearance:none; -moz-appearance:none; -webkit-appearance:none; background: url("../ima ...

  2. Sqoop 的基本使用

    目录 一.Sqoop 基本命令 1. 查看所有命令 2. 查看某条命令的具体使用方法 二.Sqoop 与 MySQL 1. 查询MySQL所有数据库 2. 查询指定数据库中所有数据表 三.Sqoop ...

  3. mysql产生死锁

    select * from information_schema.innodb_trx; kill 对应线程ID show PROCESSLIST;

  4. 性能监控: SPF4J介绍

    1. 总体介绍 性能测试是一项在软件生命开发周期中总是被置于最后一环的活动.我们经常依靠 Java profilers 去帮助发现性能问题. 在这篇文章中,我们将会学习关于 Java 的简单性能测试框 ...

  5. GitLab通过API创建项目

    示例: #!/usr/bin/python # -*- coding:utf-8 -*- import os import json import requests import subprocess ...

  6. shell写的俄罗斯方块

    共享一下. #!/bin/bash # Tetris Game # xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declaratio ...

  7. HDU 1724:Ellipse(自适应辛普森积分)

    题目链接 题意 给出一个椭圆,问一个[l, r] 区间(蓝色区域)的面积是多少. 思路 自适应辛普森积分 具体一些分析如上. 很方便,套上公式就可以用了. 注意 eps 的取值影响了跑的时间,因为决定 ...

  8. POJ 3686:The Windy's(最小费用最大流)***

    http://poj.org/problem?id=3686 题意:给出n个玩具和m个工厂,每个工厂加工每个玩具有一个时间,问要加工完这n个玩具最少需要等待的平均时间.例如加工1号玩具时间为t1,加工 ...

  9. Linux部署项目常用命令

    前言:一般项目都会使用阿里云等服务器作为云服务器.此时必不可免会使用到一系列常用的命令.这里我整合一下常用的命令 1.一般链接阿里云服务器常用的的是xshell跟xftp. 下载路径:https:// ...

  10. 深度总结eMMC发展史 ICMAX值得更好地期待

    随着大数据.云计算.物联网等产业的发展,信息存储安全一旦受到威胁,将危害到政军.石油.化工.核能.金融等所有行业的安全.存储芯片又被称为电子产品的“粮食”,占产品成本的二成左右,尽管中国是全球最大的手 ...