sacred

Sacred 安装

# 主角
pip install sacred

# 用于数据库连接
pip install numpy pymongo

MongoDB 安装

MongoDB 是一个数据库管理系统,这里用作 Sacred 的存储后端。

在 ubuntu 上的 MongoDB 安装可以参考 Install MongoDB Community Edition on Ubuntu,其他系统也可以在该网站上找到对应的安装方式。

mongoDB 常用命令

# 启动
sudo service mongod start

# 停止
sudo service mongod stop

# 重启
sudo service mongod restart

# 进入MongoDB
mongosh

创建一个名为 sacred 的数据库,用作 sacred 工具的后端存储:

# 进入MongoDB
mongosh

# 创建sacred数据库。use命令切换数据库,没有该数据就会自动创建一个
use sacred

Omniboard安装

在 Ubuntu 机器上安装版本≥v8 的 Node.js,系统默认 apt 安装的版本不够,需要手动安装

安装node.js和npm

sudo apt install nodejs
sudo apt install npm

新版本安装(hexo不支持)

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

nvm指定版本安装

proxychains4 wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | proxychains4 bash
source ~/.zshrc
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node/
export NVM_IOJS_ORG_MIRROR=http://npm.taobao.org/mirrors/iojs
nvm install 12.16.2
nvm use 12.16.2

换源

sudo npm install -g nrm
nrm ls
nrm use taobao
# 4. 测试安装版本信息
node -v
npm version
npx -v

第二步,npm 安装 omniboard

npm install -g omniboard

第三步,开启 omniboard 服务。平时也是用该命令开启 omniboard 可视化前端

# 开启用法
omniboard -m hostname:port:database

# 默认情况下如下,其中27017是MongoDB的端口
omniboard -m localhost:27017:sacred

第四步,打开 http://localhost:9000 来查看前端,并进行管理。

添加远程查看

ssh -L 9000:127.0.0.1:9000 -p 10102 [email protected]

使用案例

使用 yunjey 的一个 pytorch 教程作为演示,代码是演示用 pytorch 实现基于 CNN 的 MINIST 手写数字识别。

根据 Sacred 文档稍作修改,就可以演示如何进行实验的记录。

更多用法请去看 Sacred 文档:Welcome to Sacred’s documentation!。内容超丰富,功能超级多。

代码:

from sacred import Experiment
from sacred.observers import MongoObserver
from sacred.utils import apply_backspaces_and_linefeeds

import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms


ex = Experiment("mnist_cnn")
ex.observers.append(MongoObserver.create(url='localhost:27017',
db_name='sacred'))
ex.captured_out_filter = apply_backspaces_and_linefeeds


# 超参数设置
@ex.config
def myconfig():
# Device configuration
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

# Hyper parameters
num_epochs = 5
num_classes = 10
batch_size = 100
learning_rate = 0.001


# Convolutional neural network (two convolutional layers)
class ConvNet(nn.Module):
def __init__(self, num_classes=10):
super(ConvNet, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.fc = nn.Linear(7 * 7 * 32, num_classes)

def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.reshape(out.size(0), -1)
out = self.fc(out)
return out


# notice how we can access the message here by taking it as an argument
@ex.automain
def main(device,num_epochs,num_classes,batch_size,learning_rate ):
# MNIST dataset
train_dataset = torchvision.datasets.MNIST(root='/home/ubuntu/Datasets/MINIST/',
train=True,
transform=transforms.ToTensor(),
download=True)

test_dataset = torchvision.datasets.MNIST(root='/home/ubuntu/Datasets/MINIST/',
train=False,
transform=transforms.ToTensor())

# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)

model = ConvNet(num_classes).to(device)

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.to(device)
labels = labels.to(device)

# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)

# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()

if (i + 1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))

# Test the model
model.eval() # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance)
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))

# Save the model checkpoint
torch.save(model.state_dict(), 'model.ckpt')

执行完该程序后,可以打开 omniboard 前端 http://localhost:9000

------ 本文结束 🎉🎉 谢谢观看 ------
0%