Pytorch矩阵基本操作

本篇文章的阅读量是: 653

pdf文件:

导入pytorch库

 from __future__ import print_function
 import torch

导入基本的包、库。

[Why does using from future import print_function breaks Python2-style print? closed]:

First of all, from __future__ import print_function needs to be the first line of code in your script (aside from some exceptions mentioned below). Second of all, as other answers have said, you have to use print as a function now. That’s the whole point of from __future__ import print_function; to bring the print function from Python 3 into Python 2.6+.

First of all, from __future__ import print_function needs to be the first line of code in your script (aside from some exceptions mentioned below). Second of all, as other answers have said, you have to use print as a function now. That’s the whole point of from __future__ import print_function; to bring the print function from Python 3 into Python 2.6+.

 from __future__ import print_function
 ​
 import sys, os, time
 ​
 for x in range(0,10):
  print(x, sep=' ', end='')  # No need for sep here, but okay :)
  time.sleep(1)

__future__ statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning. From the documentation:

A future statement is recognized and treated specially at compile time: Changes to the semantics of core constructs are often implemented by generating different code. It may even be the case that a new feature introduces new incompatible syntax (such as a new reserved word), in which case the compiler may need to parse the module differently. Such decisions cannot be pushed off until runtime.

The documentation also mentions that the only things that can precede a __future__ statement are the module docstring, comments, blank lines, and other future statements.

注意:不要把源文件命名为torch.py,否则会引起循环冲突:

AttributeError: partially initialized module 'torch' has no attribute 'empty' (most likely due to a circular import)

创建矩阵

  1. 构造一个5 * 3未初始化的矩阵
 x = torch.empty(5, 3)
 print(x)

《Pytorch矩阵基本操作》有一个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据