python - utils.py 放在哪里

标签 python azure jupyter-notebook python-import

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

它表示 utils.py 应该与代码位于同一文件夹中。我尝试在 myconda-environment 中安装 python-utils,但这并没有解决问题。使用 pip install utils 之后,我宁愿让它变得更糟:-(

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

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

我在运行 Azure SDK 和 python 3.6 的单独环境中使用 Anaconda。

最佳答案

根据你的描述,我认为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.

这里是utils.py的源代码.

# 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 Notebook,请参阅下面的步骤。

  1. 进入您的项目页面,点击新建按钮并选择空白文件enter image description here
  2. 然后将文件命名为 utils.py 并按 Enter 键。 enter image description here
  3. 选择文件并单击编辑文件enter image description here
  4. 从教程 Github 存储库中复制并粘贴 utils.py 的内容,然后单击保存文件enter image description here
  5. 创建一个笔记本来测试导入实用程序,它有效。 enter image description here

所以# makesure utils.py is in the same directory as this code意思如下图。

enter image description here

关于python - utils.py 放在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326377/

相关文章:

python - Python 迭代工具

azure - 使用 Express Web 应用程序设置 Azure DevOps 源代码控制时出错

pandas - 在pandas中使用groupby或aggregate的最佳方法

visual-studio-code - VS Code 中的 Jupyter Notebook : inline input()?

python - AWS 路由表导出详细信息到 CSV

python - 列表的逻辑索引

python - 带有 Python 列表的 header

azure - 如何将 Blob 和表添加到同一个 Azure 存储

C# - 具有托管服务身份的 Azure 存储

python - Jupyter - Python 3 内核死亡