-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
test_insert_along_axis.py
69 lines (59 loc) · 2.27 KB
/
test_insert_along_axis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Tests for the InsertAlongAxis op."""
import numpy as np
import theano
from theano import config
from theano import tensor
from pylearn2.utils.insert_along_axis import (
insert_columns, insert_rows, InsertAlongAxis
)
def test_insert_along_axis():
x = tensor.matrix()
y = insert_columns(x, 10, range(0, 10, 2))
f = theano.function([x], y)
x_ = np.random.normal(size=(7, 5)).astype(config.floatX)
f_val = f(x_)
assert f_val.shape == (7, 10)
assert np.all(f_val[:, range(0, 10, 2)] == x_)
assert f_val.dtype == x_.dtype
y = insert_rows(x, 10, range(0, 10, 2))
f = theano.function([x], y)
x_ = np.random.normal(size=(5, 6)).astype(config.floatX)
f_val = f(x_)
assert f_val.shape == (10, 6)
assert np.all(f_val[range(0, 10, 2)] == x_)
assert f_val.dtype == x_.dtype
x = tensor.tensor3()
y = InsertAlongAxis(3, 1)(x, 10, range(0, 10, 2))
f = theano.function([x], y)
x_ = np.random.normal(size=(2, 5, 2)).astype(config.floatX)
f_val = f(x_)
assert f_val.shape == (2, 10, 2)
assert np.all(f_val[:, range(0, 10, 2), :] == x_)
assert f_val.dtype == x_.dtype
x = tensor.tensor3()
y = InsertAlongAxis(3, 1, fill=2)(x, 10, range(0, 10, 2))
f = theano.function([x], y)
x_ = np.random.normal(size=(2, 5, 2)).astype(config.floatX)
f_val = f(x_)
assert f_val.shape == (2, 10, 2)
assert np.all(f_val[:, range(0, 10, 2), :] == x_)
assert np.all(f_val[:, range(1, 10, 2), :] == 2)
assert f_val.dtype == x_.dtype
def test_insert_along_axis_gradient():
x = tensor.matrix()
y = insert_columns(x, 10, range(0, 10, 2))
f = theano.function([x], tensor.grad(y.sum(), x))
f_val = f(np.random.normal(size=(7, 5)).astype(config.floatX))
assert np.all(f_val == 1)
assert f_val.shape == (7, 5)
y = insert_rows(x, 10, range(0, 10, 2))
f = theano.function([x], tensor.grad(y.sum(), x))
f_val = f(np.random.normal(size=(5, 6)).astype(config.floatX))
assert np.all(f_val == 1)
assert f_val.shape == (5, 6)
x = tensor.tensor3()
y = InsertAlongAxis(3, 1)(x, 10, range(0, 10, 2))
f = theano.function([x], tensor.grad(y.sum(), x))
f_val = f(np.random.normal(size=(2, 5, 2)).astype(config.floatX))
assert np.all(f_val == 1)
assert f_val.shape == (2, 5, 2)