renom ¶
-
class
renom.core.
Grads
( root=None , weight_decay=None ) ¶ -
ベースクラス:
object
Gradsクラス. このクラスは, 各Nodeオブジェクトの勾配を保持する.
Nodeクラスのオブジェクトが持つ
grad
メソッドが実行されると, Gradsクラスのオブジェクトが返される.計算グラフ上の変数'x'に関する勾配を取得したい場合, Gradsオブジェクトの'get'メソッドを使用することができる.
Example
>>> import numpy as np >>> import renom as rm >>> a = rm.Variable(np.random.rand(2, 3)) >>> b = rm.Variable(np.random.rand(2, 3)) >>> c = rm.sum(a + 2*b) >>> grad = c.grad() >>> grad.get(a) # Getting gradient of a. Mul([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> grad.get(b) RMul([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32)
-
get
( node , default=<object object> ) ¶ -
この関数は, 与えられた変数に関する勾配を返す.与えられた変数に関する勾配が存在しない場合, 'None'を返す.
パラメータ: 戻り値: 引数(node)に与えられた変数の勾配, もしくは引数(default)を返す.
戻り値の型:
-
update
( opt=None , models=() ) ¶ -
計算された勾配を用いて, 計算グラフ上の対応する変数を更新する.
Optimizerインスタンスが与えられた場合, 勾配はOptimizerインスタンスによって処理された後, 勾配に対応する変数の更新に使用される.
パラメータ: - opt ( Optimizer ) -- Optimizerオブジェクト
- models -- 勾配による更新対象の変数のリストを与えることができる.もしひとつ以上の変数が与えられた場合, リストに存在する変数のみが更新される.
Example
>>> import numpy as np >>> import renom as rm >>> a = rm.Variable(np.arange(4).reshape(2, 2)) >>> b = rm.Variable(np.arange(4).reshape(2, 2)) >>> print("Before", a) Before [[ 0. 1.] [ 2. 3.]] >>> out = rm.sum(2*a + 3*b) >>> grad = out.grad(models=(a, )) >>> print("Gradient", grad.get(a)) Gradient [[ 2. 2.] [ 2. 2.]] >>> grad.update() >>> print("Updated", a) Updated [[-2. -1.] [ 0. 1.]]
-
-
class
renom.core.
Node
( *args , **kwargs ) ¶ -
ベースクラス:
numpy.ndarray
基底クラス. Numpyのndarrayクラスを継承している.
Example
>>> import numpy as np >>> import renom as rm >>> vx = rm.Variable(np.random.rand(3, 2)) >>> isinstance(vx, rm.Node) True
-
to_cpu
( ) ¶ -
GPUデバイス上のデータをCPUへ転送する.
-
to_gpu
( ) ¶ -
CPU上のデータをGPUデバイスへ転送する.この関数は, Cudaモジュールが使用可能なときのみ有効となる.それ以外の場合, ValueError を投げる.
Example
>>> import numpy as np >>> import renom as rm >>> from renom.cuda import set_cuda_active >>> set_cuda_active(True) >>> a = rm.Variable(np.arange(4).reshape(2, 2)) >>> a.to_gpu() # Sending array to gpu device.
-
as_ndarray
( ) ¶ -
Nodeオブジェクトをndarrayへ変換して返す.
-
release_gpu
( ) ¶ -
GPUデバイス上に確保された行列データを解放する.
-
detach_graph
( ) ¶ -
計算グラフを削除する.
-
reshape
( *shape ) ¶ -
Reshapeされた行列を返す.
パラメータ: shape ( list , int ) -- 行列を引数に与えられた形に, 変形する. 戻り値: 変形された行列 戻り値の型: ( Node ) Example
>>> import numpy as np >>> import renom as rm >>> a = rm.Variable(np.arange(4).reshape(2, 2)) >>> print(a) [[ 0. 1.] [ 2. 3.]] >>> print(a.reshape(-1)) [ 0. 1. 2. 3.] >>> print(a.reshape(1, 4)) [[ 0. 1. 2. 3.]]
-
grad
( initial=None , detach_graph=True , weight_decay=None , **kwargs ) ¶ -
計算グラフを遡り, 各変数の勾配を計算する.
パラメータ:
-
transpose
( *axis ) ¶ -
与えられた引数に基づき, 行列の転置を行う.
パラメータ: axes ( list of ints ) -- えられた番号に従って, 軸を入れ替える. 戻り値: 転置後の行列 戻り値の型: ( Node ) Example
>>> import numpy as np >>> import renom as rm >>> a = rm.Variable(np.arange(4).reshape(2, 2)) >>> print(a) [[ 0. 1.] [ 2. 3.]] >>> print(a.transpose(1, 0)) [[ 0. 2.] [ 1. 3.]]
-
-
class
renom.core.
Variable
( *args , **kwargs ) ¶ -
ベースクラス:
renom.core.basic_node.Node
Variable class.
Variableクラスとして定義された変数について, 勾配計算が実行される.
パラメータ: 引数 weight_decay にNoneが与えられた場合, この行列に対して重み減衰は適用されない.
Gradsオブジェクトのupdateメソッドにおいても, 重み減衰を追加できる引数オプションが存在するが, Variableオブジェクトのweight_decayに与えられた減衰係数が優先される.
Variableの引数weight_decayに対し, 0が与えられた時のみ, Gradsオブジェクトのupdateメソッドの引数weight_decayが有効になる.
以下の表のような優先順で重み減衰が適用される.
Variable Grad Result None <Any> 重み減衰無し 0.3 <Any> 0.3 0 None/0 重み減衰無し 0 0.3 0.3 Example
>>> import numpy as np >>> import renom as rm >>> x = np.array([1. -1]) >>> rm.Variable(x) Variable([ 1., -1.], dtype=float32)
-
class
renom.operation.
Abase
( *args , **kwargs ) ¶
-
class
renom.operation.
Amax
( *args , **kwargs ) ¶ -
行列の指定された軸方向に対し, 最大値を取得する.
パラメータ: Example
>>> import numpy as np >>> import renom as rm >>> # Forward Calculation >>> a = np.arange(4).reshape(2, 2) >>> a [[0 1] [2 3]] >>> rm.amax(a, axis=1) [ 1. 3.] >>> >>> rm.amax(a, axis=0) [ 2. 3.] >>> rm.amax(a, axis=0, keepdims=True) [[ 2. 3.]] >>> >>> # Calculation of differentiation >>> va = rm.Variable(a) >>> out = rm.amax(va) >>> grad = out.grad() >>> grad.get(va) # Getting the gradient of 'va'. [[ 0., 0.], [ 0., 1.]]
-
class
renom.operation.
Amin
( *args , **kwargs ) ¶ -
行列の指定された軸方向に対し, 最小値を取得する.
パラメータ: Example
>>> import numpy as np >>> import renom as rm >>> # Forward Calculation >>> a = np.arange(4).reshape(2, 2) >>> a [[0 1] [2 3]] >>> rm.amin(a, axis=1) [ 0. 2.] >>> >>> rm.amin(a, axis=0) [ 0. 1.] >>> rm.amin(a, axis=0, keepdims=True) [[ 0. 1.]] >>> >>> # Calculation of differentiation >>> va = rm.Variable(a) >>> out = rm.amin(va) >>> grad = out.grad() >>> grad.get(va) # Getting the gradient of 'va'. [[ 1., 0.], [ 0., 0.]]
-
renom.operation.
reshape
( array , shape ) ¶ -
行列の形を変形する.
パラメータ: 戻り値: 変形された行列
戻り値の型: ( Node )
Example
>>> import renom as rm >>> import numpy as np >>> x = rm.Variable(np.arange(6)) >>> x.shape (6,) >>> y = rm.reshape(x, (2, 3)) >>> y.shape (2, 3)
-
class
renom.operation.
sum
( *args , **kwargs ) ¶ -
行列の要素を足し合わせる.引数axisが与えられた場合, その軸に沿って要素を足し合わせる.
パラメータ: 戻り値: 足し合わせた結果
戻り値の型: ( Node )
Example
>>> import numpy as np >>> import renom as rm >>> >>> x = np.random.rand(2, 3) >>> z = rm.sum(x) >>> z sum(3.21392822265625, dtype=float32)
-
class
renom.operation.
dot
( *args , **kwargs ) ¶ -
行列積を実行する.
パラメータ: 戻り値: 行列積の結果
戻り値の型: ( Node )
Example
>>> import numpy as np >>> import renom as rm >>> >>> x = np.random.rand(2, 3) >>> y = np.random.rand(2, 2) >>> z = rm.dot(y, x) >>> z dot([[ 0.10709135, 0.15022227, 0.12853521], [ 0.30557284, 0.32320538, 0.26753256]], dtype=float32)
-
class
renom.operation.
concat
( *args , **kwargs ) ¶ -
二つ以上の行列を, 与えられた軸に沿って結合する.
パラメータ: 戻り値: 行列が結合された結果
戻り値の型: ( Node )
Example
>>> import numpy as np >>> import renom as rm >>> >>> x = np.random.rand(2, 3) >>> y = np.random.rand(2, 2) >>> z = rm.concat(x, y) >>> z.shape (2, 5) >>> z concat([[ 0.56989014, 0.50372809, 0.40573129, 0.17601326, 0.07233092], [ 0.09377897, 0.8510806 , 0.78971916, 0.52481949, 0.06913455]], dtype=float32)
-
class
renom.operation.
where
( *args , **kwargs ) ¶ -
引数conditionに与えられた条件に従い, 引数に与えられた行列aもしくはbの要素を返す.
パラメータ: 戻り値: 条件によって選択された要素を持つ行列
戻り値の型: ( Node )
Example
>>> import numpy as np >>> import renom as rm >>> >>> x = np.random.rand(2, 3) >>> x array([[ 0.56989017, 0.50372811, 0.4057313 ], [ 0.09377897, 0.85108059, 0.78971919]]) >>> z = rm.where(x > 0.5, x, 0) >>> z where([[ 0.56989014, 0.50372809, 0. ], [ 0. , 0.8510806 , 0.78971916]], dtype=float32)
-
class
renom.operation.
sqrt
( *args , **kwargs ) ¶ -
与えられた行列の各要素に対する平方根を計算する.
パラメータ: arg ( Node , ndarray ) -- 行列 戻り値: 平方根計算の結果 戻り値の型: ( Node ) Example
>>> import numpy as np >>> import renom as rm >>> >>> x = np.random.rand(2, 3) >>> x array([[ 0.56989017, 0.50372811, 0.4057313 ], [ 0.09377897, 0.85108059, 0.78971919]]) >>> z = rm.sqrt(x) >>> z sqrt([[ 0.75491071, 0.70973808, 0.6369704 ], [ 0.30623353, 0.92254031, 0.88866144]], dtype=float32)
-
class
renom.operation.
square
( *args , **kwargs ) ¶ -
与えられた行列の各要素に対する二乗を計算する.
パラメータ: arg ( Node , ndarray ) -- 行列 戻り値: 二乗計算の結果 戻り値の型: ( Node )
-
class
renom.operation.
log
( *args , **kwargs ) ¶ -
与えられた行列の各要素の対数を計算する.
パラメータ: arg ( Node , ndarray ) -- 行列 戻り値: 対数計算の結果 戻り値の型: ( Node )
-
class
renom.operation.
exp
( *args , **kwargs ) ¶ -
ネイピア定数の指数乗を計算する.
パラメータ: arg ( Node , ndarray ) -- 行列 戻り値: 与えられた行列の要素によるネイピア数の指数乗を計算する. 戻り値の型: ( Node )
-
class
renom.operation.
amin
( *args , **kwargs ) ¶ -
行列の指定された軸方向に対し, 最小値を取得する.
パラメータ: Example
>>> import numpy as np >>> import renom as rm >>> # Forward Calculation >>> a = np.arange(4).reshape(2, 2) >>> a [[0 1] [2 3]] >>> rm.amin(a, axis=1) [ 0. 2.] >>> >>> rm.amin(a, axis=0) [ 0. 1.] >>> rm.amin(a, axis=0, keepdims=True) [[ 0. 1.]] >>> >>> # Calculation of differentiation >>> va = rm.Variable(a) >>> out = rm.amin(va) >>> grad = out.grad() >>> grad.get(va) # Getting the gradient of 'va'. [[ 1., 0.], [ 0., 0.]]
-
class
renom.operation.
amax
( *args , **kwargs ) ¶ -
行列の指定された軸方向に対し, 最大値を取得する.
パラメータ: Example
>>> import numpy as np >>> import renom as rm >>> # Forward Calculation >>> a = np.arange(4).reshape(2, 2) >>> a [[0 1] [2 3]] >>> rm.amax(a, axis=1) [ 1. 3.] >>> >>> rm.amax(a, axis=0) [ 2. 3.] >>> rm.amax(a, axis=0, keepdims=True) [[ 2. 3.]] >>> >>> # Calculation of differentiation >>> va = rm.Variable(a) >>> out = rm.amax(va) >>> grad = out.grad() >>> grad.get(va) # Getting the gradient of 'va'. [[ 0., 0.], [ 0., 1.]]
-
class
renom.operation.
mean
( *args , **kwargs ) ¶ -
行列の指定された軸方向に対し, 平均値を取得する.
パラメータ: 戻り値: 平均値
戻り値の型: ( Node )
Example
>>> import numpy as np >>> import renom as rm >>> >>> x = np.random.rand(2, 3) >>> z = rm.mean(x) >>> z