在Kubernetes上的JupyterHub,通过Notebook快速运行PyTorch例程,测试镜像是否可用。

1、快速安装

在我的镜像中,已经将PyTorch、TorchVision打包到JupyterHub for K8s的Notebook镜像中,可以直接使用。

或者,在notebook中进行安装,如下:

%%bash
pip install torch torchvision

2、导入引用库

from __future__ import print_function, division
import os
import torch

3、运行例程

x = torch.rand(5, 3)
print(x)

输出:
tensor([[0.4482, 0.9189, 0.2227],
        [0.3906, 0.4695, 0.1300],
        [0.5034, 0.7224, 0.0471],
        [0.5570, 0.4676, 0.8005],
        [0.0363, 0.2650, 0.1269]])

4、查看torchvision库方法

查看torchvison的信息:

import torchvision
help(torchvision)

输出信息:

Help on package torchvision:

NAME
    torchvision

PACKAGE CONTENTS
    _C
    datasets (package)
    models (package)
    ops (package)
    transforms (package)
    utils
    version

FUNCTIONS
    get_image_backend()
        Gets the name of the package used to load images

    set_image_backend(backend)
        Specifies the package used to load images.

        Args:
            backend (string): Name of the image backend. one of {'PIL', 'accimage'}.
                The :mod:`accimage` package uses the Intel IPP library. It is
                generally faster than PIL, but does not support as many operations.

VERSION
    0.3.0

FILE
    /opt/conda/lib/python3.6/site-packages/torchvision/__init__.py

5、查看torch库方法

help(torch)

输出信息:

Help on package torch:

NAME
    torch

DESCRIPTION
    The torch package contains data structures for multi-dimensional
    tensors and mathematical operations over these are defined.
    Additionally, it provides many utilities for efficient serializing of
    Tensors and arbitrary types, and other useful utilities.

    It has a CUDA counterpart, that enables you to run your tensor computations
    on an NVIDIA GPU with compute capability >= 3.0.

PACKAGE CONTENTS
    _C
    __config__
    _dl
    _jit_internal
    _ops
    _six
    _storage_docs
    _tensor_docs
    _tensor_str
    _thnn (package)
    _torch_docs
    _utils
    _utils_internal
    autograd (package)
    backends (package)
    contrib (package)
    cuda (package)
    distributed (package)
    distributions (package)
    for_onnx (package)
    functional
    hub
    jit (package)
    multiprocessing (package)
    nn (package)
    onnx (package)
    optim (package)
    quasirandom
    random
    serialization
    sparse (package)
    storage
    tensor
    testing (package)
    utils (package)
    version

SUBMODULES
    cpp
    ops

......

更多参考:

07-09 18:15