python3怎么使用mnist_loader_mnist_loader.py

该博客介绍了如何使用 python3 的 mnist_loader 模块来加载和处理 MNIST 数据集,包括不同模式的数据预处理(如 vanilla、randomly_binarized 和 rounded_binarized)以及加载训练和测试数据。此外,还提供了选择 EMNIST 数据集的功能。
摘要由CSDN通过智能技术生成

import gzip

import os

import struct

from array import array

import random

_allowed_modes = (

# integer values in {0..255}

'vanilla',

# integer values in {0,1}

# values set at 1 (instead of 0) with probability p = orig/255

# as in Ruslan Salakhutdinov and Iain Murray's paper

# 'On The Quantitative Analysis of Deep Belief Network' (2008)

'randomly_binarized',

# integer values in {0,1}

# values set at 1 (instead of 0) if orig/255 > 0.5

'rounded_binarized',

)

_allowed_return_types = (

# default return type. Computationally more expensive.

# Useful if numpy is not installed.

'lists',

# Numpy module will be dynamically loaded on demand.

'numpy',

)

np = None

def _import_numpy():

# will be called only when the numpy return type has been specifically

# requested via the 'return_type' parameter in MNIST class' constructor.

global np

if np is None: # import only once

try:

import numpy as _np

except ImportError as e:

raise MNISTException(

"need to have numpy installed to return numpy arrays."\

+" Otherwise, please set return_type='lists' in constructor."

)

np = _np

else:

pass # was already previously imported

return np

class MNISTException(Exception):

pass

class MNIST(object):

def __init__(self, path='.', mode='vanilla', return_type='lists', gz=False):

self.path = path

assert mode in _allowed_modes, \

"selected mode '{}' not in {}".format(mode,_allowed_modes)

self._mode = mode

assert return_type in _allowed_return_types, \

"selected return_type '{}' not in {}".format(

return_type,

_allo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值