亲爱的,我正在运行 MNIST 数据集的第一个 Azure 教程。
它表示 utils.py 应该与代码位于同一文件夹中。我尝试在 myconda-environment 中安装 python-utils,但这并没有解决问题。使用 pip install utils 之后,我宁愿让它变得更糟:-(
这可能很简单,但我被困住了。
您将如何在运行以下设备的笔记本上执行此操作:
- 本地
- 在 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,请参阅下面的步骤。
- 进入您的项目页面,点击
新建
按钮并选择空白文件
。 - 然后将文件命名为
utils.py
并按Enter
键。 - 选择文件并单击
编辑文件
。 - 从教程 Github 存储库中复制并粘贴
utils.py
的内容,然后单击保存文件
。 - 创建一个笔记本来测试
导入实用程序
,它有效。
所以# makesure utils.py is in the same directory as this code
意思如下图。
关于python - utils.py 放在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326377/