@ -16,7 +16,7 @@ from __future__ import print_function
import unittest
import numpy as np
import sys
import paddle
import paddle . fluid . core as core
import paddle . fluid as fluid
import paddle . fluid . layers as layers
@ -64,5 +64,118 @@ class TestMseInvalidInput(unittest.TestCase):
self . assertRaises ( TypeError , test_invalid_label )
class TestNNMseLoss ( unittest . TestCase ) :
def test_NNMseLoss_mean ( self ) :
for dim in [ [ 10 , 10 ] , [ 2 , 10 , 10 ] , [ 3 , 3 , 10 , 10 ] ] :
input_np = np . random . uniform ( 0.1 , 0.5 , dim ) . astype ( " float32 " )
label_np = np . random . uniform ( 0.1 , 0.5 , dim ) . astype ( " float32 " )
prog = fluid . Program ( )
startup_prog = fluid . Program ( )
place = fluid . CUDAPlace ( 0 ) if fluid . core . is_compiled_with_cuda (
) else fluid . CPUPlace ( )
with fluid . program_guard ( prog , startup_prog ) :
input = fluid . layers . data (
name = ' input ' , shape = dim , dtype = ' float32 ' )
label = fluid . layers . data (
name = ' label ' , shape = dim , dtype = ' float32 ' )
mse_loss = paddle . nn . loss . MSELoss ( )
ret = mse_loss ( input , label )
exe = fluid . Executor ( place )
static_result = exe . run (
prog ,
feed = { " input " : input_np ,
" label " : label_np } ,
fetch_list = [ ret ] )
with fluid . dygraph . guard ( ) :
mse_loss = paddle . nn . loss . MSELoss ( )
dy_ret = mse_loss (
fluid . dygraph . to_variable ( input_np ) ,
fluid . dygraph . to_variable ( label_np ) )
dy_result = dy_ret . numpy ( )
sub = input_np - label_np
expected = np . mean ( sub * sub )
self . assertTrue ( np . allclose ( static_result , expected ) )
self . assertTrue ( np . allclose ( static_result , dy_result ) )
self . assertTrue ( np . allclose ( dy_result , expected ) )
self . assertTrue ( dy_result . shape , [ 1 ] )
def test_NNMseLoss_sum ( self ) :
for dim in [ [ 10 , 10 ] , [ 2 , 10 , 10 ] , [ 3 , 3 , 10 , 10 ] ] :
input_np = np . random . uniform ( 0.1 , 0.5 , dim ) . astype ( " float32 " )
label_np = np . random . uniform ( 0.1 , 0.5 , dim ) . astype ( " float32 " )
prog = fluid . Program ( )
startup_prog = fluid . Program ( )
place = fluid . CUDAPlace ( 0 ) if fluid . core . is_compiled_with_cuda (
) else fluid . CPUPlace ( )
with fluid . program_guard ( prog , startup_prog ) :
input = fluid . layers . data (
name = ' input ' , shape = dim , dtype = ' float32 ' )
label = fluid . layers . data (
name = ' label ' , shape = dim , dtype = ' float32 ' )
mse_loss = paddle . nn . loss . MSELoss ( reduction = ' sum ' )
ret = mse_loss ( input , label )
exe = fluid . Executor ( place )
static_result = exe . run (
prog ,
feed = { " input " : input_np ,
" label " : label_np } ,
fetch_list = [ ret ] )
with fluid . dygraph . guard ( ) :
mse_loss = paddle . nn . loss . MSELoss ( reduction = ' sum ' )
dy_ret = mse_loss (
fluid . dygraph . to_variable ( input_np ) ,
fluid . dygraph . to_variable ( label_np ) )
dy_result = dy_ret . numpy ( )
sub = input_np - label_np
expected = np . sum ( sub * sub )
self . assertTrue ( np . allclose ( static_result , expected ) )
self . assertTrue ( np . allclose ( static_result , dy_result ) )
self . assertTrue ( np . allclose ( dy_result , expected ) )
self . assertTrue ( dy_result . shape , [ 1 ] )
def test_NNMseLoss_none ( self ) :
for dim in [ [ 10 , 10 ] , [ 2 , 10 , 10 ] , [ 3 , 3 , 10 , 10 ] ] :
input_np = np . random . uniform ( 0.1 , 0.5 , dim ) . astype ( " float32 " )
label_np = np . random . uniform ( 0.1 , 0.5 , dim ) . astype ( " float32 " )
prog = fluid . Program ( )
startup_prog = fluid . Program ( )
place = fluid . CUDAPlace ( 0 ) if fluid . core . is_compiled_with_cuda (
) else fluid . CPUPlace ( )
with fluid . program_guard ( prog , startup_prog ) :
input = fluid . layers . data (
name = ' input ' , shape = dim , dtype = ' float32 ' )
label = fluid . layers . data (
name = ' label ' , shape = dim , dtype = ' float32 ' )
mse_loss = paddle . nn . loss . MSELoss ( reduction = ' none ' )
ret = mse_loss ( input , label )
exe = fluid . Executor ( place )
static_result = exe . run (
prog ,
feed = { " input " : input_np ,
" label " : label_np } ,
fetch_list = [ ret ] )
with fluid . dygraph . guard ( ) :
mse_loss = paddle . nn . loss . MSELoss ( reduction = ' none ' )
dy_ret = mse_loss (
fluid . dygraph . to_variable ( input_np ) ,
fluid . dygraph . to_variable ( label_np ) )
dy_result = dy_ret . numpy ( )
sub = input_np - label_np
expected = ( sub * sub )
self . assertTrue ( np . allclose ( static_result , expected ) )
self . assertTrue ( np . allclose ( static_result , dy_result ) )
self . assertTrue ( np . allclose ( dy_result , expected ) )
self . assertTrue ( dy_result . shape , [ 1 ] )
if __name__ == " __main__ " :
unittest . main ( )