一、安装TensorFlow并在Jupyter中配置
1.安装
1.在anacoda prompt中使用命令行进行安装
2.使用语句python查询python版本
3.创建一个虚拟环境
使用虚拟环境可以防止安装错误导致anaconda错误,如果安装错误,可以删除虚拟环境,不用重装Anaconda
4.激活虚拟环境
5.安装Tensorflow 1.8.0版本(由于版本问题,建议选用1.8.0版本)
其中mk1包有157Mb大,可能由于网速或者丢包等原因导致安装失败,此时重复输入命令行进行安装,多试几次。
6.将虚拟环境安装到jupyter notebook中
a.激活Tensorflow环境并安装ipykernel包
conda activate tensorflow
conda install -n tensorflow ipykernel
b.在base环境中安装nb_conda包
conda activate base
conda install nb_conda
c.结果
7.测试
3.报错解决
1.anaconda 不是内部或外部命令
解决方法:将…\Anaconda\Scripts 文件加入环境变量
2.指定Tensorflow 1.8.0版本安装
此处有一个包mk1比较大,如果出现安装失败可以多安装几次,它会继续安装,或者换源安装。
3.关于jupyter notebook使用虚拟环境中tensorflow,可以参考 链接
4.关于"import tensorflow.examples.tutorials.minst.input_data as input_data"时,出现错误:ModuleNotFoundError:No module named ‘tensorflow.examples.tutorials’
可能是版本问题,使用1.8.0版本没有问题.
二、Mnist手写数据集
1.下载源
Mnist手写体下载地址
2.数据结构
在这里贴出的是Mnist手写体数据集的训练集部分,Mnist数据集使用二进制进行存储,4个Bit存储一个数据,在图像集中,前16B存储描述数据集的信息,分别是幻数(标注数据格式),图片数量(60000张),图片矩阵行数(28),
图片矩阵列数(28)。在训练集中,前8Bit存储描述信息,分别是幻数个图片数量。测试集有10000张图像,相关数据格式与训练集相同。
3.导出图像数据
import numpy as np
import struct
import random
import tensorflow as tf
import matplotlib.pyplot as plt
class MnistData():
def __init__(self,train_image_path,train_label_path,test_image_path,test_label_path):
self.train_image_path = train_image_path
self.train_label_path = train_label_path
self.test_image_path = test_image_path
self.test_label_path =test_label_path
self.train_images,self.train_labels = self.get_data(0)
self.test_images,self.test_labels = self.get_data(1)
#定义两个辅助变量,用来判断一个回合的训练是否完成
self.num_of_batch = 0
self.got_batch = 0
#get_data(0)提取训练集 get_data(1)提取测试集
def get_data(self,data_type):
if data_type == 0:
image_path = self.train_image_path
label_path = self.train_label_path
else:
image_path = self.test_image_path
label_path = self.test_label_path
with open(image_path,'rb') as file1:
image_file = file1.read()
with open(label_path,'rb') as file2:
label_file = file2.read()
label_index = 0
image_index = 0
labels = []
images = []
#读取训练集图像数据文件的文件信息
magic,num_of_datasets,rows,colunmns = struct.unpack_from('>IIII',image_file,image_index)
image_index += struct.calcsize('>IIII')
for i in range(num_of_datasets):
#读取784个unsigned byte ,即一副图像的所有像素值
temp = struct.unpack_from('>784B',image_file,image_index)
temp = np.reshape(temp,(28,28))
#归一化处理
temp = temp/255
images.append(temp)
image_index +=struct.calcsize('>784B')#每次增加784B
#跳过描述信息
label_index +=struct.calcsize('>II')
labels = struct