由于网络不通,无法通过 load_data 直接下载加载数据集。
可以在网上离线下载保存。

在jupyterlab中输入

?? tf.keras.datasets.fashion_mnist.load_data

在这里插入图片描述

可以直接查看函数源码。因为还涉及到一些包的import,在服务器上可以直接拷贝fashion_mnist.py 文件到当前目录进行查看,然后将相应的path[0] 这些替换成自己下载好的文件目录即可。

具体代码可以参考

import os
import numpy as np
import pandas as pd
import sys
import gzip

os.getcwd()

#dirname = os.path.join('data/', 'fashion-mnist')
dirname = 'data'
#base = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/'
base = ''
files = [
  'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
  't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
]

paths = []
for fname in files:
    paths.append(dirname+'/'+ fname)

with gzip.open(paths[0], 'rb') as lbpath:
    y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

with gzip.open(paths[1], 'rb') as imgpath:
    x_train = np.frombuffer(
    imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)

with gzip.open(paths[2], 'rb') as lbpath:
    y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)

with gzip.open(paths[3], 'rb') as imgpath:
    x_test = np.frombuffer(
    imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐