python函数手册(1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DreamHome_S/article/details/84072248

copy

copy.copy(x)
Return a shallow copy of x.

copy.deepcopy(x)
Return a deep copy of x.

深复制和浅复制的区别:

  • A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
  • A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

PyTorch

模型保存和恢复

model.state_dict()
返回一个字典,保存着module的所有状态(state)。

parameters和persistent buffers都会包含在字典中,字典的key就是parameter和buffer的 names。

保存

torch.save(the_model.state_dict(), PATH)

恢复

the_model = TheModelClass(*args, **kwargs)
the_model.load_state_dict(torch.load(PATH))

input.size(0)

input是包含batchsize维度为4tensor,即(batchsize,channels,x,y)x.size(0)batchsize的值。
NOTE: x = x.view(x.size(0), -1)简化x = x.view(batchsize, -1)。

学习率衰减

class torch.optim.lr_scheduler.StepLR(optimizer, step_size, gamma=0.1, last_epoch=-1)

将每个参数组的学习速率设置为每个step_size时间段由gamma衰减的初始lr。当last_epoch = -1时,将初始lr设置为lr

optimizer (Optimizer) – 包装的优化器。
step_size (int) – 学习率衰减期。
gamma (float) – 学习率衰减的乘积因子。默认值:-0.1。
last_epoch (int) – 最后一个时代的指数。默认值:1。

model.eval()

model变成测试模式,这主要是对dropoutbatch normalization的操作在训练和测试的时候是不一样的。框架会自动把BNDropOut固定住,不会取平均,而是用训练好的值。

train(mode=True)

将模块设置为训练模式。

仅仅当模型中有DropoutBatchNorm是才会有影响。

猜你喜欢

转载自blog.csdn.net/DreamHome_S/article/details/84072248
今日推荐