keras-source-1

该博客主要介绍Keras中用于加载数据的`keras.datasets.mnist`和管理文件下载的`keras.utils.data_utils.get_file`。同时,它探讨了如何使用`keras.utils.generic_utils.Progbar`来显示进度状态,并更新进度。内容涉及系统目录操作和屏幕打印,适合数据输入/输出和可视化场景。
摘要由CSDN通过智能技术生成
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# load data with keras.datasets.mnist.load_data

The code of keras.datasets.mnist

from __future__ import absolute_import # use std lib
from __future__ import division # use accurate division
from __future__ import print_function # use py3 print
from ..utils.data_utils import get_file # fetch file
import numpy as np
def load_data(path='mnist.npz'):
	# load data using keras.utils.data_utils.get_file
    path = get_file(path, # path is the fname
                    origin='https://s3.amazonaws.com/img-datasets/mnist.npz',
                    file_hash='8a61469f7ea1b51cbae51d4f78837e45')
    f = np.load(path) # open and read
    x_train, y_train = f['x_train'], f['y_train'] # assgian dict values
    x_test, y_test = f['x_test'], f['y_test']
    f.close() # close the file
    return (x_train, y_train), (x_test, y_test) # tuple of numpy arrays

the code of keras.utils.data_utils.get_file

def get_file(fname,
             origin,
             untar=False,
             md5_hash=None,
             file_hash=None,
             cache_subdir='datasets',
             hash_algorithm='auto',
             extract=False,
             archive_format='auto',
             cache_dir=None):
    if cache_dir is None: # if no dir is indicated, use the default dir
        cache_dir = os.path.join(os.path.expanduser('~'), '.keras') # default dir is ~/.keras
    if md5_hash is not None and file_hash is None: # if no hash is assigned to file
        file_hash = md5_hash 
        hash_algorithm = 'md5' # use md5 algorithm to decode
    datadir_base = os.path.expanduser(cache_dir) # ~/.keras
    if not os.access(datadir_base, os.W_OK): # if unable to write
        datadir_base = os.path.join('/tmp', '.keras') # change path to /tmp/.keras
    datadir = os.path.join(datadir_base, cache_subdir) # assemble paths, ~/.keras/datasets
    if not os.path.exists(datadir): # if dir is not exist
        os.makedirs(datadir) # mkdir
    if untar: # if using untar
        untar_fpath = os.path.join(datadir, fname) # ~/.keras/datasets/mnist.npz
        fpath = untar_fpath + '.tar.gz' # ~/.keras/datasets/mnist.npz.tar.gz
    else: # if untar is not needed
        fpath = os.path.join(datadir, fname) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值