pytorch学习碰到的问题

torch.view

torch.view(-1, 2)中第一位为-1的意思。即让电脑自动推理的意思,详见链接:

reference: https://www.cnblogs.com/MartinLwx/p/10543604.html

torchvision.transform

transform的用法,只需要记住它是对数据预处理便可,详见链接:

torchvision.transforms用法介绍: https://www.jianshu.com/p/1ae863c1e66d

python中enumerate使用

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,以turtle类型返回。一般用在 for 循环当中,常见案例如下:

>>> seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
...     print i, element
...
0 one
1 two
2 three

菜鸟教程:https://www.runoob.com/python/python-func-enumerate.html

vscode中debug参数

在vscode中常见的有字面型变量:int,float,string。如下:

在这里插入图片描述

以及迭代变量:list,turtle,dictionary等。如下:

在这里插入图片描述

还有自定义类型:class。如下:

在这里插入图片描述

python中的with和try…except

with

python中的with用来自动调用对象的--exit--()方法,它具有如下性质:

  • 紧跟with后面的语句被求值后,返回对象的“–enter–()”方法被调用,这个方法的返回值将被赋值给as后面的变量
  • 当with后面的代码块全部被执行完之后,将调用前面返回对象的“–exit–()”方法

案例如下:

class Sample:
    def __enter__(self):
        print "in __enter__"
        return "Foo"
    def __exit__(self, exc_type, exc_val, exc_tb):
        print "in __exit__"
def get_sample():
    return Sample()
with get_sample() as sample:
    print "Sample: ", sample

# ---output---
in __enter__
Sample:  Foo
in __exit__

try…except

使用try…except来处理多种报错问题:

try:
	# read down the code that will go wrong

except:
	# write down the error process 

Python 中 with用法及原理:https://blog.csdn.net/u012609509/article/details/72911564

服务器

Linux端的命令,在服务器端后台运行命令,并且在退出shell后仍然运行的操作。

# 用nohup在后台一直运行脚本程序
nohup python -u test.py > test.out 2>&1 &

# 再次连接服务器杀脚本进程
ps -ef | python
kill -9 [process_number]

# 实时显示nvidia-smi显卡状态
nvidia-smi -l 2

Python代码在Linux后台运行:https://codeantenna.com/a/9gs8QioB4u

python main函数

Python 是一种解释型脚本语言,和 C++、Golang 等语言不同, C++、Golang 等程序都是从 main 函数开始执行,而 Python 程序是从开始到结尾顺序执行。

Python 中的 main() 函数只有当该 Python 脚本直接作为执行程序时才会执行,当该 Python 脚本被作为模块被导入时,其中的 main() 函数将不会被执行。

Python main函数教程:https://haicoder.net/python/python-function-main.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值