Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix optimizer list parameters input bug #5848

Merged
merged 20 commits into from
Aug 14, 2021

Conversation

wyg1997
Copy link
Contributor

@wyg1997 wyg1997 commented Aug 12, 2021

Optimizer支持3种传参方式:

  1. Iterator: flow.optim.SGD(module.parameters(), lr=0.1)
  2. List[Dict]: flow.optim.SGD([{"params": module1.parameters()}, {"params": module2.parameters()}])
  3. List[Parameter or Tensor]: flow.optim.SGD([module.weight, module.bias])

现在在ParamGroup构造函数中统一参数为dict

@wyg1997 wyg1997 requested a review from tea321000 August 12, 2021 03:26
Copy link
Contributor

@tea321000 tea321000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我切分支测过test_graph_optimizer和课件了 可以收敛

batch_size, num_epochs, lr = 1, 100, 0.003
net, loss = linear, squared_loss
#划分数据集
dataset = flow.utils.data.TensorDataset(train_features, train_labels)
train_iter = flow.utils.data.DataLoader(dataset, batch_size, shuffle=True)
#训练模型
def fit_and_plot(lambd):
    w, b = init_params()
    train_ls, test_ls = [], []
    for _ in range(num_epochs):
        for X, y in train_iter:
            # 添加了L2范数惩罚项
            l = loss(net(X, w, b), y) + lambd * l2_penalty(w)
            l = l.sum()
            if w.grad is not None:
                w.grad.data.zeros_()
                b.grad.data.zeros_()
            l.backward()
            SGD([w, b], lr)
        train_ls.append(loss(net(train_features, w, b), train_labels).mean().numpy())
        test_ls.append(loss(net(test_features, w, b), test_labels).mean().numpy())
    Draw_Loss_Curve(range(1, num_epochs + 1), train_ls, 'epochs', 'loss',
                 range(1, num_epochs + 1), test_ls, ['train', 'test'])
    print('L2 norm of w:', w.norm().numpy())

image

@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 05:33
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 08:31
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 10:25
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 12:13
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 13:46
@github-actions
Copy link
Contributor

CI failed, removing label automerge

@oneflow-ci-bot oneflow-ci-bot removed their request for review August 12, 2021 14:59
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 19:55
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 21:49
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 23:01
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 13, 2021 00:14
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 13, 2021 01:52
@wyg1997 wyg1997 requested a review from oneflow-ci-bot August 13, 2021 04:22
@chengtbf chengtbf removed the request for review from oneflow-ci-bot August 13, 2021 04:43
@chengtbf
Copy link
Contributor

mark。暂时移除 automerge,两个小时以后加回来。

@oneflow-ci-bot oneflow-ci-bot self-requested a review August 13, 2021 04:44
@chengtbf chengtbf removed the request for review from oneflow-ci-bot August 13, 2021 04:51
@github-actions
Copy link
Contributor

Speed stats:
GPU Name: GeForce GTX 1080 

PyTorch resnet50 time: 148.1ms (= 7405.2ms / 50, input_shape=[16, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 128.1ms (= 6403.8ms / 50, input_shape=[16, 3, 224, 224], backward is enabled)
Relative speed: 1.16 (= 148.1ms / 128.1ms)

PyTorch resnet50 time: 83.5ms (= 4174.2ms / 50, input_shape=[8, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 74.5ms (= 3724.7ms / 50, input_shape=[8, 3, 224, 224], backward is enabled)
Relative speed: 1.12 (= 83.5ms / 74.5ms)

PyTorch resnet50 time: 58.7ms (= 2933.4ms / 50, input_shape=[4, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 47.7ms (= 2385.0ms / 50, input_shape=[4, 3, 224, 224], backward is enabled)
Relative speed: 1.23 (= 58.7ms / 47.7ms)

PyTorch resnet50 time: 50.0ms (= 2500.5ms / 50, input_shape=[2, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 45.2ms (= 2260.3ms / 50, input_shape=[2, 3, 224, 224], backward is enabled)
Relative speed: 1.11 (= 50.0ms / 45.2ms)

PyTorch resnet50 time: 44.4ms (= 2217.6ms / 50, input_shape=[1, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 47.2ms (= 2358.3ms / 50, input_shape=[1, 3, 224, 224], backward is enabled)
Relative speed: 0.94 (= 44.4ms / 47.2ms)

@oneflow-ci-bot oneflow-ci-bot removed their request for review August 14, 2021 03:01
@chengtbf chengtbf merged commit 34c4d25 into master Aug 14, 2021
@chengtbf chengtbf deleted the fix-optimizer_list_parameters_input branch August 14, 2021 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants