# RecomputeOptimizer¶

API属性：声明式编程(静态图)专用API

class `paddle.fluid.optimizer.` `RecomputeOptimizer` ( optimizer ) [源代码]

## 参数¶

• optimizer (Optimizer)-内部优化器

## 代码示例¶

```import paddle.fluid as fluid
import numpy as np
def gen_data():
return {"x": np.random.random(size=(32, 32)).astype('float32'),
"y": np.random.randint(2, size=(32, 1)).astype('int64')}
def mlp(input_x, input_y, hid_dim=128, label_dim=2):
print(input_x)
fc_1 = fluid.layers.fc(input=input_x, size=hid_dim)
prediction = fluid.layers.fc(input=[fc_1], size=label_dim, act='softmax')
cost = fluid.layers.cross_entropy(input=prediction, label=input_y)
sum_cost = fluid.layers.reduce_mean(cost)
return sum_cost, fc_1, prediction
input_x = fluid.layers.data(name="x", shape=[32], dtype='float32')
input_y = fluid.layers.data(name="y", shape=[1], dtype='int64')
cost, fc_1, pred = mlp(input_x, input_y)

sgd = fluid.optimizer.RecomputeOptimizer(sgd)
sgd._set_checkpoints([fc_1, pred])
sgd.minimize(cost)

print("Finished optimize")
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
step = 10

for i in range(step):
cost_val = exe.run(feed=gen_data(),
program=fluid.default_main_program(),
fetch_list=[cost.name])
print("step=%d cost=%f" % (i, cost_val[0]))
```

## 方法¶

list

```import paddle.fluid as fluid

def mlp(input_x, input_y, hid_dim=128, label_dim=2):
fc_1 = fluid.layers.fc(input=input_x, size=hid_dim)
prediction = fluid.layers.fc(input=[fc_1], size=label_dim, act='softmax')
cost = fluid.layers.cross_entropy(input=prediction, label=input_y)
sum_cost = fluid.layers.reduce_mean(cost)
return sum_cost, fc_1, prediction

input_x = fluid.layers.data(name="x", shape=[32], dtype='float32')
input_y = fluid.layers.data(name="y", shape=[1], dtype='int64')
cost, fc_1, pred = mlp(input_x, input_y)
print("Finished FF")

sgd = fluid.optimizer.RecomputeOptimizer(sgd)
cost,
startup_program=None,
parameter_list=None,

program = cost.block.program
with framework.program_guard(program, None):

```

• loss (Variable) – 用于优化过程的损失值变量
• startup_program (Program) – 用于初始化在parameter_list中参数的startup_program

list

```import paddle.fluid as fluid

def mlp(input_x, input_y, hid_dim=128, label_dim=2):
fc_1 = fluid.layers.fc(input=input_x, size=hid_dim)
prediction = fluid.layers.fc(input=[fc_1], size=label_dim, act='softmax')
cost = fluid.layers.cross_entropy(input=prediction, label=input_y)
sum_cost = fluid.layers.reduce_mean(cost)
return sum_cost, fc_1, prediction

input_x = fluid.layers.data(name="x", shape=[32], dtype='float32')
input_y = fluid.layers.data(name="y", shape=[1], dtype='int64')
cost, fc_1, pred = mlp(input_x, input_y)
print("Finished FF")

sgd = fluid.optimizer.RecomputeOptimizer(sgd)
cost,
startup_program=None,
parameter_list=None,

optimize_ops = sgd.apply_optimize(

print("Finished apply_optimize")
```

### backward(loss, startup_program=None, parameter_list=None, no_grad_set=None, callbacks=None)¶

• loss (Variable) – 需要最小化的损失值变量
• startup_program (Program, 可选) – 用于初始化parameter_list中参数的 Program , 默认值为None，此时将使用 default_startup_program
• parameter_list (list, 可选) – 待更新的Parameter或者Parameter.name组成的列表， 默认值为None，此时将更新所有的Parameter
• no_grad_set (set, 可选) – 不需要更新的Parameter或者Parameter.name组成的的集合，默认值为None
• callbacks (list, 可选) – 当为某参数附加反向算子时所要运行的callables组成的列表
• checkpoints (list, 可选) – 一批作为checkpoints的Variables

list

```import paddle.fluid as fluid

def mlp(input_x, input_y, hid_dim=128, label_dim=2):
fc_1 = fluid.layers.fc(input=input_x, size=hid_dim)
prediction = fluid.layers.fc(input=[fc_1], size=label_dim, act='softmax')
cost = fluid.layers.cross_entropy(input=prediction, label=input_y)
sum_cost = fluid.layers.reduce_mean(cost)
return sum_cost, fc_1, prediction

input_x = fluid.layers.data(name="x", shape=[32], dtype='float32')
input_y = fluid.layers.data(name="y", shape=[1], dtype='int64')
cost, fc_1, pred = mlp(input_x, input_y)
print("Finished FF")