square_error_cost

paddle.fluid.layers.square_error_cost(input, label)[source]

This op accepts input predictions and target label and returns the squared error cost.

For predictions label, and target label, the equation is:

\[Out = (input - label)^2\]
Parameters
  • input (Variable) – Input tensor, the data type should be float32.

  • label (Variable) – Label tensor, the data type should be float32.

Returns

The tensor variable storing the element-wise squared error difference between input and label.

Return type: Variable.

Examples

# declarative mode
import paddle.fluid as fluid
import numpy as np
input = fluid.data(name="input", shape=[1])
label = fluid.data(name="label", shape=[1])
output = fluid.layers.square_error_cost(input,label)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

input_data = np.array([1.5]).astype("float32")
label_data = np.array([1.7]).astype("float32")
output_data = exe.run(fluid.default_main_program(),
    feed={"input":input_data, "label":label_data},
    fetch_list=[output],
    return_numpy=True)

print(output_data)
# [array([0.04000002], dtype=float32)]

# imperative mode
import paddle.fluid.dygraph as dg

with dg.guard(place) as g:
    input = dg.to_variable(input_data)
    label = dg.to_variable(label_data)
    output = fluid.layers.square_error_cost(input, label)
    print(output.numpy())

    # [0.04000002]