chapter 1 Exercises

  1. Start Python to get an interactive prompt.

    • What Python version are you using: 2.x or 3.x?
    1
    !python -V

    运行结果

    1
    Python 3.7.4
  • Can you import torch? What version of PyTorch do you get?

    1
    import torch
    2
    torch.__version__

    运行结果

    1
    '1.4.0'
  • What is the result of torch.cuda.is_available()? Does it match your expectation based on the hardware you’re using?

    1
    torch.cuda.is_available()

    运行结果

    1
    True
  1. Start the Jupyter Notebook server.

    • What version of Python is Jupyter using?
    1
    import sys
    2
    sys.version

    运行结果

    1
    '3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]'
  • Is the location of the torch library used by Jupyter the same as the one you imported from the interactive prompt?

    1
    torch.__config__

    运行结果

    1
    <module 'torch.__config__' from 'D:\\Anaconda3\\lib\\site-packages\\torch\\__config__.py'>

chapter 2 Exercises

  1. Create a tensor a from list(range(9)). Predict then check what the size, offset, and strides are.

    1
    a = list(range(9))
    2
    a = torch.Tensor(a)
    3
    print(a)
    4
    print(a.size())
    5
    print(a.storage_offset())
    6
    print(a.stride())

    运行结果

    1
    tensor([0., 1., 2., 3., 4., 5., 6., 7., 8.])
    2
    torch.Size([9])
    3
    0
    4
    (1,)
  1. Create a tensor b = a.view(3, 3). What is the value of b[1,1]?

    1
    b = a.view(3, 3)
    2
    b[1, 1]

    运行结果

    1
    tensor(4.)
  1. Create a tensor c = b[1:,1:]. Predict then check what the size, offset, and strides are.

    1
    c = b[1:, 1:]
    2
    print(c)
    3
    print(c.size())
    4
    print(c.storage_offset())
    5
    print(c.stride())

    运行结果

    1
    tensor([[4., 5.],
    2
            [7., 8.]])
    3
    torch.Size([2, 2])
    4
    4
    5
    (3, 1)
  1. Pick a mathematical operation like cosine or square root. Can you find a corresponding function in the torch library?

    1
    import math
    2
    math.sin(1)

    运行结果

    1
    0.8414709848078965
    1
    a = torch.Tensor([1])
    2
    a.sin()

    运行结果

    1
    tensor([0.8415])
  1. Is there a version of your function that operates in-place?
1
a.sin_()
2
a

运行结果

1
tensor([0.8415])