@ -16,7 +16,7 @@ from __future__ import print_function
import unittest
import numpy as np
from op_test import OpTest
from op_test import OpTest , skip_check_grad_ci
class TestElementwiseOp ( OpTest ) :
@ -25,9 +25,9 @@ class TestElementwiseOp(OpTest):
# If x and y have the same value, the max() is not differentiable.
# So we generate test data by the following method
# to avoid them being too close to each other.
x = np . random . uniform ( 0.1 , 1 , [ 13 , 17 ] ) . astype ( " float 32 " )
sgn = np . random . choice ( [ - 1 , 1 ] , [ 13 , 17 ] ) . astype ( " float 32 " )
y = x + sgn * np . random . uniform ( 0.1 , 1 , [ 13 , 17 ] ) . astype ( " float 32 " )
x = np . random . uniform ( 0.1 , 1 , [ 13 , 17 ] ) . astype ( " float 64 " )
sgn = np . random . choice ( [ - 1 , 1 ] , [ 13 , 17 ] ) . astype ( " float 64 " )
y = x + sgn * np . random . uniform ( 0.1 , 1 , [ 13 , 17 ] ) . astype ( " float 64 " )
self . inputs = { ' X ' : x , ' Y ' : y }
self . outputs = { ' Out ' : np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] ) }
@ -46,11 +46,13 @@ class TestElementwiseOp(OpTest):
[ ' X ' ] , ' Out ' , max_relative_error = 0.005 , no_grad_set = set ( ' Y ' ) )
@skip_check_grad_ci (
reason = " [skip shape check] Use y_shape(1) to test broadcast. " )
class TestElementwiseMaxOp_scalar ( TestElementwiseOp ) :
def setUp ( self ) :
self . op_type = " elementwise_max "
x = np . random . random_integers ( - 5 , 5 , [ 2 , 3 , 20 ] ) . astype ( " float 32 " )
y = np . array ( [ 0.5 ] ) . astype ( " float 32 " )
x = np . random . random_integers ( - 5 , 5 , [ 2 , 3 , 20 ] ) . astype ( " float 64 " )
y = np . array ( [ 0.5 ] ) . astype ( " float 64 " )
self . inputs = { ' X ' : x , ' Y ' : y }
self . outputs = { ' Out ' : np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] ) }
@ -58,9 +60,9 @@ class TestElementwiseMaxOp_scalar(TestElementwiseOp):
class TestElementwiseMaxOp_Vector ( TestElementwiseOp ) :
def setUp ( self ) :
self . op_type = " elementwise_max "
x = np . random . random ( ( 100 , ) ) . astype ( " float 32 " )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 100 , ) ) . astype ( " float 32 " )
y = x + sgn * np . random . uniform ( 0.1 , 1 , ( 100 , ) ) . astype ( " float 32 " )
x = np . random . random ( ( 100 , ) ) . astype ( " float 64 " )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 100 , ) ) . astype ( " float 64 " )
y = x + sgn * np . random . uniform ( 0.1 , 1 , ( 100 , ) ) . astype ( " float 64 " )
self . inputs = { ' X ' : x , ' Y ' : y }
self . outputs = { ' Out ' : np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] ) }
@ -68,73 +70,73 @@ class TestElementwiseMaxOp_Vector(TestElementwiseOp):
class TestElementwiseMaxOp_broadcast_0 ( TestElementwiseOp ) :
def setUp ( self ) :
self . op_type = " elementwise_max "
x = np . random . uniform ( 0.5 , 1 , ( 2, 3 , 20 ) ) . astype ( np . float32 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 2, ) ) . astype ( np . float32 )
x = np . random . uniform ( 0.5 , 1 , ( 100, 5 , 2 ) ) . astype ( np . float64 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 100, ) ) . astype ( np . float64 )
y = x [ : , 0 , 0 ] + sgn * \
np . random . uniform ( 1 , 2 , ( 2, ) ) . astype ( np . float32 )
np . random . uniform ( 1 , 2 , ( 100, ) ) . astype ( np . float64 )
self . inputs = { ' X ' : x , ' Y ' : y }
self . attrs = { ' axis ' : 0 }
self . outputs = {
' Out ' :
np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] . reshape ( 2 , 1 , 1 ) )
np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] . reshape ( 100 , 1 , 1 ) )
}
class TestElementwiseMaxOp_broadcast_1 ( TestElementwiseOp ) :
def setUp ( self ) :
self . op_type = " elementwise_max "
x = np . random . uniform ( 0.5 , 1 , ( 2 , 3, 20 ) ) . astype ( np . float32 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 3, ) ) . astype ( np . float32 )
x = np . random . uniform ( 0.5 , 1 , ( 2 , 100, 3 ) ) . astype ( np . float64 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 100, ) ) . astype ( np . float64 )
y = x [ 0 , : , 0 ] + sgn * \
np . random . uniform ( 1 , 2 , ( 3, ) ) . astype ( np . float32 )
np . random . uniform ( 1 , 2 , ( 100, ) ) . astype ( np . float64 )
self . inputs = { ' X ' : x , ' Y ' : y }
self . attrs = { ' axis ' : 1 }
self . outputs = {
' Out ' :
np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] . reshape ( 1 , 3 , 1 ) )
np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] . reshape ( 1 , 100 , 1 ) )
}
class TestElementwiseMaxOp_broadcast_2 ( TestElementwiseOp ) :
def setUp ( self ) :
self . op_type = " elementwise_max "
x = np . random . uniform ( 0.5 , 1 , ( 1 0, 3 , 4 ) ) . astype ( np . float32 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 4, ) ) . astype ( np . float32 )
x = np . random . uniform ( 0.5 , 1 , ( 1 , 3 , 100 ) ) . astype ( np . float64 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 100, ) ) . astype ( np . float64 )
y = x [ 0 , 0 , : ] + sgn * \
np . random . uniform ( 1 , 2 , ( 4, ) ) . astype ( np . float32 )
np . random . uniform ( 1 , 2 , ( 100, ) ) . astype ( np . float64 )
self . inputs = { ' X ' : x , ' Y ' : y }
self . outputs = {
' Out ' :
np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] . reshape ( 1 , 1 , 4 ) )
np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] . reshape ( 1 , 1 , 100 ) )
}
class TestElementwiseMaxOp_broadcast_3 ( TestElementwiseOp ) :
def setUp ( self ) :
self . op_type = " elementwise_max "
x = np . random . uniform ( 0.5 , 1 , ( 2 , 3, 4 , 5 ) ) . astype ( np . float32 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 3, 4 ) ) . astype ( np . float32 )
x = np . random . uniform ( 0.5 , 1 , ( 2 , 50, 2 , 1 ) ) . astype ( np . float64 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 50, 2 ) ) . astype ( np . float64 )
y = x [ 0 , : , : , 0 ] + sgn * \
np . random . uniform ( 1 , 2 , ( 3, 4 ) ) . astype ( np . float32 )
np . random . uniform ( 1 , 2 , ( 50, 2 ) ) . astype ( np . float64 )
self . inputs = { ' X ' : x , ' Y ' : y }
self . attrs = { ' axis ' : 1 }
self . outputs = {
' Out ' :
np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] . reshape ( 1 , 3, 4 , 1 ) )
np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] . reshape ( 1 , 50, 2 , 1 ) )
}
class TestElementwiseMaxOp_broadcast_4 ( TestElementwiseOp ) :
def setUp ( self ) :
self . op_type = " elementwise_max "
x = np . random . uniform ( 0.5 , 1 , ( 2 , 3 , 4 , 5 ) ) . astype ( np . float 32 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 2 , 3 , 1 , 5 ) ) . astype ( np . float 32 )
x = np . random . uniform ( 0.5 , 1 , ( 2 , 3 , 4 , 5 ) ) . astype ( np . float 64 )
sgn = np . random . choice ( [ - 1 , 1 ] , ( 2 , 3 , 1 , 5 ) ) . astype ( np . float 64 )
y = x + sgn * \
np . random . uniform ( 1 , 2 , ( 2 , 3 , 1 , 5 ) ) . astype ( np . float 32 )
np . random . uniform ( 1 , 2 , ( 2 , 3 , 1 , 5 ) ) . astype ( np . float 64 )
self . inputs = { ' X ' : x , ' Y ' : y }
self . outputs = { ' Out ' : np . maximum ( self . inputs [ ' X ' ] , self . inputs [ ' Y ' ] ) }