nn.py 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Y
Yu Yang 已提交
14
"""
15
All layers just related to the neural network.
Y
Yu Yang 已提交
16
"""
P
peizhilin 已提交
17
import os
S
sneaxiy 已提交
18
import inspect
19 20 21 22 23
import warnings

import numpy as np

import paddle
Y
Yu Yang 已提交
24
from ..layer_helper import LayerHelper
25 26 27 28 29 30
from ..framework import (
    Variable,
    OpProtoHolder,
    dygraph_only,
    _dygraph_tracer,
    default_main_program,
31
    _create_tensor,
32 33 34 35
    static_only,
    _global_flags,
    in_dygraph_mode,
)
36
from ..framework import _current_expected_place
37
from .. import dygraph_utils
Y
yangyaming 已提交
38
from ..param_attr import ParamAttr
39 40 41 42 43
from .layer_function_generator import (
    autodoc,
    templatedoc,
    _generate_doc_string_,
)
44

F
fengjiayi 已提交
45
from .. import unique_name
46
from .. import core
47
from ...utils import deprecated
48 49 50 51 52 53
from ..data_feeder import (
    convert_dtype,
    check_variable_and_dtype,
    check_type,
    check_dtype,
)
54
from paddle.utils import deprecated
55
from paddle import _C_ops, _legacy_C_ops
56 57
from collections.abc import Iterable

Y
Yu Yang 已提交
58 59

__all__ = [
X
Xin Pan 已提交
60
    'autoincreased_step_counter',
Y
Yu Yang 已提交
61 62
]

63

Y
Yu Yang 已提交
64
def autoincreased_step_counter(counter_name=None, begin=1, step=1):
Y
Yu Yang 已提交
65
    """
66 67
    :api_attr: Static Graph

68 69
    Create an auto-increase variable. which will be automatically increased
    by 1 in every iteration. By default, the first return of this counter is 1,
Y
Yibing Liu 已提交
70
    and the step size is 1.
Y
Yu Yang 已提交
71 72

    Args:
Y
Yibing Liu 已提交
73 74 75
        counter_name(str, optional): The counter name. Default '@STEP_COUNTER@'.
        begin(int, optional): The first return value of this counter. Default 1.
        step(int, optional): The step size. Default 1.
Y
Yu Yang 已提交
76

77
    Returns:
Y
Yibing Liu 已提交
78
        Variable: The auto-increased Variable with data type int64.
Y
yi.wu 已提交
79 80 81 82

    Examples:
        .. code-block:: python

83
           import paddle.fluid as fluid
84 85
           import paddle
           paddle.enable_static()
Y
yi.wu 已提交
86
           global_step = fluid.layers.autoincreased_step_counter(
Y
Yibing Liu 已提交
87
               counter_name='@LR_DECAY_COUNTER@', begin=0, step=1)
Y
Yu Yang 已提交
88 89
    """
    helper = LayerHelper('global_step_counter')
Y
Yu Yang 已提交
90 91
    if counter_name is None:
        counter_name = '@STEP_COUNTER@'
Y
Yu Yang 已提交
92
    counter, is_new_var = helper.create_or_get_global_variable(
H
hong 已提交
93 94 95 96
        name=counter_name,
        dtype='int64',
        shape=[1],
        persistable=True,
97 98
        belong_to_optimizer=True,
    )
Y
Yu Yang 已提交
99
    if is_new_var:
100
        helper.set_variable_initializer(
101 102 103 104
            counter,
            initializer=paddle.nn.initializer.ConstantInitializer(
                value=begin - 1, force_cpu=True
            ),
105
        )
W
Wu Yi 已提交
106
        helper.main_program.global_block()._prepend_op(
Y
Yu Yang 已提交
107 108
            type='increment',
            inputs={'X': [counter]},
Y
Yu Yang 已提交
109
            outputs={'Out': [counter]},
110 111
            attrs={'step': float(step)},
        )
Y
Yu Yang 已提交
112 113 114
        counter.stop_gradient = True

    return counter