Skip to content

Commit

Permalink
Merge pull request #4350 from vilyaair/typecodes
Browse files Browse the repository at this point in the history
Prefer data type objects over character codes in the NumPy
  • Loading branch information
okuta committed Feb 19, 2018
2 parents ff41173 + 5e03494 commit a88982a
Show file tree
Hide file tree
Showing 64 changed files with 159 additions and 139 deletions.
4 changes: 2 additions & 2 deletions chainer/function.py
Expand Up @@ -24,7 +24,7 @@ def no_backprop_mode():
that calling :func:`~chainer.Variable.backward` on ``y`` has no effect on
the gradients of ``x``.
>>> x = chainer.Variable(np.array([1,], 'f'))
>>> x = chainer.Variable(np.array([1,], np.float32))
>>> with chainer.no_backprop_mode():
... y = x + 1
>>> y.backward()
Expand Down Expand Up @@ -53,7 +53,7 @@ def force_backprop_mode():
:func:`~chainer.Variable.backward` on ``y`` will compute and accumulate the
gradients of the variables in the graph, in this case only ``x``.
>>> x = chainer.Variable(np.array([1,], 'f'))
>>> x = chainer.Variable(np.array([1,], np.float32))
>>> with chainer.no_backprop_mode():
... with chainer.force_backprop_mode():
... y = x + 1
Expand Down
2 changes: 1 addition & 1 deletion chainer/function_hook.py
Expand Up @@ -55,7 +55,7 @@ class FunctionHook(object):
... return F.exp(self.l(x1))
>>> model1 = Model()
>>> model2 = Model()
>>> x = chainer.Variable(np.zeros((1, 10), 'f'))
>>> x = chainer.Variable(np.zeros((1, 10), np.float32))
>>> with chainer.function_hooks.TimerHook() as m:
... _ = model1(x)
... y = model2(x)
Expand Down
2 changes: 1 addition & 1 deletion chainer/function_hooks/debug_print.py
Expand Up @@ -34,7 +34,7 @@ class PrintHook(function_hook.FunctionHook):
>>> from chainer import function_hooks
>>> l = L.Linear(10, 10)
>>> x = chainer.Variable(np.zeros((1, 10), 'f'))
>>> x = chainer.Variable(np.zeros((1, 10), np.float32))
>>> with chainer.function_hooks.PrintHook():
... y = l(x)
... z = F.sum(y)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/clipped_relu.py
Expand Up @@ -93,7 +93,7 @@ def clipped_relu(x, z=20.0):
.. admonition:: Example
>>> x = np.random.uniform(-100, 100, (10, 20)).astype('f')
>>> x = np.random.uniform(-100, 100, (10, 20)).astype(np.float32)
>>> z = 10.0
>>> np.any(x < 0)
True
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/crelu.py
Expand Up @@ -70,7 +70,7 @@ def crelu(x, axis=1):
.. admonition:: Example
>>> x = np.array([[-1, 0], [2, -3]], 'f')
>>> x = np.array([[-1, 0], [2, -3]], np.float32)
>>> x
array([[-1., 0.],
[ 2., -3.]], dtype=float32)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/elu.py
Expand Up @@ -103,7 +103,7 @@ def elu(x, alpha=1.0):
.. admonition:: Example
>>> x = np.array([[-1, 0], [2, -3]], 'f')
>>> x = np.array([[-1, 0], [2, -3]], np.float32)
>>> x
array([[-1., 0.],
[ 2., -3.]], dtype=float32)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/leaky_relu.py
Expand Up @@ -110,7 +110,7 @@ def leaky_relu(x, slope=0.2):
.. admonition:: Example
>>> x = np.array([[-1, 0], [2, -3], [-2, 1]], 'f')
>>> x = np.array([[-1, 0], [2, -3], [-2, 1]], np.float32)
>>> x
array([[-1., 0.],
[ 2., -3.],
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/log_softmax.py
Expand Up @@ -154,7 +154,7 @@ def log_softmax(x):
.. admonition:: Example
>>> x = np.array([[0, 1, 2], [0, 2, 4]], 'f')
>>> x = np.array([[0, 1, 2], [0, 2, 4]], np.float32)
>>> x
array([[0., 1., 2.],
[0., 2., 4.]], dtype=float32)
Expand Down
6 changes: 3 additions & 3 deletions chainer/functions/activation/lstm.py
Expand Up @@ -332,9 +332,9 @@ def lstm(c_prev, x):
Most typical preparation of ``x`` is:
>>> n_units = 100
>>> y = chainer.Variable(np.zeros((1, n_units), 'f'))
>>> h = chainer.Variable(np.zeros((1, n_units), 'f'))
>>> c = chainer.Variable(np.zeros((1, n_units), 'f'))
>>> y = chainer.Variable(np.zeros((1, n_units), np.float32))
>>> h = chainer.Variable(np.zeros((1, n_units), np.float32))
>>> c = chainer.Variable(np.zeros((1, n_units), np.float32))
>>> model = chainer.Chain()
>>> with model.init_scope():
... model.w = L.Linear(n_units, 4 * n_units)
Expand Down
4 changes: 2 additions & 2 deletions chainer/functions/activation/maxout.py
Expand Up @@ -38,9 +38,9 @@ def maxout(x, pool_size, axis=1):
combination with a Linear link.
>>> in_size, out_size, pool_size = 10, 10, 10
>>> bias = np.arange(out_size * pool_size).astype('f')
>>> bias = np.arange(out_size * pool_size).astype(np.float32)
>>> l = L.Linear(in_size, out_size * pool_size, initial_bias=bias)
>>> x = np.zeros((1, in_size), 'f') # prepare data
>>> x = np.zeros((1, in_size), np.float32) # prepare data
>>> x = l(x)
>>> y = F.maxout(x, pool_size)
>>> x.shape
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/relu.py
Expand Up @@ -165,7 +165,7 @@ def relu(x):
.. admonition:: Example
>>> x = np.array([[-1, 0], [2, -3], [-2, 1]], 'f')
>>> x = np.array([[-1, 0], [2, -3], [-2, 1]], np.float32)
>>> np.any(x < 0)
True
>>> y = F.relu(x)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/sigmoid.py
Expand Up @@ -109,7 +109,7 @@ def sigmoid(x):
It maps the input values into the range of :math:`[0, 1]`.
>>> x = np.arange(-2, 3, 2).astype('f')
>>> x = np.arange(-2, 3, 2).astype(np.float32)
>>> x
array([-2., 0., 2.], dtype=float32)
>>> F.sigmoid(x)
Expand Down
8 changes: 4 additions & 4 deletions chainer/functions/activation/slstm.py
Expand Up @@ -253,10 +253,10 @@ def slstm(c_prev1, c_prev2, x1, x2):
Most typical preparation of ``x1``, ``x2`` is:
>>> n_units = 100
>>> h1 = chainer.Variable(np.zeros((1, n_units), 'f'))
>>> h2 = chainer.Variable(np.zeros((1, n_units), 'f'))
>>> c1 = chainer.Variable(np.zeros((1, n_units), 'f'))
>>> c2 = chainer.Variable(np.zeros((1, n_units), 'f'))
>>> h1 = chainer.Variable(np.zeros((1, n_units), np.float32))
>>> h2 = chainer.Variable(np.zeros((1, n_units), np.float32))
>>> c1 = chainer.Variable(np.zeros((1, n_units), np.float32))
>>> c2 = chainer.Variable(np.zeros((1, n_units), np.float32))
>>> model1 = chainer.Chain()
>>> with model1.init_scope():
... model1.w = L.Linear(n_units, 4 * n_units)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/softmax.py
Expand Up @@ -145,7 +145,7 @@ def softmax(x, axis=1):
.. admonition:: Example
>>> x = np.array([[0, 1, 2], [0, 2, 4]], 'f')
>>> x = np.array([[0, 1, 2], [0, 2, 4]], np.float32)
>>> x
array([[0., 1., 2.],
[0., 2., 4.]], dtype=float32)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/softplus.py
Expand Up @@ -109,7 +109,7 @@ def softplus(x, beta=1.0):
.. admonition:: Example
>>> x = np.arange(-2, 3, 2).astype('f')
>>> x = np.arange(-2, 3, 2).astype(np.float32)
>>> x
array([-2., 0., 2.], dtype=float32)
>>> F.softplus(x, beta=1.0).data
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/activation/tanh.py
Expand Up @@ -104,7 +104,7 @@ def tanh(x):
.. admonition:: Example
>>> x = np.arange(-1, 4, 2).astype('f')
>>> x = np.arange(-1, 4, 2).astype(np.float32)
>>> x
array([-1., 1., 3.], dtype=float32)
>>> F.tanh(x).data
Expand Down
10 changes: 5 additions & 5 deletions chainer/functions/activation/tree_lstm.py
Expand Up @@ -269,11 +269,11 @@ def tree_lstm(*inputs):
... model.w = L.Linear(10, 5 * 10)
... model.v1 = L.Linear(10, 5 * 10)
... model.v2 = L.Linear(10, 5 * 10)
>>> y = np.random.uniform(-1, 1, (4, 10)).astype('f')
>>> h1 = np.random.uniform(-1, 1, (4, 10)).astype('f')
>>> h2 = np.random.uniform(-1, 1, (4, 10)).astype('f')
>>> c1 = np.random.uniform(-1, 1, (4, 10)).astype('f')
>>> c2 = np.random.uniform(-1, 1, (4, 10)).astype('f')
>>> y = np.random.uniform(-1, 1, (4, 10)).astype(np.float32)
>>> h1 = np.random.uniform(-1, 1, (4, 10)).astype(np.float32)
>>> h2 = np.random.uniform(-1, 1, (4, 10)).astype(np.float32)
>>> c1 = np.random.uniform(-1, 1, (4, 10)).astype(np.float32)
>>> c2 = np.random.uniform(-1, 1, (4, 10)).astype(np.float32)
>>> x = model.w(y) + model.v1(h1) + model.v2(h2)
>>> c, h = F.tree_lstm(c1, c2, x)
Expand Down
4 changes: 2 additions & 2 deletions chainer/functions/array/broadcast.py
Expand Up @@ -64,11 +64,11 @@ def broadcast(*args):
.. admonition:: Example
>>> x = np.random.uniform(0, 1, (3, 2)).astype('f')
>>> x = np.random.uniform(0, 1, (3, 2)).astype(np.float32)
>>> y = F.broadcast(x)
>>> np.all(x == y.data)
True
>>> z = np.random.uniform(0, 1, (3, 2)).astype('f')
>>> z = np.random.uniform(0, 1, (3, 2)).astype(np.float32)
>>> y, w = F.broadcast(x, z)
>>> np.all(x == y.data) & np.all(z == w.data)
True
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/array/depth2space.py
Expand Up @@ -67,7 +67,7 @@ def depth2space(X, r):
.. admonition:: Example
>>> X = np.arange(24).reshape(1, 4, 2, 3).astype('f')
>>> X = np.arange(24).reshape(1, 4, 2, 3).astype(np.float32)
>>> X.shape
(1, 4, 2, 3)
>>> X
Expand Down
6 changes: 3 additions & 3 deletions chainer/functions/array/permutate.py
Expand Up @@ -107,12 +107,12 @@ def permutate(x, indices, axis=0, inv=False):
.. admonition:: Example
>>> x = np.arange(6).reshape((3, 2)).astype('f')
>>> x = np.arange(6).reshape((3, 2)).astype(np.float32)
>>> x
array([[0., 1.],
[2., 3.],
[4., 5.]], dtype=float32)
>>> indices = np.array([2, 0, 1], 'i')
>>> indices = np.array([2, 0, 1], np.int32)
>>> y = F.permutate(x, indices)
>>> y.data
array([[4., 5.],
Expand All @@ -123,7 +123,7 @@ def permutate(x, indices, axis=0, inv=False):
array([[2., 3.],
[4., 5.],
[0., 1.]], dtype=float32)
>>> indices = np.array([1, 0], 'i')
>>> indices = np.array([1, 0], np.int32)
>>> y = F.permutate(x, indices, axis=1)
>>> y.data
array([[1., 0.],
Expand Down
4 changes: 2 additions & 2 deletions chainer/functions/array/select_item.py
Expand Up @@ -104,8 +104,8 @@ def select_item(x, t):
.. admonition:: Example
>>> x = np.array([[0, 1, 2], [3, 4, 5]], 'f')
>>> t = np.array([0, 2], 'i')
>>> x = np.array([[0, 1, 2], [3, 4, 5]], np.float32)
>>> t = np.array([0, 2], np.int32)
>>> y = F.select_item(x, t)
>>> y.shape
(2,)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/array/separate.py
Expand Up @@ -58,7 +58,7 @@ def separate(x, axis=0):
.. admonition:: Example
>>> x = np.arange(6).reshape((2, 3)).astype('f')
>>> x = np.arange(6).reshape((2, 3)).astype(np.float32)
>>> x
array([[0., 1., 2.],
[3., 4., 5.]], dtype=float32)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/array/space2depth.py
Expand Up @@ -66,7 +66,7 @@ def space2depth(X, r):
.. admonition:: Example
>>> X = np.arange(24).reshape(1, 1, 4, 6).astype('f')
>>> X = np.arange(24).reshape(1, 1, 4, 6).astype(np.float32)
>>> X.shape
(1, 1, 4, 6)
>>> X
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/array/squeeze.py
Expand Up @@ -80,7 +80,7 @@ def squeeze(x, axis=None):
.. admonition:: Example
>>> x = np.array([[[[0, 1, 2]]], [[[3, 4, 5]]]], 'f')
>>> x = np.array([[[[0, 1, 2]]], [[[3, 4, 5]]]], np.float32)
>>> x.shape
(2, 1, 1, 3)
>>> y = F.squeeze(x)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/array/swapaxes.py
Expand Up @@ -40,7 +40,7 @@ def swapaxes(x, axis1, axis2):
.. admonition:: Example
>>> x = np.array([[[0, 1, 2], [3, 4, 5]]], 'f')
>>> x = np.array([[[0, 1, 2], [3, 4, 5]]], np.float32)
>>> x.shape
(1, 2, 3)
>>> y = F.swapaxes(x, axis1=0, axis2=1)
Expand Down
2 changes: 1 addition & 1 deletion chainer/functions/array/transpose.py
Expand Up @@ -45,7 +45,7 @@ def transpose(x, axes=None):
.. admonition:: Example
>>> x = np.array([[[0, 1, 2], [3, 4, 5]]], 'f')
>>> x = np.array([[[0, 1, 2], [3, 4, 5]]], np.float32)
>>> x.shape
(1, 2, 3)
>>> y = F.transpose(x) # reverse the dimensions
Expand Down
4 changes: 2 additions & 2 deletions chainer/functions/array/where.py
Expand Up @@ -73,8 +73,8 @@ def where(condition, x, y):
>>> cond
array([[ True, False],
[False, True]])
>>> x = np.array([[1, 2], [3, 4]], 'f')
>>> y = np.zeros((2, 2), 'f')
>>> x = np.array([[1, 2], [3, 4]], np.float32)
>>> y = np.zeros((2, 2), np.float32)
>>> F.where(cond, x, y).data
array([[1., 0.],
[0., 4.]], dtype=float32)
Expand Down
7 changes: 4 additions & 3 deletions chainer/functions/connection/convolution_2d.py
Expand Up @@ -660,13 +660,14 @@ def convolution_2d(x, W, b=None, stride=1, pad=0, cover_all=False, group=1,
>>> h_i, w_i = 30, 40
>>> h_k, w_k = 10, 10
>>> h_p, w_p = 5, 5
>>> x = np.random.uniform(0, 1, (n, c_i, h_i, w_i)).astype('f')
>>> x = np.random.uniform(0, 1, (n, c_i, h_i, w_i)).astype(np.float32)
>>> x.shape
(10, 3, 30, 40)
>>> W = np.random.uniform(0, 1, (c_o, c_i, h_k, w_k)).astype('f')
>>> W = np.random.uniform(0, 1, (c_o, c_i, h_k, w_k)).\
astype(np.float32)
>>> W.shape
(1, 3, 10, 10)
>>> b = np.random.uniform(0, 1, (c_o,)).astype('f')
>>> b = np.random.uniform(0, 1, (c_o,)).astype(np.float32)
>>> b.shape
(1,)
>>> s_y, s_x = 5, 7
Expand Down
8 changes: 5 additions & 3 deletions chainer/functions/connection/convolution_nd.py
Expand Up @@ -410,13 +410,15 @@ def convolution_nd(x, W, b=None, stride=1, pad=0, cover_all=False):
>>> d1, d2, d3 = 30, 40, 50
>>> k1, k2, k3 = 10, 10, 10
>>> p1, p2, p3 = 5, 5, 5
>>> x = np.random.uniform(0, 1, (n, c_i, d1, d2, d3)).astype('f')
>>> x = np.random.uniform(0, 1, (n, c_i, d1, d2, d3)).\
astype(np.float32)
>>> x.shape
(10, 3, 30, 40, 50)
>>> W = np.random.uniform(0, 1, (c_o, c_i, k1, k2, k3)).astype('f')
>>> W = np.random.uniform(0, 1, (c_o, c_i, k1, k2, k3)).\
astype(np.float32)
>>> W.shape
(1, 3, 10, 10, 10)
>>> b = np.random.uniform(0, 1, (c_o)).astype('f')
>>> b = np.random.uniform(0, 1, (c_o)).astype(np.float32)
>>> b.shape
(1,)
>>> s1, s2, s3 = 2, 4, 6
Expand Down
7 changes: 4 additions & 3 deletions chainer/functions/connection/deconvolution_2d.py
Expand Up @@ -462,13 +462,14 @@ def deconvolution_2d(x, W, b=None, stride=1, pad=0, outsize=None, group=1,
>>> h_i, w_i = 5, 10
>>> h_k, w_k = 10, 10
>>> h_p, w_p = 5, 5
>>> x = np.random.uniform(0, 1, (n, c_i, h_i, w_i)).astype('f')
>>> x = np.random.uniform(0, 1, (n, c_i, h_i, w_i)).astype(np.float32)
>>> x.shape
(10, 1, 5, 10)
>>> W = np.random.uniform(0, 1, (c_i, c_o, h_k, w_k)).astype('f')
>>> W = np.random.uniform(0, 1, (c_i, c_o, h_k, w_k)).\
astype(np.float32)
>>> W.shape
(1, 3, 10, 10)
>>> b = np.random.uniform(0, 1, c_o).astype('f')
>>> b = np.random.uniform(0, 1, c_o).astype(np.float32)
>>> b.shape
(3,)
>>> s_y, s_x = 5, 5
Expand Down
16 changes: 10 additions & 6 deletions chainer/functions/connection/deconvolution_nd.py
Expand Up @@ -385,13 +385,15 @@ def deconvolution_nd(x, W, b=None, stride=1, pad=0, outsize=None):
>>> d1, d2, d3 = 5, 10, 15
>>> k1, k2, k3 = 10, 10, 10
>>> p1, p2, p3 = 5, 5, 5
>>> x = np.random.uniform(0, 1, (n, c_i, d1, d2, d3)).astype('f')
>>> x = np.random.uniform(0, 1, (n, c_i, d1, d2, d3)).\
astype(np.float32)
>>> x.shape
(10, 3, 5, 10, 15)
>>> W = np.random.uniform(0, 1, (c_i, c_o, k1, k2, k3)).astype('f')
>>> W = np.random.uniform(0, 1, (c_i, c_o, k1, k2, k3)).\
astype(np.float32)
>>> W.shape
(3, 1, 10, 10, 10)
>>> b = np.random.uniform(0, 1, (c_o)).astype('f')
>>> b = np.random.uniform(0, 1, (c_o)).astype(np.float32)
>>> b.shape
(1,)
>>> s1, s2, s3 = 2, 4, 6
Expand All @@ -412,13 +414,15 @@ def deconvolution_nd(x, W, b=None, stride=1, pad=0, outsize=None):
>>> d1, d2, d3 = 5, 10, 15
>>> k1, k2, k3 = 10, 10, 10
>>> p1, p2, p3 = 5, 5, 5
>>> x = np.random.uniform(0, 1, (n, c_i, d1, d2, d3)).astype('f')
>>> x = np.random.uniform(0, 1, (n, c_i, d1, d2, d3)).\
astype(np.float32)
>>> x.shape
(10, 3, 5, 10, 15)
>>> W = np.random.uniform(0, 1, (c_i, c_o, k1, k2, k3)).astype('f')
>>> W = np.random.uniform(0, 1, (c_i, c_o, k1, k2, k3)).\
astype(np.float32)
>>> W.shape
(3, 1, 10, 10, 10)
>>> b = np.random.uniform(0, 1, (c_o)).astype('f')
>>> b = np.random.uniform(0, 1, (c_o)).astype(np.float32)
>>> b.shape
(1,)
>>> s1, s2, s3 = 2, 4, 6
Expand Down

0 comments on commit a88982a

Please sign in to comment.