放在哪里实用工具.py

2024-05-23 22:48:58 发布

您现在位置:Python中文网/ 问答频道 /正文

亲爱的,我正在为MNIST数据集运行第一个Azure教程。在

上面写着实用工具.py应该与代码在同一个文件夹中。我试图在myconda环境中安装python实用程序,但这并没有解决问题。在使用了pip install utils之后,我宁愿让它变得更糟:-(

这可能很简单,但我卡住了。
您将如何在运行的笔记本上执行此操作:

  1. 本地
  2. 在Azure笔记本中

我在运行azuresdk和python3.6的单独环境中使用Anaconda。在


Tags: installpip数据代码py实用程序文件夹环境
1条回答
网友
1楼 · 发布于 2024-05-23 22:48:58

根据您的描述,我认为MNIST数据集的第一个Azure教程是Tutorial: Train an image classification model with Azure Machine Learning service。在

您可以通过教程中的链接找到所有源代码,如下所示:here。在

Get the notebook

For your convenience, this tutorial is available as a Jupyter notebook. Run the tutorials/img-classification-part1-training.ipynb notebook either in Azure Notebooks or in your own Jupyter notebook server.

这是^{}的源代码。在

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import gzip
import numpy as np
import struct


# load compressed MNIST gz files and return numpy arrays
def load_data(filename, label=False):
    with gzip.open(filename) as gz:
        struct.unpack('I', gz.read(4))
        n_items = struct.unpack('>I', gz.read(4))
        if not label:
            n_rows = struct.unpack('>I', gz.read(4))[0]
            n_cols = struct.unpack('>I', gz.read(4))[0]
            res = np.frombuffer(gz.read(n_items[0] * n_rows * n_cols), dtype=np.uint8)
            res = res.reshape(n_items[0], n_rows * n_cols)
        else:
            res = np.frombuffer(gz.read(n_items[0]), dtype=np.uint8)
            res = res.reshape(n_items[0], 1)
    return res


# one-hot encode a 1-D array
def one_hot_encode(array, num_of_classes):
    return np.eye(num_of_classes)[array.reshape(-1)]

如果你想将它导入到Azure Jupyter笔记本中,请参阅我下面的步骤。在

  1. 进入项目页面,单击New按钮并选择Blank Fileenter image description here
  2. 然后将文件命名为utils.py,并按Enter键。 enter image description here
  3. 选择文件并单击Edit Fileenter image description here
  4. 从教程Github repo复制并粘贴utils.py的内容,然后单击Save Fileenter image description here
  5. 创建一个笔记本来测试import utils,它可以工作。 enter image description here

所以# make sure utils.py is in the same directory as this code的意思是如下图所示。在

enter image description here

相关问题 更多 >