N年没在优快云上发布文章了,今天抽空发现10年前,转发过一篇量子隐形传输都文章,还有朋友留言说是谎言,2019年,量子隐形传输有了重大的进展,没想10年前尽然接触过量子传输,看样子还是要留下自己学习的足迹
从去年开始研究人工智能,下面基于 tensorflow 提供的DEMO,修改了一个可以查看 训练数据(图片对应的数字)的代码
# coding=utf-8
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# encoding: utf-8
"""A very simple MNIST classifier.
See extensive documentation at
http://tensorflow.org/tutorials/mnist/beginners/index.md
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Import data
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import math
def drawDigit(position, image, title):
plt.subplot(*position)
plt.imshow(image.reshape(-1, 28), cmap='gray_r')
plt.axis('off')
plt.title(title)
def batchdraw():
images, labels = mnist.train.next_batch(196)
image_number = images.shape[0]
row_number = math.ceil(image_number ** 0.5) # type: int
column_number = row_number
plt.figure(figsize=(row_number, column_number))
for i in range( int(row_number)):
for j in range(int(column_number)):
index = int( i * column_number + j)
if index < image_number:
position = (row_number, column_number, index + 1)
image = images[index]
title = 'actual:%d' % (np.argmax(labels[index]))
drawDigit(position, image, title)
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
sess = tf.InteractiveSession()
# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# Train
tf.initialize_all_variables().run()
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x: batch_xs, y_: batch_ys})
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# predint = accuracy.eval({x: mnist.test.images, y_: mnist.test.labels})
test_data = mnist.test.images
test_labels = mnist.test.labels
print(mnist.train.images.shape)
# print(mnist.test.images[0])
# print()
print(dir(mnist.test)[-5:])
print(mnist.test.num_examples)
batch_testxs = mnist.test.next_batch(100)
# plt.figure()
# im = test_data[0].reshape(28, 28)
# # im = batch_testxs[0].reshape(28, 28)
# plt.imshow(im,'gray')
# plt.show()
# im = im.convert('L')test_data
# tv = list(im.getdata())
# y_conv = [(255-x)*1.0/255.0 for x in im]
#显示一组图片,同时打印这组图片对应的labels
# fig, ax = plt.subplots(nrows=4,ncols=5,sharex='all',sharey='all')
# ax = ax.flatten()
# for i in range(20):
# print('test: %s' % np.argmax(test_labels[i]))
# img = test_data[i].reshape(28, 28)
# ax[i].imshow(img,cmap='Greys')
# ax[0].set_xticks([])
# ax[0].set_yticks([])
# plt.tight_layout()
# plt.show()
#end
#显示一张图片,包含数字 和 识别的 结果
batchdraw()
plt.show()
# 显示一张图片,包含数字 和 识别的 结果 结束
prediction = tf.argmax(im, 1)
predint=prediction.eval(feed_dict={x: [mnist.test.images], keep_prob: 1},session=sess)
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))
print(predint[0])
本文介绍了作者从研究人工智能开始,基于TensorFlow的DEMO,修改代码以查看MNIST数据集中的训练图片及其对应数字的过程。
3302

被折叠的 条评论
为什么被折叠?



