Here are the examples of the python api numpy.random.random.astype taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
80 Examples
3
Example 1
Project: scikit-learn Source File: test_ball_tree.py
def test_node_heap(n_nodes=50):
vals = np.random.random(n_nodes).astype(DTYPE)
i1 = np.argsort(vals)
vals2, i2 = nodeheap_sort(vals)
assert_array_almost_equal(i1, i2)
assert_array_almost_equal(vals[i1], vals2)
3
Example 2
Project: pIDLy Source File: pidly.py
def test_100_dicts_float32_double_string(self):
x = [{'a':numpy.random.random(2).astype('float32'),
'b':numpy.random.random(1),
'c':(numpy.random.random(1)*100).astype('str')}
for i in range(100)]
y = self.sendAndReceive(x)
for i, d in enumerate(x):
self.assertEqual([y[i][key].tolist() for key in y[i]],
[d[key].tolist() for key in d])
for i, d in enumerate(x):
for key in d:
self.assertEqual(d[key][0].dtype,
y[i][key][0].dtype)
self.assertEqual(numpy.array(x).shape, numpy.array(y).shape)
3
Example 3
Project: attention-lvcsr Source File: test_nerv.py
def test_gemm16_value():
if nerv is None:
raise SkipTest("nervanagpu not available")
m = matrix(dtype='float16')
m2 = matrix(dtype='float16')
f = function([m, m2], dot(m, m2), mode=mode_with_gpu)
v1 = numpy.random.random((3, 4)).astype('float16')
v2 = numpy.random.random((4, 2)).astype('float16')
of = f(v1, v2)
on = numpy.dot(v1, v2)
utt.assert_allclose(of, on)
3
Example 4
Project: attention-lvcsr Source File: test_extra_ops.py
def test_var_interface(self):
# same as test_op, but use a_theano_var.squeeze.
for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = numpy.random.random(size=shape).astype(theano.config.floatX)
variable = tensor.TensorType(theano.config.floatX, broadcast)()
f = theano.function([variable], variable.squeeze())
expected = numpy.squeeze(data)
tested = f(data)
assert tested.shape == expected.shape
assert numpy.allclose(tested, expected)
3
Example 5
Project: attention-lvcsr Source File: test_extra_ops.py
def test_CumprodOp(self):
x = T.tensor3('x')
a = np.random.random((3, 5, 2)).astype(config.floatX)
# Test axis out of bounds
self.assertRaises(ValueError, cuemprod, x, axis=3)
self.assertRaises(ValueError, cuemprod, x, axis=-4)
f = theano.function([x], cuemprod(x))
assert np.allclose(np.cuemprod(a), f(a)) # Test axis=None
for axis in range(-len(a.shape), len(a.shape)):
f = theano.function([x], cuemprod(x, axis=axis))
assert np.allclose(np.cuemprod(a, axis=axis), f(a))
3
Example 6
Project: scikit-learn Source File: test_ball_tree.py
def test_neighbors_heap(n_pts=5, n_nbrs=10):
heap = NeighborsHeap(n_pts, n_nbrs)
for row in range(n_pts):
d_in = np.random.random(2 * n_nbrs).astype(DTYPE)
i_in = np.arange(2 * n_nbrs, dtype=ITYPE)
for d, i in zip(d_in, i_in):
heap.push(row, d, i)
ind = np.argsort(d_in)
d_in = d_in[ind]
i_in = i_in[ind]
d_heap, i_heap = heap.get_arrays(sort=True)
assert_array_almost_equal(d_in[:n_nbrs], d_heap[row])
assert_array_almost_equal(i_in[:n_nbrs], i_heap[row])
3
Example 7
Project: nolearn Source File: test_base.py
def test_no_conv(self, net_no_conv):
net_no_conv.initialize()
X = np.random.random((10, 100)).astype(floatX)
result = net_no_conv.get_output('output', X)
expected = net_no_conv.predict_proba(X)
np.testing.assert_equal(result, expected)
3
Example 8
Project: attention-lvcsr Source File: test_opt.py
def test_print_op():
""" Test that print ops don't block gpu optimization"""
b = tensor.fmatrix()
f = theano.function([b], theano.printing.Print()(b)*2, mode=mode_with_gpu)
# theano.printing.debugprint(f)
# print f.maker.fgraph.toposort()
#[GpuFromHost(<TensorType(float32, matrix)>), <theano.printing.Print object at 0x3581210>(GpuFromHost.0), GpuElemwise{mul}(CudaNdarray{[[ 2.]]}, <theano.printing.Print object at 0x3581210>.0), HostFromGpu(GpuElemwise{mul}.0)]
topo = f.maker.fgraph.toposort()
assert topo[0].op == cuda.gpu_from_host
assert isinstance(topo[1].op, theano.printing.Print)
assert isinstance(topo[2].op, cuda.GpuElemwise)
assert topo[3].op == cuda.host_from_gpu
f(numpy.random.random((5, 5)).astype('float32'))
3
Example 9
Project: rio-color Source File: test_colorspace.py
def test_bad_array_type():
bad = np.random.random((3, 3, 3)).astype('uint8')
with pytest.raises(ValueError) as exc:
saturate_rgb(bad, 1.1)
assert 'dtype mismatch' in str(exc.value)
with pytest.raises(ValueError) as exc:
convert_arr(bad, cs.rgb, cs.lch)
assert 'dtype mismatch' in str(exc.value)
3
Example 10
Project: attention-lvcsr Source File: test_opt.py
def test_local_lift_dot22scalar():
x = tensor.matrix()
y = tensor.matrix()
a = tensor.scalar()
o = tensor.blas.Dot22Scalar()(x, y, a)
f_cpu = theano.function([x, y, a], o)
f_gpu = theano.function([x, y, a], o, mode=mode_with_gpu)
assert not any(isinstance(n.op, tensor.blas.Dot22Scalar)
for n in f_gpu.maker.fgraph.apply_nodes)
assert any(isinstance(n.op, GpuGemm)
for n in f_gpu.maker.fgraph.apply_nodes)
x_val = numpy.random.random((2, 3)).astype(theano.config.floatX)
y_val = numpy.random.random((3, 4)).astype(theano.config.floatX)
a_val = 0.5
utt.assert_allclose(f_cpu(x_val, y_val, a_val), f_gpu(x_val, y_val, a_val))
3
Example 11
def test_op(self):
for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = numpy.random.random(size=shape).astype(theano.config.floatX)
variable = tensor.TensorType(theano.config.floatX, broadcast)()
f = theano.function([variable], self.op(variable))
expected = numpy.squeeze(data)
tested = f(data)
assert tested.shape == expected.shape
assert numpy.allclose(tested, expected)
3
Example 12
def test_grad(self):
a = np.random.random((3, 5, 2)).astype(config.floatX)
utt.verify_grad(self.op, [a]) # Test axis=None
for axis in range(-len(a.shape), len(a.shape)):
utt.verify_grad(self.op_class(axis=axis), [a])
3
Example 13
def test_infer_shape(self):
for shape, broadcast in zip(self.shape_list, self.broadcast_list):
data = numpy.random.random(size=shape).astype(theano.config.floatX)
variable = tensor.TensorType(theano.config.floatX, broadcast)()
self._compile_and_check([variable],
[self.op(variable)],
[data],
tensor.DimShuffle,
warn=False)
3
Example 14
def test_grad(self):
for ndim in range(3):
a = np.random.random((10, ) * ndim).astype(config.floatX)
for axis in self._possible_axis(ndim):
utt.verify_grad(lambda x: RepeatOp(axis=axis)(x, 3), [a])
3
Example 15
Project: scikit-learn Source File: test_ball_tree.py
def test_simultaneous_sort(n_rows=10, n_pts=201):
dist = np.random.random((n_rows, n_pts)).astype(DTYPE)
ind = (np.arange(n_pts) + np.zeros((n_rows, 1))).astype(ITYPE)
dist2 = dist.copy()
ind2 = ind.copy()
# simultaneous sort rows using function
simultaneous_sort(dist, ind)
# simultaneous sort rows using numpy
i = np.argsort(dist2, axis=1)
row_ind = np.arange(n_rows)[:, None]
dist2 = dist2[row_ind, i]
ind2 = ind2[row_ind, i]
assert_array_almost_equal(dist, dist2)
assert_array_almost_equal(ind, ind2)
3
Example 16
def __init__(self, n_docs, n_topics, n_dim, n_vocab):
factors = np.random.random((n_topics, n_dim)).astype('float32')
super(LDA, self).__init__(proportions=L.EmbedID(n_docs, n_topics),
factors=L.Parameter(factors),
embedding=L.Linear(n_dim, n_vocab))
self.n_docs = n_docs
self.n_topics = n_topics
self.n_vocab = n_vocab
self.n_dim = n_dim
3
Example 17
def __init__(self, counts, n_docs, n_topics, n_dim, n_vocab, n_samples=5):
factors = np.random.random((n_topics, n_dim)).astype('float32')
loss_func = L.NegativeSampling(n_dim, counts, n_samples)
loss_func.W.data[:, :] = np.random.randn(*loss_func.W.data.shape)
loss_func.W.data[:, :] /= np.sqrt(np.prod(loss_func.W.data.shape))
super(NSLDA, self).__init__(proportions=L.EmbedID(n_docs, n_topics),
factors=L.Parameter(factors),
loss_func=loss_func)
self.n_docs = n_docs
self.n_topics = n_topics
self.n_vocab = n_vocab
self.n_dim = n_dim
3
Example 18
Project: C-PAC Source File: test_a_threshold.py
@attr('threshold', 'weighted')
def test_thresh_weighted():
print "testing threshold weighted"
nvoxs = 1000
r_value = 0.2
corr_matrix = np.random.random((nvoxs, nvoxs)).astype('float32')
ref = corr_matrix*(corr_matrix>r_value)
comp = corr_matrix.copy()
thresh_weighted_float(comp, r_value)
assert_equal(ref, comp)
3
Example 19
Project: attention-lvcsr Source File: test_extra_ops.py
def test_op(self):
for axis, cond, shape in zip(self.axis_list, self.cond_list,
self.shape_list):
cond_var = theano.tensor.ivector()
data = numpy.random.random(size=shape).astype(theano.config.floatX)
data_var = theano.tensor.matrix()
f = theano.function([cond_var, data_var],
self.op(cond_var, data_var, axis=axis))
expected = numpy.compress(cond, data, axis=axis)
tested = f(cond, data)
assert tested.shape == expected.shape
assert numpy.allclose(tested, expected)
3
Example 20
Project: pIDLy Source File: pidly.py
def test_50_float32_1e30(self):
x = numpy.random.random(50).astype('float32') * 1e30
y = self.sendAndReceive(x)
self.assertEqual(y.tolist(), x.tolist())
self.assertEqual(numpy.array(x).dtype, y.dtype)
self.assertEqual(numpy.array(x).shape, y.shape)
3
Example 21
Project: C-PAC Source File: test_a_threshold.py
@attr('threshold', 'transform', 'weighted')
def test_thresh_transform_weighted():
print "testing threshold weighted"
nvoxs = 1000
r_value = 0.2
corr_matrix = np.random.random((nvoxs, nvoxs)).astype('float32')
ref = ((1.0+corr_matrix)/2.0)*(corr_matrix>r_value)
comp = corr_matrix.copy()
thresh_transform_weighted_float(comp, r_value)
assert_equal(ref, comp)
3
Example 22
def test_grad(self):
a = np.random.random((3, 5, 2)).astype(config.floatX)
utt.verify_grad(self.op, [a]) # Test axis=None
for axis in range(-len(a.shape), len(a.shape)):
utt.verify_grad(self.op_class(axis=axis), [a], eps=4e-4)
3
Example 23
Project: attention-lvcsr Source File: test_opt.py
def test_print_op():
""" Test that print ops don't block gpu optimization"""
b = tensor.fmatrix()
f = theano.function([b], theano.printing.Print()(b) * 2,
mode=mode_with_gpu)
topo = f.maker.fgraph.toposort()
assert isinstance(topo[0].op, GpuFromHost)
assert isinstance(topo[1].op, theano.printing.Print)
assert isinstance(topo[2].op, GpuElemwise)
assert topo[3].op == host_from_gpu
f(numpy.random.random((5, 5)).astype('float32'))
3
Example 24
def test_infer_shape(self):
x = T.tensor3('x')
a = np.random.random((3, 5, 2)).astype(config.floatX)
# Test axis=None
self._compile_and_check([x],
[self.op(x)],
[a],
self.op_class)
for axis in range(-len(a.shape), len(a.shape)):
self._compile_and_check([x],
[cuemprod(x, axis=axis)],
[a],
self.op_class)
3
Example 25
Project: attention-lvcsr Source File: test_extra_ops.py
def test_cuemsumOp(self):
x = T.tensor3('x')
a = np.random.random((3, 5, 2)).astype(config.floatX)
# Test axis out of bounds
self.assertRaises(ValueError, cuemsum, x, axis=3)
self.assertRaises(ValueError, cuemsum, x, axis=-4)
f = theano.function([x], cuemsum(x))
assert np.allclose(np.cuemsum(a), f(a)) # Test axis=None
for axis in range(-len(a.shape), len(a.shape)):
f = theano.function([x], cuemsum(x, axis=axis))
assert np.allclose(np.cuemsum(a, axis=axis), f(a))
3
Example 26
def test_grad(self):
x = T.vector('x')
a = np.random.random(50).astype(config.floatX)
theano.function([x], T.grad(T.sum(diff(x)), x))
utt.verify_grad(self.op, [a])
for k in range(TestDiffOp.nb):
theano.function([x], T.grad(T.sum(diff(x, n=k)), x))
utt.verify_grad(DiffOp(n=k), [a], eps=7e-3)
3
Example 27
Project: attention-lvcsr Source File: test_extra_ops.py
def test_diffOp(self):
x = T.matrix('x')
a = np.random.random((30, 50)).astype(config.floatX)
f = theano.function([x], diff(x))
assert np.allclose(np.diff(a), f(a))
for axis in range(len(a.shape)):
for k in range(TestDiffOp.nb):
g = theano.function([x], diff(x, n=k, axis=axis))
assert np.allclose(np.diff(a, n=k, axis=axis), g(a))
3
Example 28
Project: C-PAC Source File: test_a_threshold.py
@attr('threshold', 'binarize')
def test_thresh_binarize():
print "testing threshold binarize"
nvoxs = 1000
r_value = 0.2
corr_matrix = np.random.random((nvoxs, nvoxs)).astype('float32')
ref = 1*(corr_matrix>r_value)
comp = corr_matrix.copy()
thresh_binarize_float(comp, r_value)
assert_equal(ref, comp)
3
Example 29
Project: pIDLy Source File: pidly.py
def test_50_float32(self):
x = numpy.random.random(50).astype('float32')
y = self.sendAndReceive(x)
self.assertEqual(y.tolist(), x.tolist())
self.assertEqual(numpy.array(x).dtype, y.dtype)
self.assertEqual(numpy.array(x).shape, y.shape)
3
Example 30
def test_infer_shape(self):
x = T.tensor3('x')
a = np.random.random((3, 5, 2)).astype(config.floatX)
# Test axis=None
self._compile_and_check([x],
[self.op(x)],
[a],
self.op_class)
for axis in range(-len(a.shape), len(a.shape)):
self._compile_and_check([x],
[cuemsum(x, axis=axis)],
[a],
self.op_class)
3
Example 31
def test_infer_shape(self):
x = T.matrix('x')
a = np.random.random((30, 50)).astype(config.floatX)
self._compile_and_check([x],
[self.op(x)],
[a],
self.op_class)
for axis in range(len(a.shape)):
for k in range(TestDiffOp.nb):
self._compile_and_check([x],
[diff(x, n=k, axis=axis)],
[a],
self.op_class)
0
Example 32
Project: attention-lvcsr Source File: test_gemmcorr3d.py
def run_conv_valid(self, inputs_shape, filters_shape,
subsample=(1, 1, 1)):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
bias = shared(numpy.zeros(filters_shape[0]).astype('float32'))
conv_ref = theano.tensor.nnet.conv3D(V=inputs, W=filters,
b=bias, d=subsample)
conv = GpuCorr3dMM(border_mode="valid",
subsample=subsample)(
inputs.dimshuffle(0, 4, 1, 2, 3),
filters.dimshuffle(0, 4, 1, 2, 3))
conv = conv.dimshuffle(0, 2, 3, 4, 1)
f_ref = theano.function([], conv_ref)
f = theano.function([], conv, mode=mode_with_gpu)
res_ref = f_ref()
res = f()
utt.assert_allclose(res_ref, res)
0
Example 33
Project: attention-lvcsr Source File: test_fftconv.py
def test_opt_full(self):
inputs_shape = (5, 3, 7, 6)
filters_shape = (2, 3, 3, 3)
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
conv = theano.tensor.nnet.conv.conv2d(inputs, filters,
border_mode='full')
mode = mode_with_gpu.including('conv_fft_full')
f_ref = theano.function([], conv)
f_fft = theano.function([], conv, mode=mode)
# make sure we inserted the fft trickery
topo = f_fft.maker.fgraph.toposort()
assert sum(isinstance(n.op, theano.sandbox.cuda.fftconv.CuFFTOp)
for n in topo) == 2, topo
res_ref = f_ref()
res_fft = f_fft()
utt.assert_allclose(res_ref, res_fft)
0
Example 34
Project: attention-lvcsr Source File: test_gemmcorr3d.py
def run_gradweight(self, inputs_shape, filters_shape, dCdH_shape,
subsample=(1, 1, 1)):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
dCdH_val = numpy.random.random(dCdH_shape).astype('float32')
inputs = shared(inputs_val)
dCdH = shared(dCdH_val)
conv = theano.tensor.nnet.convGrad3D(V=inputs, dCdH=dCdH,
WShape=filters_shape,
d=subsample)
img = gpu_contiguous(inputs.dimshuffle(0, 4, 1, 2, 3))
topgrad = gpu_contiguous(dCdH.dimshuffle(0, 4, 1, 2, 3))
if (subsample == (1, 1, 1)):
conv_gemm = GpuCorr3dMM_gradWeights(subsample=subsample)(img,
topgrad)
else:
conv_gemm = GpuCorr3dMM_gradWeights(subsample=subsample)(
img, topgrad, shape=filters_shape[1:4])
conv_gemm = conv_gemm.dimshuffle(0, 2, 3, 4, 1)
f_ref = theano.function([], conv)
f = theano.function([], conv_gemm, mode=mode_with_gpu)
res_ref = f_ref()
res = f()
utt.assert_allclose(res_ref, res)
0
Example 35
Project: attention-lvcsr Source File: test_gemmcorr3d.py
def run_gradinput(self, inputs_shape, filters_shape,
subsample=(1, 1, 1)):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
bias = shared(numpy.zeros(filters_shape[4]).astype('float32'))
conv = theano.tensor.nnet.convTransp3D(W=filters, b=bias, d=subsample,
H=inputs)
f_ref = theano.function([], conv)
res_ref = f_ref()
# Get bottom shape using convTransp3D
bottom_shape = res_ref.shape
bottom_val = numpy.random.random(bottom_shape).astype('float32')
bottom = shared(bottom_val)
weight = gpu_contiguous(filters.dimshuffle(0, 4, 1, 2, 3))
top = gpu_contiguous(inputs.dimshuffle(0, 4, 1, 2, 3))
if (subsample == (1, 1, 1)):
conv_gemm = GpuCorr3dMM_gradInputs(subsample=subsample)(
kern=weight, topgrad=top)
else:
conv_gemm = GpuCorr3dMM_gradInputs(subsample=subsample)(
kern=weight, topgrad=top,
shape=bottom.shape[1:4])
conv_gemm = conv_gemm.dimshuffle(0, 2, 3, 4, 1)
f = theano.function([], conv_gemm, mode=mode_with_gpu)
res = f()
utt.assert_allclose(res_ref, res)
0
Example 36
Project: attention-lvcsr Source File: test_extra_ops.py
def test_GpuCumsum1D(self):
block_max_size = self.max_threads_dim0 * 2
x = T.fvector('x')
f = theano.function([x], cuemsum(x), mode=self.mode)
assert [n for n in f.maker.fgraph.toposort()
if isinstance(n.op, GpuCumsum)]
# Extensive testing for the first 1025 sizes
a = np.random.random(1025).astype("float32")
for i in xrange(a.shape[0]):
utt.assert_allclose(np.cuemsum(a[:i]), f(a[:i]))
# Use multiple GPU threadblocks
a = np.random.random((block_max_size+2,)).astype("float32")
utt.assert_allclose(np.cuemsum(a), f(a))
# Use recursive cuemsum
a = np.ones((block_max_size*(block_max_size+1)+2,),
dtype="float32")
utt.assert_allclose(np.cuemsum(a), f(a))
0
Example 37
Project: attention-lvcsr Source File: test_fftconv.py
def test_opt_nofft_valid(self):
inputs_shape = (5, 3, 7, 6)
filters_shape = (2, 3, 3, 3)
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
conv = theano.tensor.nnet.conv.conv2d(inputs, filters,
version='no_fft')
mode = mode_with_gpu.including('conv_fft_valid')
f_fft = theano.function([], conv, mode=mode)
# make sure we that no CuFFTOp has been inserted
topo = f_fft.maker.fgraph.toposort()
assert sum(isinstance(n.op, theano.sandbox.cuda.fftconv.CuFFTOp)
for n in topo) == 0
0
Example 38
Project: attention-lvcsr Source File: test_gemmcorr3d.py
def test_opt_conv3d_gemm(self):
inputs_shape = (16, 20, 32, 16, 1)
filters_shape = (10, 6, 12, 4, 1)
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
bias = shared(numpy.zeros(filters_shape[0]).astype('float32'))
conv = theano.tensor.nnet.conv3D(V=inputs, W=filters,
b=bias, d=(1, 1, 1))
mode = mode_with_gpu.including('conv3d_gemm')
mode.check_py_code = False
f_ref = theano.function([], conv, mode="FAST_RUN")
f_gemm = theano.function([], conv, mode=mode)
# make sure we inserted the gemm trickery
topo = f_gemm.maker.fgraph.toposort()
assert sum(isinstance(n.op, GpuCorr3dMM) for n in topo) > 0
res_ref = f_ref()
res_gemm = f_gemm()
utt.assert_allclose(res_ref, res_gemm)
0
Example 39
Project: attention-lvcsr Source File: test_fftconv.py
def test_opt_nofft_full(self):
inputs_shape = (5, 3, 7, 6)
filters_shape = (2, 3, 3, 3)
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
conv = theano.tensor.nnet.conv.conv2d(inputs, filters,
border_mode='full',
version='no_fft')
mode = mode_with_gpu.including('conv_fft_full')
f_fft = theano.function([], conv, mode=mode)
# make sure we that no CuFFTOp has been inserted
topo = f_fft.maker.fgraph.toposort()
assert sum(isinstance(n.op, theano.sandbox.cuda.fftconv.CuFFTOp)
for n in topo) == 0
0
Example 40
Project: attention-lvcsr Source File: test_extra_ops.py
def test_Strides2D(self):
x = T.fmatrix('x')
for axis in [0, 1, None, -1, -2]:
a = np.random.random((42, 30)).astype("float32")
cuemsum_function = theano.function([x], cuemsum(x, axis=axis),
mode=self.mode)
slicings = [slice(None, None, None), # Normal strides
slice(None, None, 2), # Stepped strides
slice(None, None, -1), # Negative strides
]
# Cartesian product of all slicings to test.
for slicing in itertools.product(slicings, repeat=x.ndim):
f = theano.function([x], cuemsum(x[slicing], axis=axis),
mode=self.mode)
assert [n for n in f.maker.fgraph.toposort()
if isinstance(n.op, GpuCumsum)]
utt.assert_allclose(np.cuemsum(a[slicing], axis=axis), f(a))
utt.assert_allclose(np.cuemsum(a[slicing], axis=axis),
cuemsum_function(a[slicing]))
0
Example 41
Project: attention-lvcsr Source File: test_extra_ops.py
def test_GpuCumsum2D(self):
block_max_size = self.max_threads_dim0 * 2
x = T.fmatrix('x')
for shape_axis, axis in zip([0, 1, 0, 1, 0], [0, 1, None, -1, -2]):
f = theano.function([x], cuemsum(x, axis=axis), mode=self.mode)
assert [n for n in f.maker.fgraph.toposort()
if isinstance(n.op, GpuCumsum)]
# Extensive testing for the first 1025 sizes
a_shape = [5, 5]
a_shape[shape_axis] = 1025
a = np.random.random(a_shape).astype("float32")
slices = [slice(None), slice(None)]
for i in xrange(a.shape[shape_axis]):
slices[shape_axis] = slice(i)
fa = f(a[slices])
npa = np.cuemsum(a[slices], axis=axis)
utt.assert_allclose(npa, fa)
# Use multiple GPU threadblocks
a_shape = [5, 5]
a_shape[shape_axis] = block_max_size+2
a = np.random.random(a_shape).astype("float32")
utt.assert_allclose(np.cuemsum(a, axis=axis), f(a))
# Use multiple GPU gridblocks
a_shape = [4, 4]
a_shape[1-shape_axis] = self.max_grid_size1+1
a = np.random.random(a_shape).astype("float32")
utt.assert_allclose(np.cuemsum(a, axis=axis), f(a), rtol=5e-5)
# Use recursive cuemsum
a_shape = [3, 3]
a_shape[shape_axis] = block_max_size*(block_max_size+1)+2
a = np.random.random(a_shape).astype("float32")
a = np.sign(a-0.5).astype("float32") # Avoid floating point error
utt.assert_allclose(np.cuemsum(a, axis=axis), f(a))
0
Example 42
Project: attention-lvcsr Source File: test_fftconv.py
def run_conv_valid(self, inputs_shape, filters_shape, pad=False):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
bias = shared(numpy.zeros(filters_shape[0]).astype('float32'))
# Flip filter as conv3D compute correlation
filters_flip = filters[:, ::-1, ::-1, ::-1, :]
# filters_flip = filters
conv_ref = theano.tensor.nnet.conv3D(V=inputs, W=filters_flip,
b=bias, d=(1, 1, 1))
conv_fft = theano.sandbox.cuda.fftconv.conv3d_fft(
inputs.dimshuffle(0, 4, 1, 2, 3),
filters.dimshuffle(0, 4, 1, 2, 3),
border_mode="valid",
pad_last_dim=pad)
conv_fft = conv_fft.dimshuffle(0, 2, 3, 4, 1)
f_ref = theano.function([], conv_ref, mode="FAST_RUN")
mode = mode_with_gpu
mode.check_py_code = False
f_fft = theano.function([], conv_fft, mode=mode)
res_ref = f_ref()
res_fft = f_fft()
utt.assert_allclose(res_ref, res_fft, rtol=1e-05, atol=1e-05)
0
Example 43
Project: attention-lvcsr Source File: test_fftconv.py
def run_conv_full(self, inputs_shape, filters_shape, pad=False):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
bias = shared(numpy.zeros(filters_shape[4]).astype('float32'))
conv_ref = theano.tensor.nnet.convTransp3D(
W=filters, b=bias, d=(1, 1, 1),
H=inputs)
filters = filters.dimshuffle(4, 0, 1, 2, 3)
inputs = inputs.dimshuffle(0, 4, 1, 2, 3)
conv_fft = theano.sandbox.cuda.fftconv.conv3d_fft(inputs, filters,
border_mode="full",
pad_last_dim=pad)
conv_fft = conv_fft.dimshuffle(0, 2, 3, 4, 1)
f_ref = theano.function([], conv_ref)
f_fft = theano.function([], conv_fft, mode=mode_with_gpu)
res_ref = f_ref()
res_fft = f_fft()
utt.assert_allclose(res_ref, res_fft, rtol=1e-04, atol=1e-04)
0
Example 44
Project: attention-lvcsr Source File: test_fftconv.py
def run_conv(self, inputs_shape, filters_shape, pad=False, **other_args):
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
conv_ref = theano.tensor.nnet.conv.conv2d(inputs, filters,
**other_args)
conv_fft = theano.sandbox.cuda.fftconv.conv2d_fft(inputs, filters,
pad_last_dim=pad,
**other_args)
f_ref = theano.function([], conv_ref)
f_fft = theano.function([], conv_fft, mode=mode_with_gpu)
res_ref = f_ref()
res_fft = f_fft()
utt.assert_allclose(res_ref, res_fft)
0
Example 45
Project: attention-lvcsr Source File: test_fftconv.py
def test_opt_conv3d_fft(self):
inputs_shape = (16, 20, 32, 16, 1)
filters_shape = (10, 6, 12, 4, 1)
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
bias = shared(numpy.zeros(filters_shape[0]).astype('float32'))
conv = theano.tensor.nnet.conv3D(V=inputs, W=filters,
b=bias, d=(1, 1, 1))
mode = mode_with_gpu.including('conv3d_fft')
mode.check_py_code = False
f_ref = theano.function([], conv, mode="FAST_RUN")
f_fft = theano.function([], conv, mode=mode)
# make sure we inserted the fft trickery
topo = f_fft.maker.fgraph.toposort()
assert sum(isinstance(n.op, theano.sandbox.cuda.fftconv.CuFFTOp)
for n in topo) == 2
res_ref = f_ref()
res_fft = f_fft()
utt.assert_allclose(res_ref, res_fft)
0
Example 46
Project: attention-lvcsr Source File: test_fftconv.py
def test_opt_convgrad3d_fft(self):
inputs_shape = (2, 17, 15, 16, 1)
filters_shape = (10, 6, 7, 4, 1)
dCdH_shape = (inputs_shape[0],
inputs_shape[1] - filters_shape[1] + 1,
inputs_shape[2] - filters_shape[2] + 1,
inputs_shape[3] - filters_shape[3] + 1,
filters_shape[0])
inputs_val = numpy.random.random(inputs_shape).astype('float32')
dCdH_val = numpy.random.random(dCdH_shape).astype('float32')
inputs = shared(inputs_val)
dCdH = shared(dCdH_val)
conv = theano.tensor.nnet.convGrad3D(V=inputs, dCdH=dCdH,
WShape=filters_shape,
d=(1, 1, 1))
mode = mode_with_gpu.including('convgrad3d_fft')
mode.check_py_code = False
f_ref = theano.function([], conv, mode="FAST_RUN")
f_fft = theano.function([], conv, mode=mode)
# make sure we inserted the fft trickery
topo = f_fft.maker.fgraph.toposort()
assert sum(isinstance(n.op, theano.sandbox.cuda.fftconv.CuFFTOp)
for n in topo) == 2
res_ref = f_ref()
res_fft = f_fft()
utt.assert_allclose(res_ref, res_fft, rtol=1e-04, atol=1e-04)
0
Example 47
Project: attention-lvcsr Source File: test_extra_ops.py
def test_Strides1D(self):
x = T.fvector('x')
for axis in [0, None, -1]:
a = np.random.random((42,)).astype("float32")
cuemsum_function = theano.function([x], cuemsum(x, axis=axis),
mode=self.mode)
slicings = [slice(None, None, None), # Normal strides
slice(None, None, 2), # Stepped strides
slice(None, None, -1), # Negative strides
]
# Cartesian product of all slicings to test.
for slicing in itertools.product(slicings, repeat=x.ndim):
f = theano.function([x], cuemsum(x[slicing], axis=axis),
mode=self.mode)
assert [n for n in f.maker.fgraph.toposort()
if isinstance(n.op, GpuCumsum)]
utt.assert_allclose(np.cuemsum(a[slicing], axis=axis), f(a))
utt.assert_allclose(np.cuemsum(a[slicing], axis=axis),
cuemsum_function(a[slicing]))
0
Example 48
Project: attention-lvcsr Source File: test_extra_ops.py
def test_Strides3D(self):
x = T.ftensor3('x')
for axis in [0, 1, 2, None, -1, -2, -3]:
a = np.random.random((42, 30, 25)).astype("float32")
cuemsum_function = theano.function([x], cuemsum(x, axis=axis),
mode=self.mode)
slicings = [slice(None, None, None), # Normal strides
slice(None, None, 2), # Stepped strides
slice(None, None, -1), # Negative strides
]
# Cartesian product of all slicings to test.
for slicing in itertools.product(slicings, repeat=x.ndim):
f = theano.function([x], cuemsum(x[slicing], axis=axis),
mode=self.mode)
assert [n for n in f.maker.fgraph.toposort()
if isinstance(n.op, GpuCumsum)]
utt.assert_allclose(np.cuemsum(a[slicing], axis=axis), f(a))
utt.assert_allclose(np.cuemsum(a[slicing], axis=axis),
cuemsum_function(a[slicing]))
0
Example 49
Project: attention-lvcsr Source File: test_fftconv.py
def test_opt_valid(self):
inputs_shape = (5, 3, 7, 6)
filters_shape = (2, 3, 3, 3)
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
inputs = shared(inputs_val)
filters = shared(filters_val)
conv = theano.tensor.nnet.conv.conv2d(inputs, filters)
mode = mode_with_gpu.including('conv_fft_valid')
f_ref = theano.function([], conv)
f_fft = theano.function([], conv, mode=mode)
# make sure we inserted the fft trickery
topo = f_fft.maker.fgraph.toposort()
assert sum(isinstance(n.op, theano.sandbox.cuda.fftconv.CuFFTOp)
for n in topo) == 2, topo
res_ref = f_ref()
res_fft = f_fft()
utt.assert_allclose(res_ref, res_fft)
0
Example 50
Project: attention-lvcsr Source File: test_fftconv.py
def test_opt_convtransp3d_fft(self):
inputs_shape = (2, 9, 16, 12, 10)
filters_shape = (10, 3, 8, 4, 1)
inputs_val = numpy.random.random(inputs_shape).astype('float32')
filters_val = numpy.random.random(filters_shape).astype('float32')
bias = shared(numpy.zeros(filters_shape[4]).astype('float32'))
inputs = shared(inputs_val)
filters = shared(filters_val)
conv = theano.tensor.nnet.convTransp3D(W=filters, b=bias, d=(1, 1, 1),
H=inputs)
mode = mode_with_gpu.including('convtransp3d_fft')
f_ref = theano.function([], conv)
f_fft = theano.function([], conv, mode=mode)
# make sure we inserted the fft trickery
topo = f_fft.maker.fgraph.toposort()
assert sum(isinstance(n.op, theano.sandbox.cuda.fftconv.CuFFTOp)
for n in topo) == 2
res_ref = f_ref()
res_fft = f_fft()
utt.assert_allclose(res_ref, res_fft, rtol=1e-04, atol=1e-04)