def test_channels_batch(self): inpl = theano_var([[ [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [3, 0, 0]] ], [ [[0, 3, 3], [4, 5, 6], [7, 8, 4]], [[-3, -3, -3], [-3, -3, -3], [3, 3, 3]] ]]) inpu = theano_var([[ [[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1], [4, 1, 1]] ], [ [[2, 4, 4], [9, 9, 9], [9, 9, 9]], [[2, 2, 2], [2, 2, 2], [5, 5, 5]] ]]) iinp = TheanoInterval(inpl, inpu) doutl = theano_var([[ [[-1, -2], [-3, -4]], [[1, 2], [-3, -2]] ], [ [[1, 2], [-3, -2]], [[-1, 1], [-1, 1]] ]]) doutu = theano_var([[ [[5, 4], [3, 2]], [[4, 4], [4, 4]], ], [ [[4, 5], [0, 1]], [[0, 2], [0, 2]] ]]) idout = TheanoInterval(doutl, doutu) shp = (2, 2, 3, 3) din = d_pool(idout, iinp, shp, poolsize=(2, 2), mode='avg') l, u = din.eval() array_almost_equal(l, A([[ [[-1, -3, -2], [-4, -10, -6], [-3, -7, -4]], [[1, 3, 2], [-2, -2, 0], [-3, -5, -2]] ], [ [[1, 3, 2], [-2, -2, 0], [-3, -5, -2]], [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] ]]) / 4.0) array_almost_equal(u, A([[ [[5, 9, 4], [8, 14, 6], [3, 5, 2]], [[4, 8, 4], [8, 16, 8], [4, 8, 4]] ], [ [[4, 9, 5], [4, 10, 6], [0, 1, 1]], [[0, 2, 2], [0, 4, 4], [0, 2, 2]] ]]) / 4.0)
def test_interval_negative(self): actl = theano_var([-2, -1, -1, 0, 0, 1]) actu = theano_var([-1, 1, 0, 0, 1, 2]) doutl = theano_var([-3, -5, -7, -11, -13, -17]) doutu = theano_var([-2, -3, -5, -7, -11, -13]) iact = TheanoInterval(actl, actu) idout = TheanoInterval(doutl, doutu) idin = d_relu(idout, iact) l, u = idin.eval() rl = A([0, -5, -7, -11, -13, -17]) ru = A([0, 0, 0, 0, 0, -13]) array_almost_equal(l, rl) array_almost_equal(u, ru)
def test_interval(self): actl = theano_var([-2, -1, -1, 0, 0, 1]) actu = theano_var([-1, 1, 0, 0, 1, 2]) doutl = theano_var([2, 3, 4, 7, 11, 13]) doutu = theano_var([3, 5, 7, 11, 13, 17]) iact = TheanoInterval(actl, actu) idout = TheanoInterval(doutl, doutu) idin = d_relu(idout, iact) l, u = idin.eval() rl = A([0, 0, 0, 0, 0, 13]) ru = A([0, 5, 7, 11, 13, 17]) array_almost_equal(l, rl) array_almost_equal(u, ru)
def test_simple(self): inpl = theano_var([[[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]]) inpu = theano_var([[[[1, 1, 1], [1, 1, 1], [1, 1, 1]]]]) iinp = TheanoInterval(inpl, inpu) doutl = theano_var([[[[-1, -2], [-3, -4]]]]) doutu = theano_var([[[[5, 4], [3, 2]]]]) idout = TheanoInterval(doutl, doutu) shp = (1, 1, 3, 3) din = d_pool(idout, iinp, shp, poolsize=(2, 2), mode='avg') l, u = din.eval() array_almost_equal(l, A([[[[-1, -3, -2], [-4, -10, -6], [-3, -7, -4]]]]) / 4.0) array_almost_equal(u, A([[[[5, 9, 4], [8, 14, 6], [3, 5, 2]]]]) / 4.0)
def test_padding(self): inpl = theano_var([[[[0, 0, 0], [0, 0, 0], [0, 0, 0]]]]) inpu = theano_var([[[[1, 1, 1], [1, 1, 1], [1, 1, 1]]]]) iinp = TheanoInterval(inpl, inpu) doutl = theano_var([[[[-1, -2], [-3, -4]]]]) doutu = theano_var([[[[5, 4], [3, 2]]]]) idout = TheanoInterval(doutl, doutu) shp = (1, 1, 3, 3) din = d_pool(idout, iinp, shp, poolsize=(2, 2), padding=(1, 1), stride=(3, 3), mode='max') l, u = din.eval() array_almost_equal(l, A([[[[-1, 0, -2], [0, 0, 0], [-3, 0, -4]]]])) array_almost_equal(u, A([[[[5, 0, 4], [0, 0, 0], [3, 0, 2]]]]))
def test_softmax(self): dout = TheanoInterval.derest_output(1000) start_time = timeit.default_timer() din = d_softmax(dout) constr_time = timeit.default_timer() l, u = din.eval() ex_time = timeit.default_timer() self.check_time('d_softmax', start_time, constr_time, ex_time)
def test_2D_batches(self): dout = theano_var([[3, 6], [1, 2]]) idout = TheanoInterval(dout, dout) w = theano_var([[9, 15], [12, 18]]) shp = (2, 2) din = d_fully_connected(idout, w, shp) l, u = din.eval() array_almost_equal(l, u) array_almost_equal(l, A([[117, 144], [39, 48]]))
def test_2D_simple_used_1D_of_weights(self): dout = theano_var([[3, 6]]) idout = TheanoInterval(dout, dout) w = theano_var([9, 12]) shp = (1, 1) din = d_fully_connected(idout, w, shp) l, u = din.eval() array_almost_equal(l, u) array_almost_equal(l, A([[99]]))
def test_2D_simple_used_2D_of_weights(self): dout = theano_var([[3, 0]]) idout = TheanoInterval(dout, dout) w = theano_var([[6, 0], [9, 0]]) shp = (1, 2) din = d_fully_connected(idout, w, shp) l, u = din.eval() array_almost_equal(l, u) array_almost_equal(l, A([[18, 27]]))
def test_1D_simple(self): dout = theano_var([[1]]) idout = TheanoInterval(dout, dout) w = theano_var([[2]]) shp = (1, 1) din = d_fully_connected(idout, w, shp) l, u = din.eval() array_almost_equal(l, u) array_almost_equal(l, A([[2]]))
def test_interval_3x3n(self): doutl = theano_var([[-3, 0, 3], [3, -3, -5], [-3, -2, 1]]) doutu = theano_var([[-3, 2, 3], [5, 3, 2], [-1, 3, 3]]) idout = TheanoInterval(doutl, doutu) idin = d_dropout(idout, 0.8) l, u = idin.eval() rl = A([[-0.6, 0, 0.6], [0.6, -0.6, -1], [-0.6, -0.4, 0.2]]) ru = A([[-0.6, 0.4, 0.6], [1, 0.6, 0.4], [-0.2, 0.6, 0.6]]) array_almost_equal(l, rl) array_almost_equal(u, ru)
def test_neg_output(self): inpl = theano_var([[[[1, 1], [1, 1]]]]) inpu = theano_var([[[[2, 2], [2, 2]]]]) iinp = TheanoInterval(inpl, inpu) idout = theano_interval([[[[-3]]]]) shp = (1, 1, 2, 2) din = d_pool(idout, iinp, shp, poolsize=(2, 2), mode='max') l, u = din.eval() array_almost_equal(l, A([[[[-3, -3], [-3, -3]]]])) array_almost_equal(u, A([[[[0, 0], [0, 0]]]]))
def test_4x3x2(self): shp = (4, 3, 2) act = np.zeros(shp) ind_n_in, ind_h, ind_w = np.indices(shp) act = 100 * ind_n_in + 10 * ind_h + ind_w thact = theano.shared(act) iact = TheanoInterval(thact, thact) thdout = theano.shared(np.ones(shp)) idout = TheanoInterval(thdout, thdout) idin = d_relu(idout, iact) l, u = idin.eval() assert_almost_equal(l[0, 0, 0], 0.0) assert_almost_equal(u[0, 0, 0], 1.0) assert_almost_equal(l[2, 1, 1], 1.0) assert_almost_equal(l[2, 2, 1], 1.0) assert_almost_equal(l[1, 0, 1], 1.0) assert_almost_equal(l[2, 1, 1], 1.0) assert_almost_equal(l[2, 2, 0], 1.0) assert_almost_equal(l[1, 0, 1], 1.0)
def test_2D_Intervals(self): doutl = theano_var([[-3, -6, 3]]) doutu = theano_var([[9, -3, 6]]) idout = TheanoInterval(doutl, doutu) w = theano_var([[2, -3, -3], [-3, 1, 2], [5, -4, 3], [-2, -3, -4]]) shp = (1, 2, 2) din = d_fully_connected(idout, w, shp) l, u = din.eval() array_almost_equal(l, A([[[-15, -27], [6, -33]]])) array_almost_equal(u, A([[[27, 18], [87, 12]]]))
def _get_theano_interval_result(self, lower, upper, function, *args, **kwargs): dim = len(lower.shape) if dim == 1: t_lower, t_upper = T.dvectors('inpl', 'inpu') elif dim == 3: t_lower, t_upper = T.tensor3s('inpl', 'inpu') else: raise NotImplementedError iinp = TheanoInterval(t_lower, t_upper) res = function(iinp, *args, **kwargs) d = {t_lower: lower, t_upper: upper} return res.eval(d)
def test_stride(self): tinpl = theano.shared(np.arange(25).reshape((1, 1, 5, 5))) tinpu = theano.shared(np.arange(25).reshape((1, 1, 5, 5)) + 2) iinp = TheanoInterval(tinpl, tinpu) idout = theano_interval([[[[-1, 2], [-3, 4]]]]) shp = (1, 1, 5, 5) din = d_pool(idout, iinp, shp, poolsize=(2, 2), stride=(3, 3), mode='avg') l, u = din.eval() array_almost_equal(l, A([[[[-1, -1, 0, 2, 2], [-1, -1, 0, 2, 2], [0, 0, 0, 0, 0], [-3, -3, 0, 4, 4], [-3, -3, 0, 4, 4]]]]) / 4.0) array_almost_equal(u, A([[[[-1, -1, 0, 2, 2], [-1, -1, 0, 2, 2], [0, 0, 0, 0, 0], [-3, -3, 0, 4, 4], [-3, -3, 0, 4, 4]]]]) / 4.0)
def test_bitonicity_and_extremas_interval(self): shp = (5, 1, 1) tinpl, tinpu = T.tensor3s('tinpl', 'tinpu') iinp = TheanoInterval(tinpl, tinpu) out = a_norm(iinp, shp) b = 200.0 a = (2.0 * (50000.0 + b * b))**0.5 inp = A([[[b]], [[0.0]], [[a]], [[0.0]], [[0.0]]]) d = {tinpl: inp, tinpu: inp} l1, _ = out.eval(d) inp[2, 0, 0] = a - 20.0 l2, _ = out.eval(d) inp[2, 0, 0] = a + 20.0 l3, _ = out.eval(d) inp[2, 0, 0] = a - 19.0 l4, _ = out.eval(d) inp[2, 0, 0] = a - 18.0 l5, _ = out.eval(d) inpl = inp.copy() inpu = inp.copy() d2 = {tinpl: inpl, tinpu: inpu} inpl[2, 0, 0] = a - 18.0 inpu[2, 0, 0] = a + 20.0 l6, _ = out.eval(d2) inpl[2, 0, 0] = a - 19.0 inpu[2, 0, 0] = a + 20.0 l7, _ = out.eval(d2) inpl[2, 0, 0] = a - 20.0 inpu[2, 0, 0] = a + 20.0 l8, _ = out.eval(d2) inpl[2, 0, 0] = a - 19.0 inpu[2, 0, 0] = a + 20.0 assert_greater(l1[2, 0, 0], l2[2, 0, 0]) assert_greater(l1[2, 0, 0], l3[2, 0, 0]) assert_greater(l5[2, 0, 0], l3[2, 0, 0]) assert_almost_equal(l3[2, 0, 0], l6[2, 0, 0], places=2) assert_almost_equal(l3[2, 0, 0], l7[2, 0, 0], places=2)
def theano_interval(x): v = theano_var(x) return TheanoInterval(v, v)
def test_1_output(self): dout = TheanoInterval.derest_output(1) din = d_softmax(dout) l, u = din.eval() array_almost_equal(l, u) array_almost_equal(l, A([[1]]))
def test_3_outputs(self): dout = TheanoInterval.derest_output(3) din = d_softmax(dout) l, u = din.eval() array_almost_equal(l, u) array_almost_equal(l, A([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))