这个程序是采用TensorFlow一维卷积神经网络实现MNIST数据集的的数字识别,图像适合采用二维卷积,在这里仅仅是想尝试一下一维卷积的操作方法。
#2021.11.8 HIT ATCI LZH
#TensorFlow一维卷积神经网络实现MNIST数据集的的数字识别,仅仅是想尝试一维卷积的操作方法
#从mnist中选取一部分样本
from __future__ import division,print_function#python2中也能使用python3的函数
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np #矩阵操作库
#Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot = True)
#从训练集中选取前num个数据
def train_size(num):
print('Total Training Image in Dataset = '+
str(mnist.train.images.shape))
print('-----------------------------------')
x_train = mnist.train.images[:num,:]
print('x_train Examples Loaded = ' + str(x_train.shape))
y_train = mnist.train.labels[:num,:]
print('y_train Examples Loaded = ' + str(y_train.shape))
print('')
return x_train,y_train
#从测试集中选取前num个数据
def test_size(num):
print('Total Test Examples in Dataset = '+
str(mnist.test.images.shape))
print('-----------------------------------')
x_test = mnist.test.images[:num,:]
print('x_test Examples Loaded =' + str(x_test.shape))
y_test = mnist.test.labels[:num,:]
print('y_test Examples Loaded =' + str(y_test.shape))
return x_test,y_test
#显示训练集中的第num个数据,以图片的形式显示
def display_digit(num):
print(y_train[num])
label = y_train[num].argmax(axis = 0)#返回最大值索引值,也即是该数字大小
images = x_train[num].reshape([28,28])