|
| 1 | +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from .framework import Program, program_guard, unique_name, default_startup_program |
| 16 | +from .param_attr import ParamAttr |
| 17 | +from .initializer import Constant |
| 18 | +from . import layers |
| 19 | +from . import backward |
| 20 | +from .imperative import Layer, nn |
| 21 | +from . import executor |
| 22 | + |
| 23 | +from . import core |
| 24 | +import numpy as np |
| 25 | + |
| 26 | +__all__ = ['run_check'] |
| 27 | + |
| 28 | + |
| 29 | +class SimpleLayer(Layer): |
| 30 | + def __init__(self, name_scope): |
| 31 | + super(SimpleLayer, self).__init__(name_scope) |
| 32 | + self._fc1 = nn.FC(self.full_name(), |
| 33 | + 3, |
| 34 | + ParamAttr(initializer=Constant(value=0.1))) |
| 35 | + |
| 36 | + def forward(self, inputs): |
| 37 | + x = self._fc1(inputs) |
| 38 | + x = layers.reduce_sum(x) |
| 39 | + return x |
| 40 | + |
| 41 | + |
| 42 | +def run_check(): |
| 43 | + ''' intall check to verify if install is success |
| 44 | +
|
| 45 | + This func should not be called only if you need to verify installation |
| 46 | + ''' |
| 47 | + print("Running Verify Fluid Program ... ") |
| 48 | + prog = Program() |
| 49 | + startup_prog = Program() |
| 50 | + scope = core.Scope() |
| 51 | + with executor.scope_guard(scope): |
| 52 | + with program_guard(prog, startup_prog): |
| 53 | + with unique_name.guard(): |
| 54 | + np_inp = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32) |
| 55 | + inp = layers.data( |
| 56 | + name="inp", shape=[2, 2], append_batch_size=False) |
| 57 | + simple_layer = SimpleLayer("simple_layer") |
| 58 | + out = simple_layer(inp) |
| 59 | + param_grads = backward.append_backward( |
| 60 | + out, parameter_list=[simple_layer._fc1._w.name])[0] |
| 61 | + exe = executor.Executor(core.CPUPlace( |
| 62 | + ) if not core.is_compiled_with_cuda() else core.CUDAPlace(0)) |
| 63 | + exe.run(default_startup_program()) |
| 64 | + exe.run(feed={inp.name: np_inp}, |
| 65 | + fetch_list=[out.name, param_grads[1].name]) |
| 66 | + |
| 67 | + print( |
| 68 | + "Your Paddle Fluid is installed successfully! Let's start deep Learning with Paddle Fluid now" |
| 69 | + ) |
0 commit comments