7. 让__getitem__ item不变
In Pytorch DataLoader, __getitem__ item is not fixed when
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=False, num_workers=2)
关闭shuffle,再在__init__里自己写个shuffle:
random.seed(27)
random.shuffle(self.data)
6. Pytorch 调节学习率并输出当前学习率
https://pytorch.org/docs/stable/optim.html
# Assuming optimizer uses lr = 0.05 for all groups
# lr = 0.05 if epoch < 30
# lr = 0.005 if 30 <= epoch < 80
# lr = 0.0005 if epoch >= 80
scheduler = MultiStepLR(optimizer, milestones=[30,80], gamma=0.1)
for epoch in range(100):
print("lr={}".format(scheduler.get_last_lr()))
train(...)
validate(...)
scheduler.step()
A bug in printing the current learning rate: https://github.com/pytorch/pytorch/issues/22107
Use get_last_lr(), not get_lr().
5. Pycharm卡顿
File > Settings > Appearance > Windows Options > show memory indicator (Tick it) > OK
查看右下角的memory
help > Find Action > Insert VM Options > Open Edit Custom VM Options > Open pycharm64.vmoptions
修改-Xms和-Xmx,例如
-Xms512m
-Xmx2048
4. Pycharm Warning:
External file changes sync may be slow: The current inotify(7) watch limit is too low.
Solution: From https://robbinespu.github.io/eng/2018/08/25/slow_file_changes_sync_inotify_limits.html
Inotify requires a "watch handle" to be set for each directory in the project.
The default limit of watch handles may not be enough for reasonably sized projects.
Reaching the limit will force IntelliJ platform to fall back to recursive scans of directory trees.
查看当前设置:
$ cat /proc/sys/fs/inotify/max_user_watches
8192
如果不够,修改限制(512K):
$ sudo vim /etc/sysctl.d/60-jetbrains.conf
粘贴如下内容:
# Set inotify watch limit high enough for IntelliJ IDEA (PhpStorm, PyCharm, RubyMine, WebStorm).
# Create this file as /etc/sysctl.d/60-jetbrains.conf (Debian, Ubuntu), and
# run `sudo service procps start` or reboot.
# Source: https://confluence.jetbrains.com/display/IDEADEV/Inotify+Watches+Limit
#
# More information resources:
# -$ man inotify # manpage
# -$ man sysctl.conf # manpage
# -$ cat /proc/sys/fs/inotify/max_user_watches # print current value in use
fs.inotify.max_user_watches = 524288
保存关闭:
$ sudo sysctl -p --system
重启Pycharm
3. Numpy.mean() axis
From https://blog.csdn.net/sinat_38857565/article/details/94601382
axis等于n,在n轴求平均;axis等于(n, m),在n,m轴求平均。
# If a.shape = (3, 5)
np.mean(a,axis=0)
# Then shape = 5
np.mean(a,axis=1)
# Then shape = 3
# If a.shape = (2, 3, 5)
np.mean(a,axis=0)
# Then shape = (3, 5)
np.mean(a,axis=1)
# Then shape = (2, 5)
np.mean(a,axis=(0, 1))
# Then shape = 5
np.mean(a,axis=(0, 2))
# Then shape = 3
2. Enumerate in python
From https://blog.csdn.net/churximi/article/details/51648388
# These two equal.
for i in range (len(list1)):
print i ,list1[i]
for index, item in enumerate(list1):
print index, item
# 1 means index starts from 1.
for index, item in enumerate(list1, 1):
print index, item
# Read files.
count = len(open(filepath, 'r').readlines())
count = 0
for index, line in enumerate(open(filepath,'r')):
count += 1
1. 编译.py为.pyc和.pyo
python -m py_compile file.py
python -O -m py_compile file.py