《动手学深度学习》手动导入数据集产生错误的解决方法

动手学深度学习

书本网页版 https://zh.gluon.ai/chapter_preface/preface.html

b站视频讲解 https://space.bilibili.com/209599371?spm_id_from=333.788.b_765f7570696e666f.1

书本源代码、pdf及数据集 链接:https://pan.baidu.com/s/1U53gc7ZIXsF1U23x1g8g4A  提取码:f793

 

在运行书本源代码的时候

需要导入数据集(没有数据集的话第一次会下载数据集

但是 可能出现各种各样的错误导致 我们不能自动下载数据集

(网络问题或者后台路径问题等等

参考资料:https://discuss.gluon.ai/t/topic/642/32

先试着加镜像 

set MXNET_GLUON_REPO=https://apache-mxnet.s3.cn-north-1.amazonaws.com.cn/ jupyter notebook

如果 还是RunTimeError 可以试试我的方法

 

我的解决方法是 使用了同学的数据集以及修改源代码 dataset.py

最后有加 查找SHA1值的方法

1. 四个数据集放在C:\Users\OneLine\.mxnet\datasets\fashion-mnist 文件夹内 

(不用解压)

也可以根据网页提示找到原本的下载路径

2. 对比我原来的dataset.py 和同学的dataset.py 稍微修改一下 (建议直接下拉到末尾

dataset.py的位置在 D:\Anaconda\envs\gluon\Lib\site-packages\mxnet\gluon\data\vision

可能不是D盘 要看自己安装软件的位置 ~\mxnet\gluon\data\vision 找到这一串应该没错

我的 :

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# coding: utf-8
# pylint: disable=
"""Dataset container."""
__all__ = ['MNIST', 'FashionMNIST', 'CIFAR10', 'CIFAR100',
           'ImageRecordDataset', 'ImageFolderDataset']

import os
import gzip
import tarfile
import struct
import warnings
import numpy as np

from .. import dataset
from ...utils import download, check_sha1, _get_repo_file_url
from .... import nd, image, recordio, base


class MNIST(dataset._DownloadedDataset):
    """MNIST handwritten digits dataset from http://yann.lecun.com/exdb/mnist

    Each sample is an image (in 3D NDArray) with shape (28, 28, 1).

    Parameters
    ----------
    root : str, default $MXNET_HOME/datasets/mnist
        Path to temp folder for storing data.
    train : bool, default True
        Whether to load the training or testing set.
    transform : function, default None
        A user defined callback that transforms each sample. For example::

            transform=lambda data, label: (data.astype(np.float32)/255, label)

    """
    def __init__(self, root=os.path.join(base.data_dir(), 'datasets', 'mnist'),
                 train=True, transform=None):
	self._train = train
	self._train_data = ('train-images-idx3-ubyte.gz','0cf37b0d40ed5169c6b3aba31069a9770ac9043d')
        self._train_label = ('train-labels-idx1-ubyte.gz','236021d52f1e40852b06a4c3008d8de8aef1e40b')
        self._test_data = ('t10k-images-idx3-ubyte.gz','626ed6a7c06dd17c0eec72fa3be1740f146a2863')
        self._test_label = ('t10k-labels-idx1-ubyte.gz','17f9ab60e7257a1620f4ad76bbbaf857c3920701')
	self._namespace = 'mnist'
        super(MNIST, self).__init__(root, transform)

    def _get_data(self):
        if self._train:
            data, label = self._train_data, self._train_label
        else:
            data, label = self._test_data, self._test_label

        namespace = 'gluon/dataset/'+self._namespace
        data_file = download(_get_repo_file_url(namespace, data[0]),
                             path=self._root,
                             sha1_hash=data[1])
        label_file = download(_get_repo_file_url(namespace, label[0]),
                              path=self._root,
                              sha1_hash=label[1])

        with gzip.open(label_file, 'rb') as fin:
            struct.unpack(">II", fin.read(8))
            label = np.frombuffer(fin.read(), dtype=np.uint8).astype(np.int32)

        with gzip.open(data_file, 'rb') as fin:
            struct.unpack(">IIII", fin.read(16))
            data = np.frombuffer(fin.read(), dtype=np.uint8)
            data = data.reshape(len(label), 28, 28, 1)

        self._data = nd.array(data, dtype=data.dtype)
        self._label = label


class FashionMNIST(MNIST):
    """A dataset of Zalando's article images consisting of fashion products,
    a drop-in replacement of the original MNIST dataset from
    https://github.com/zalandoresearch/fashion-mnist

    Each sample is an image (in 3D NDArray) with shape (28, 28, 1).

    Parameters
    ----------
    root : str, default $MXNET_HOME/datasets/fashion-mnist'
        Path to temp folder for storing data.
    train : bool, default True
        Whether to load the training or testing set.
    transform : function, default None
        A user defined callback that transforms each sample. For example::

            transform=lambda
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 使用TensorFlow进行深度习的CIFAR-10数据集的模型训练和识别可以遵循以下步骤: 1. 导入必要的库和CIFAR-10数据集:首先,需要导入TensorFlow和其他必要的库(例如numpy、matplotlib等),并下载CIFAR-10数据集。可以使用TensorFlow内置的数据集API或手动下载数据集。 2. 数据预处理:在将数据提供给模型之前,需要对数据进行预处理,例如将像素值归一化、对标签进行独热编码等。 3. 构建模型:可以使用TensorFlow的高级API(例如Keras)来构建深度习模型。通常,使用卷积神经网络(CNN)来处理图像数据。可以根据任务的要求设计适当的网络结构。 4. 模型编译和训练:在将数据提供给模型之前,需要对数据进行预处理,例如将像素值归一化、对标签进行独热编码等。使用适当的优化器和损失函数来编译模型,并使用训练集训练模型。 5. 模型评估:在训练模型后,可以使用测试集对模型进行评估。可以计算准确率、损失函数值等指标。 6. 模型应用:在模型训练和评估后,可以使用模型进行预测。提供新的输入数据,模型将返回预测结果。 总之,使用TensorFlow进行CIFAR-10数据集的深度习模型训练和识别需要对数据进行预处理、构建CNN模型、编译和训练模型、评估模型以及应用模型进行预测。 ### 回答2: 深度习是一种以神经网络为基础的机器方法,而TensorFlow是一个由谷歌公司开发的支持深度习的开源框架。在TensorFlow框架下,使用卷积神经网络(Convolutional Neural Network,CNN)对CIFAR-10数据集进行分类是一个非常经典的任务。 CIFAR-10数据集包含了10个不同的分类标签,包括飞机、汽车、鸟、猫、鹿、狗、青蛙、马、船和卡车,每类数据有5000个训练样本和1000个测试样本。因此,模型的训练可以使用训练集进行,而测试过程则基于测试集进行。 在构建CNN模型时,可以使用序列化API(Sequential API)或函数式API(Functional API)进行设计和开发。对于序列化API,可以选择将不同的深度网络层(如卷积层、池化层、全连接层等)按照顺序叠加组合。而对于函数式API,则允许具有多个输入和输出的复杂模型。典型的CNN模型包括卷积层、池化层和全连接层。 在模型训练过程中,常用的优化器是随机梯度下降(Stochastic gradient descent,SGD)。还可以使用其他优化算法,如动量法(Momentum),Adagrad和Adam。同时,在训练过程中,需要考虑过拟合的问题,采取常用的对策如正则化、dropout等。 在模型训练完成后,测试模型将是一个核心的任务。可以通过计算模型的精确性(accuracy)和损失函数(loss)来验证模型的准确性。而常用的评估指标还包括ROC曲线、AUC值等。 总之,TensorFlow的深度习在CIFAR-10数据集上的模型训练与识别任务是一个非常具有挑战性的任务。它需要深入理解神经网络的架构和特性,同时也需要熟悉常用的训练算法和评估指标。随着不断发展的人工智能技术和深度习模型,TensorFlow在各个领域的应用前景非常广阔。 ### 回答3: TensorFlow是一个深度习框架,广泛应用于研究和工业应用中。CIFAR10是一个广泛使用的图像分类数据集,由10个类别的60000张32x32像素图像组成。 TensorFlow提供了许多工具和API来训练和识别基于CIFAR10数据集的模型。 1. 准备数据 CIFAR10数据集可以从网上下载,通过使用Python编写脚本可以将其转换成可用于TensorFlow训练和识别的格式。例如,使用TensorFlow的数据输入库,可以将图像预处理,并将其转换为模型输入的batch格式。 2. 定义模型 可以使用TensorFlow构建各种类型的神经网络来训练CIFAR10数据集,如卷积神经网络(CNN)和循环神经网络(RNN)。TensorFlow提供的各种API可以方便地定义网络的结构和层,从而创建一个高效的图像分类器。 3. 训练模型 要训练模型,需要使用TensorFlow的优化器来计算和调整网络权重和偏置,以最小化损失函数。在训练期间,还需要定义一些指标来监控模型的性能和进度。可以使用TensorFlow提供的训练API,创建训练循环,并针对单个或多个GPU进行分布式训练。 4. 评估和测试模型 为确保神经网络在完全独立于训练数据的环境中运行良好,需要对其进行评估和测试。可以使用TensorFlow提供的API,对模型的准确性和性能进行评估。评估模型时,可以使用验证数据集,在训练过程中监控模型的泛化性能,避免过拟合。 5. 部署模型 一旦模型经过训练并通过评估,就可以使用TensorFlow的模型导出和转换工具,将其部署到生产环境中。TensorFlow支持将模型导出为TensorFlow Lite格式,以在移动设备上运行,并提供了Java,C++和Python等多种语言的API,便于将模型嵌入到应用程序中。 总之,TensorFlow提供了几种方法来训练和识别CIFAR10数据集上的模型。可以结合使用TensorFlow的API,优化器和分布式训练工具,以提高模型效率和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值