Paddle LookAhead优化器动态图下使用报错

cdmah0mi  于 2022-10-20  发布在  其他
关注(0)|答案(8)|浏览(142)

Paddle:1.6
GPU:V100
AIStudio线上环境
训练信息:
单机单卡
复现信息:
sgd = fluid.optimizer.AdamOptimizer(learning_rate=0.01)
optimizer = fluid.optimizer.LookaheadOptimizer(sgd,alpha=0.5,k=5)
替换房价预测案例optimizer
报错信息:
epoch: 0, batch: 0, loss is: [2.3028119], acc is [0.03125]
---------------------------------------------------------------------------TypeError Traceback (most recent call last) in
5 # optimizer=fluid.optimizer.AdamOptimizer(learning_rate=0.01)
6 pretrained_path = None
----> 7 best_valid_acc = train_model(EPOCH_NUM, optimizer, pretrained_path)
in train_model(EPOCH_NUM, optimizer, pretrained_path, use_gpu)
33 #后向传播,更新参数的过程
34 avg_loss.backward()
---> 35 optimizer.minimize(avg_loss)
36 model.clear_gradients()
37
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/optimizer.py in minimize(self, loss, startup_program)
3716 slow_var, layers.elementwise_sub(one_var, alpha)))
3717 layers.assign(input=tmp_var, output=slow_var)
-> 3718 layers.assign(input=tmp_var, output=fast_var)
3719 with switch.default():
3720 pass
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/control_flow.py inexit(self, exc_type, exc_val, exc_tb)
1588
1589 defexit(self, exc_type, exc_val, exc_tb):
-> 1590 self.block.complete()
1591 return super(ConditionalBlockGuard, self).exit(exc_type, exc_val,
1592 exc_tb)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/control_flow.py in complete(self)
1673 attrs={
1674 'sub_block': inside_block,
-> 1675 'is_scalar_condition': self.is_scalar_condition
1676 })
1677
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/framework.py in append_op(self, *args,**kwargs)
2415 kwargs.get("outputs", {}), attrs
2416 if attrs else {},
-> 2417 kwargs.get("stop_gradient", False))
2418 else:
2419 op_desc = self.desc.append_op()
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/tracer.py in trace_op(self, type, inputs, outputs, attrs, stop_gradient)
45 self.trace(type, inputs, outputs, attrs,
46 framework._current_expected_place(), self._train_mode and
---> 47 not stop_gradient)
48
49 def train_mode(self):
TypeError: trace(): incompatible function arguments. The following argument types are supported:

  1. (self: paddle.fluid.core_avx.Tracer, arg0: str, arg1: Dict[str, handle], arg2: Dict[str, handle], arg3: Dict[str, Variant], arg4: paddle::platform::CUDAPlace, arg5: bool) -> None
  2. (self: paddle.fluid.core_avx.Tracer, arg0: str, arg1: Dict[str, handle], arg2: Dict[str, handle], arg3: Dict[str, Variant], arg4: paddle::platform::CPUPlace, arg5: bool) -> None

Invoked with: <paddle.fluid.dygraph.tracer.Tracer object at 0x7efb5dc94b90>, 'conditional_block', {'Cond': [name tmp_46, dtype: VarType.INT32 shape: [1] lod: {}
dim: 1
layout: NCHW
dtype: bool
data: [0]
], 'Input': []}, {'Out': [], 'Scope': [name _generated_var_2, shape: [0], not inited]}, {'sub_block': idx: 1
parent_idx: 0
, 'is_scalar_condition': True}, <paddle.fluid.core_avx.CUDAPlace object at 0x7efb5de13430>, True
[10]

yvfmudvl

yvfmudvl1#

问题已复现,处理中

qrjkbowd

qrjkbowd2#

@LDOUBLEV 可以提供一下复现代码,我们一起看下怎么修复

gpfsuwkq

gpfsuwkq3#

@LDOUBLEV 可以提供一下复现代码,我们一起看下怎么修复

复现代码:

import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import FC
import numpy as np

trainset = paddle.dataset.mnist.train()
train_reader = paddle.batch(trainset, batch_size=8)

class MNIST(fluid.dygraph.Layer):
    def __init__(self, name_scope):
        super(MNIST, self).__init__(name_scope)
        name_scope = self.full_name()
        self.fc = FC(name_scope, size=1, act=None)
    def forward(self, inputs):
        outputs = self.fc(inputs)
        return outputs

with fluid.dygraph.guard(fluid.CUDAPlace(0)):
    model = MNIST("mnist")
    model.train()
    train_loader = paddle.batch(paddle.dataset.mnist.train(), batch_size=16)
    sgd = fluid.optimizer.SGDOptimizer(learning_rate=0.001)
    optimizer = fluid.optimizer.LookaheadOptimizer(sgd,alpha=0.5,k=32)

    EPOCH_NUM = 10
    for epoch_id in range(EPOCH_NUM):
        for batch_id, data in enumerate(train_loader()):
            image_data = np.array([x[0] for x in data]).astype('float32')
            label_data = np.array([x[1] for x in data]).astype('float32').reshape(-1, 1)
            image = fluid.dygraph.to_variable(image_data)
            label = fluid.dygraph.to_variable(label_data)

            predict = model(image)
            loss = fluid.layers.square_error_cost(predict, label)
            avg_loss = fluid.layers.mean(loss)
            if batch_id  % 1 == 0:
                print("epoch: {}, batch: {}, loss is: {}".format(epoch_id, batch_id, avg_loss.numpy()))

            avg_loss.backward()
            optimizer.minimize(avg_loss)
            model.clear_gradients()
nkkqxpd9

nkkqxpd94#

@LDOUBLEV 我把Lookahead相关的代码注解掉了,问题仍然存在,暂时和LookaheadOptimizer没有关系,麻烦你看下其他原因。

如下是注解掉Lookahead后的代码:

import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import FC
import numpy as np

trainset = paddle.dataset.mnist.train()
train_reader = paddle.batch(trainset, batch_size=8)

class MNIST(fluid.dygraph.Layer):
    def __init__(self, name_scope):
        super(MNIST, self).__init__(name_scope)
        name_scope = self.full_name()
        self.fc = FC(name_scope, size=1, act=None)
    def forward(self, inputs):
        outputs = self.fc(inputs)
        return outputs

with fluid.dygraph.guard(fluid.CUDAPlace(0)):
    model = MNIST("mnist")
    model.train()
    train_loader = paddle.batch(paddle.dataset.mnist.train(), batch_size=16)
    sgd = fluid.optimizer.SGDOptimizer(learning_rate=0.001)
    #optimizer = fluid.optimizer.LookaheadOptimizer(sgd,alpha=0.5,k=32)
    optimizer = sgd

    EPOCH_NUM = 10
    for epoch_id in range(EPOCH_NUM):
        for batch_id, data in enumerate(train_loader()):
            image_data = np.array([x[0] for x in data]).astype('float32')
            label_data = np.array([x[1] for x in data]).astype('float32').reshape(-1, 1)
            image = fluid.dygraph.to_variable(image_data)
            label = fluid.dygraph.to_variable(label_data)

            predict = model(image)
            loss = fluid.layers.square_error_cost(predict, label)
            avg_loss = fluid.layers.mean(loss)
            if batch_id  % 1 == 0:
                print("epoch: {}, batch: {}, loss is: {}".format(epoch_id, batch_id, avg_loss.numpy()))

            avg_loss.backward()
            optimizer.minimize(avg_loss)
            model.clear_gradients()

以下为错误:

File "demo.py", line 22, in <module>
    sgd = fluid.optimizer.SGDOptimizer(learning_rate=0.001)
  File "python2.7/site-packages/paddle/fluid/optimizer.py", line 763, in __init__
    name=name)
  File "<python2.7/site-packages/decorator.pyc:decorator-gen-44>", line 2, in __init__
  File "python2.7/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in __impl__
    return wrapped_func(*args,**kwargs)
  File "python2.7/site-packages/paddle/fluid/dygraph/base.py", line 100, in __impl__
    return func(*args,**kwargs)
  File "python2.7/site-packages/paddle/fluid/optimizer.py", line 83, in __init__
    "parameter_list argument given to the Optimizer should not be None in dygraph mode."
AttributeError: parameter_list argument given to the Optimizer should not be None in dygraph mode.
pxiryf3j

pxiryf3j5#

@LDOUBLEV 我把Lookahead相关的代码注解掉了,问题仍然存在,暂时和LookaheadOptimizer没有关系,麻烦你看下其他原因。

如下是注解掉Lookahead后的代码:

import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import FC
import numpy as np

trainset = paddle.dataset.mnist.train()
train_reader = paddle.batch(trainset, batch_size=8)

class MNIST(fluid.dygraph.Layer):
    def __init__(self, name_scope):
        super(MNIST, self).__init__(name_scope)
        name_scope = self.full_name()
        self.fc = FC(name_scope, size=1, act=None)
    def forward(self, inputs):
        outputs = self.fc(inputs)
        return outputs

with fluid.dygraph.guard(fluid.CUDAPlace(0)):
    model = MNIST("mnist")
    model.train()
    train_loader = paddle.batch(paddle.dataset.mnist.train(), batch_size=16)
    sgd = fluid.optimizer.SGDOptimizer(learning_rate=0.001)
    #optimizer = fluid.optimizer.LookaheadOptimizer(sgd,alpha=0.5,k=32)
    optimizer = sgd

    EPOCH_NUM = 10
    for epoch_id in range(EPOCH_NUM):
        for batch_id, data in enumerate(train_loader()):
            image_data = np.array([x[0] for x in data]).astype('float32')
            label_data = np.array([x[1] for x in data]).astype('float32').reshape(-1, 1)
            image = fluid.dygraph.to_variable(image_data)
            label = fluid.dygraph.to_variable(label_data)

            predict = model(image)
            loss = fluid.layers.square_error_cost(predict, label)
            avg_loss = fluid.layers.mean(loss)
            if batch_id  % 1 == 0:
                print("epoch: {}, batch: {}, loss is: {}".format(epoch_id, batch_id, avg_loss.numpy()))

            avg_loss.backward()
            optimizer.minimize(avg_loss)
            model.clear_gradients()

以下为错误:

File "demo.py", line 22, in <module>
    sgd = fluid.optimizer.SGDOptimizer(learning_rate=0.001)
  File "python2.7/site-packages/paddle/fluid/optimizer.py", line 763, in __init__
    name=name)
  File "<python2.7/site-packages/decorator.pyc:decorator-gen-44>", line 2, in __init__
  File "python2.7/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in __impl__
    return wrapped_func(*args,**kwargs)
  File "python2.7/site-packages/paddle/fluid/dygraph/base.py", line 100, in __impl__
    return func(*args,**kwargs)
  File "python2.7/site-packages/paddle/fluid/optimizer.py", line 83, in __init__
    "parameter_list argument given to the Optimizer should not be None in dygraph mode."
AttributeError: parameter_list argument given to the Optimizer should not be None in dygraph mode.

用户的环境是1.6,你用的应该是基于develop分支下的,最新develop分支下的动态图,optimizer需要指定要优化的参数,你看报错信息里是parameter_list为空。目前关于lookaheadoptimizer的最新结论是:动态图当前不支持lookaheadoptimizer。

smdncfj3

smdncfj36#

@GitLD Hi,lookaheadoptimizer在动态图下暂时不可用,建议换其他optimizer进行优化算法,我们会尽快支持。

uqzxnwby

uqzxnwby7#

@LDOUBLEV 我把Lookahead相关的代码注解掉了,问题仍然存在,暂时和LookaheadOptimizer没有关系,麻烦你看下其他原因。
如下是注解掉Lookahead后的代码:

import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import FC
import numpy as np

trainset = paddle.dataset.mnist.train()
train_reader = paddle.batch(trainset, batch_size=8)

class MNIST(fluid.dygraph.Layer):
    def __init__(self, name_scope):
        super(MNIST, self).__init__(name_scope)
        name_scope = self.full_name()
        self.fc = FC(name_scope, size=1, act=None)
    def forward(self, inputs):
        outputs = self.fc(inputs)
        return outputs

with fluid.dygraph.guard(fluid.CUDAPlace(0)):
    model = MNIST("mnist")
    model.train()
    train_loader = paddle.batch(paddle.dataset.mnist.train(), batch_size=16)
    sgd = fluid.optimizer.SGDOptimizer(learning_rate=0.001)
    #optimizer = fluid.optimizer.LookaheadOptimizer(sgd,alpha=0.5,k=32)
    optimizer = sgd

    EPOCH_NUM = 10
    for epoch_id in range(EPOCH_NUM):
        for batch_id, data in enumerate(train_loader()):
            image_data = np.array([x[0] for x in data]).astype('float32')
            label_data = np.array([x[1] for x in data]).astype('float32').reshape(-1, 1)
            image = fluid.dygraph.to_variable(image_data)
            label = fluid.dygraph.to_variable(label_data)

            predict = model(image)
            loss = fluid.layers.square_error_cost(predict, label)
            avg_loss = fluid.layers.mean(loss)
            if batch_id  % 1 == 0:
                print("epoch: {}, batch: {}, loss is: {}".format(epoch_id, batch_id, avg_loss.numpy()))

            avg_loss.backward()
            optimizer.minimize(avg_loss)
            model.clear_gradients()

以下为错误:

File "demo.py", line 22, in <module>
    sgd = fluid.optimizer.SGDOptimizer(learning_rate=0.001)
  File "python2.7/site-packages/paddle/fluid/optimizer.py", line 763, in __init__
    name=name)
  File "<python2.7/site-packages/decorator.pyc:decorator-gen-44>", line 2, in __init__
  File "python2.7/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in __impl__
    return wrapped_func(*args,**kwargs)
  File "python2.7/site-packages/paddle/fluid/dygraph/base.py", line 100, in __impl__
    return func(*args,**kwargs)
  File "python2.7/site-packages/paddle/fluid/optimizer.py", line 83, in __init__
    "parameter_list argument given to the Optimizer should not be None in dygraph mode."
AttributeError: parameter_list argument given to the Optimizer should not be None in dygraph mode.

用户的环境是1.6,你用的应该是基于develop分支下的,最新develop分支下的动态图,optimizer需要指定要优化的参数,你看报错信息里是parameter_list为空。目前关于lookaheadoptimizer的最新结论是:动态图当前不支持lookaheadoptimizer。

好的,我试一下用1.6复现用户错误。

6tqwzwtp

6tqwzwtp8#

@GitLD Hi,lookaheadoptimizer在动态图下暂时不可用,建议换其他optimizer进行优化算法,我们会尽快支持。

好的,期待不久后能支持上

相关问题