import tensorflow as tf
import os
import pickle
import numpy as np
CIFAR_DIR = "C:/Users/tianr/Desktop/cifar-10-batches-py"
print(os.listdir(CIFAR_DIR))
def load_data(filename):
"""read data from data file"""
with open(filename,'rb') as f:
data = pickle.load(f,encoding='bytes')
return data[b'data'],data[b'labels']
# API:tesorflow.Dataset
class CifarData:
def __init__(self,filenames,need_shuffle):
all_data = []
all_labels = []
for filename in filenames:
data,labels = load_data(filename)
all_data.append(data)
all_labels.append(labels)
self._data = np.vstack(all_data)
self._data = self._data / 127.5 - 1# 归一化将像素缩放到[-1,1]
self._labels = np.hstack(all_labels)
print(self._data.shape)
print(self._labels.shape)
self._num_examples = self._data.shape[0]
self._need_shuffle = need_shuffle
self._indicator = 0 #定义数据起始位置
if self._need_shuffle:
self._shuffle_data()
def _shuffle_data(self):
# [0,1,2,3,4,5] -> [3,1,2,5,0,4]
p = np.random.permutation(self._num_examples)
self._data = self._data[p]
self._labels = self._labels[p]
def next_batch(self,batch_size):
"""return batch_size examples as a batch"""
end_indi
卷积神经网络部分,每训练到一定步就报错,什么原因?
最新推荐文章于 2023-03-31 15:26:45 发布