目录
1 基础操作
1.1 torch是否可用
>>> torch.cuda.is_available()
True
1.2 两个好用的工具
1.2.1 dir()查看成员
当所查看成员还有成员时,显示如下:
>>> dir(torch.cuda)
['Any', 'BFloat16Storage', 'BFloat16Tensor' ... 'reset_max_memory_cached', 'reset_peak_memory_stats', 'seed', 'seed_all', 'set_device', 'set_per_process_memory_fraction', 'set_rng_state', 'set_rng_state_all', 'set_stream', 'set_sync_debug_mode', 'sparse', 'stream', 'streams', 'synchronize', 'sys', 'temperature', 'threading', 'torch', 'traceback', 'tunable', 'utilization', 'warnings']
当所查看成员没有成员时,显示如下:
>>> dir(torch.cuda.is_available)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
可以看见该成员下面全是特殊方法,并没有成员了。
Python中双下划线(也叫dunder - double underscore)的作用,特别是__call__这个特殊方法。
在Python中,双下划线包围的方法名(如__call__)是特殊方法或魔术方法(Magic
Methods)。这些方法有特殊的用途,它们允许你定义对象在特定情况下的行为。
__call__在调用时使用的是小括号,__getitems__所接的是方括号
class Counter:
def __init__(self):
self.count = 0
def __call__(self):
self.count += 1
return self.count
# 创建Counter实例
counter = Counter()
# 可以像函数一样调用实例
print(counter()) # 输出: 1
print(counter()) # 输出: 2
print(counter()) # 输出: 3
其他常见的双下划线(dunder)方法包括:
init: 构造函数
str: 字符串表示
len: 长度
getitem: 索引访问
repr: 开发者字符串表示 双下划线的命名约定的主要目的是:避免名称冲突:这种命名方式很特殊,不太可能与普通方法名冲突 标识
特殊用途:清楚地表明这些方法具有特殊的Python语言级别的作用
提供统一接口:让所有Python类都可以实现相同的接口
1.2.1 help()查看使用方法
当成员中没有成员了,就可以使用help函数查看使用方法。
>>> help(torch.cuda.is_available)
Help on function is_available in module torch.cuda:
is_available() -> bool
Return a bool indicating if CUDA is currently available.
(返回布尔值查看cuda是否可用)
2 加载数据集
2.1 主要流程
让我详细解释这些组件之间的关系:


4849

被折叠的 条评论
为什么被折叠?



