我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用pickle._Unpickler()。
def download_embedding(): """ Download files from web Seems cannot download by pgm Download from: https://sites.google.com/site/rmyeid/projects/polyglot Returns: A tuple (word, embedding). Emebddings shape is (100004, 64). """ assert (tf.gfile.Exists(FLAGS.chr_embedding_dir)), ( "Embedding pkl don't found, please \ download the Chinese chr embedding from https://sites.google.com/site/rmyeid/projects/polyglot" ) with open(FLAGS.chr_embedding_dir, 'rb') as f: u = pickle._Unpickler(f) u.encoding = 'latin1' p = u.load() return p
def load_mnist(): url = "http://deeplearning.net/data/mnist/mnist.pkl.gz" mnist_compressed = "mnist.pkl.gz" if not exists(mnist_compressed): print("Downloading MNIST") urlretrieve(url, mnist_compressed) # Load the dataset with gzip.open(mnist_compressed, "rb") as f: u = pickle._Unpickler(f) u.encoding = "latin1" data = u.load() data = [(X.reshape(-1, 28, 28), y) for X, y in data] return data
def get_dataset(): f = gzip.open('mnist.pkl.gz', 'rb') u = pickle._Unpickler(f) u.encoding = 'latin1' train_set, valid_set, test_set = u.load() f.close() return train_set, valid_set, test_set
def _load_data(self): script_dir = os.path.dirname(__file__) mnist_file = os.path.join(os.path.join(script_dir, 'data'), 'mnist.pkl.gz') with gzip.open(mnist_file, 'rb') as mnist_file: u = pickle._Unpickler(mnist_file) u.encoding = 'latin1' train, val, test = u.load() return train, val, test
def load(filename): with open(filename, "rb") as f: unpickler = pickle._Unpickler(f) while True: try: yield unpickler.load() except EOFError: break
def load_data(self, file_name): with open(file_name, 'rb') as file: unpickler = pickle._Unpickler(file) unpickler.encoding = 'latin1' contents = unpickler.load() X, Y = np.asarray(contents['data'], dtype=np.float32), np.asarray(contents['labels']) one_hot = np.zeros((Y.size, Y.max() + 1)) one_hot[np.arange(Y.size), Y] = 1 return X, one_hot
def load_data(dataset): ''' Loads the dataset :type dataset: string :param dataset: the path to the dataset (here MNIST) ''' ############# # LOAD DATA # ############# # Download the MNIST dataset if it is not present data_dir, data_file = os.path.split(dataset) if data_dir == "" and not os.path.isfile(dataset): # Check if dataset is in the data directory. new_path = os.path.join( os.path.split(__file__)[0], "data", dataset ) if os.path.isfile(new_path) or data_file == 'mnist.pkl.gz': dataset = new_path if (not os.path.isfile(dataset)) and data_file == 'mnist.pkl.gz': import urllib origin = ( 'http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz' ) print('Downloading data from %s' % origin) urllib.urlretrieve(origin, dataset) print('loading data...') # Load the dataset f = gzip.open(dataset, 'rb') if sys.version_info[0] == 3: u = pickle._Unpickler(f) u.encoding = 'latin1' train_set, valid_set, test_set = u.load() else: train_set, valid_set, test_set = pickle.load(f) f.close() #train_set, valid_set, test_set format: tuple(input, target) #input is an numpy.ndarray of 2 dimensions (a matrix) #which row's correspond to an example. target is a #numpy.ndarray of 1 dimensions (vector)) that have the same length as #the number of rows in the input. It should give the target #target to the example with the same index in the input. return train_set, valid_set, test_set