//总体过程
1. 预处理data,分别伟traindata和testdata
2.建立模型,编译模型
3.训练模型 fit
4.评估模型 evaluate
5.验证模型 predict + plot
(60000, 28, 28)
6000 张图,28*28 进行训练。
标签是一个数字
train_labels
array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
from __future__ import absolute_import, division, print_function, unicode_literals
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__)
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images.shape
len(train_labels)
train_labels
test_images.shape
#(10000, 28, 28)
len(test_labels)
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
train_images = train_images / 255.0
test_images = test_images / 255.0
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nTest accuracy:', test_acc)
probability_model = tf.keras.Sequential([model,
tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
predictions[0]
np.argmax(predictions[0])
test_labels[0]
def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array, true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)
def plot_value_array(i, predictions_array, true_label):
predictions_array, true_label = predictions_array, true_label[i]
plt.grid(False)
plt.xticks(range(10))
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i], test_labels)
plt.show()
i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i], test_labels)
plt.show()
# Plot the first X test images, their predicted labels, and the true labels.
# Color correct predictions in blue and incorrect predictions in red.
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()
# Grab an image from the test dataset.
img = test_images[1]
print(img.shape)
# Add the image to a batch where it's the only member.
img = (np.expand_dims(img,0))
print(img.shape)
predictions_single = probability_model.predict(img)
print(predictions_single)
plot_value_array(1, predictions_single[0], test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
np.argmax(predictions_single[0])
分享到:
相关推荐
在tensorflow中的mnist手写识别测试中,需要调用谷歌的网站,这里已经下载好的zip文件,把这个zip放到你的代码同目录下,可以直接运行,如果不行,可以参考我的相关博客,希望各位人工智能学习者将来年薪百万。
使用TensorFlow库完成mnist数字识别,并保存模型,并用该模型识别图片,由TensorFlow谷歌实战源代码改写
tensorflow mnist 训练数据包 tensorflow mnist 训练数据包
包括数据集、网络结构、训练代码、测试代码,还有示范的最终模型
用python编写的tensorflow版代码来跑mnist数据集,并且还能生成tfrecord文件来调用,内有注释,全部自己敲的,准确率在99%
基于python3.7版本的tensorflow2.0实现mnist手写数字识别代码
Intel Movidius Neural Compute Stick 跑tensorflow训练的mnist模型的例子,博客链接: https://blog.csdn.net/rookie_wei/article/details/98873920
由于pip安装的TensorFlow缺少一部分代码,以及TensorFlow2版本相对于TensorFlow1在语句上有变化。因此大部分网上代码不适用(主要问题在于读取mnist数据包和一些函数形式有问题)。这个代码可以运行。内含离线mnist...
Python2.7_TensorFlow1.3.0_MNIST数字数据集识别与TensorFlow模型持久化的小DEMO,具体的对MNIST、模型持久化等的分析见博客https://blog.csdn.net/primetong/article/details/79940684
tensorflow基础教程中所用mnist数据集,在线下载会出问题,最好采用以下步骤: # (1) 首先在当前项目下新建 MNIST_data # (2) http://yann.lecun.com/exdb/mnist/ 从该网址将4个附件全部下载 # (3) 载的四个文件...
tensorflow教程里用到的mnist数据集
tensorflow入门教程之mnist识别手写数字 测试类+训练模型类+训练好的模型+自己手写识别的图片
import tensorflow.examples.tutorials.mnist.input_data as input_data mnist = input_data.read_data_sets(data/,one_hot=True) #next_batch()实现方法会对内部数据先进行洗牌操作! batch_image_xs, batch_lables...
简单的Python程序示例,运用TensorFlow框架实行识别MNIST上的卷积神经网络
基于selective_search源码对手写数字串进行过滤分割,并基于tensorflow在mnist训练好的模型进行识别。 环境:Windows10 + tensorflow1.2 + python3.5 + cv2 程序: example/demo.py---对手写数字图片的分割,并将每...
包括mnist数据集文件,输入数据的python代码,深度分类mnist代码,softmax函数代码以及整合的内含详细代码解释的mnist_with_summaries代码
tensorflow-mnist-VAE, MNIST变分自动编码器的Tensorflow实现 用于MNIST的变分本文介绍了一种用于 MNIST descripbed的变分自动编码器( VAE )的实现方法:基于等的自动编码变分贝叶斯。结果复制训练良好的VAE必须能够...
tensorflow2实现Mnist余弦退火+热重启+ 每个step的学习率输出+可视化.py
tensorflow实现AlexNet训练mnist数据python代码
https://codelabs.developers.google.com/codelabs/cloud-tensorflow-mnist/#0 代码