LoginSignup
2
2

More than 5 years have passed since last update.

Theanoにおけるクラスの使い方

Posted at

Theanoにおけるクラスの使い方

deeplearning.netなどでは、Theanoを使って共有変数と式シンボルをクラス変数に持つクラスを定義しています。

このクラスの挙動について確認していきましょう。

ここはシンプルに、ひとつずつの共有変数と式シンボルを持つクラスを定義して、クラス変数を活用してみます。

import numpy as np
import theano
import theano.tensor as T

class simple(object):

    def __init__(self, input):

        self.vector = theano.shared(np.array([1, 2, 3], dtype="float64"))

        self.formula = self.vector * input

x = T.iscalar('x')

s = simple(x)

func = theano.function(inputs=[x], outputs=s.formula)

func(5)
出力結果
array([  5.,  10.,  15.])

クラスを定義してからの挙動が少し複雑なので、まとめてみます。

  1. 変数のシンボルを定義
  2. インスタンスを作成する。初期化時にシンボルを引数に取って、クラス変数の式シンボルに渡す
  3. クラス(インスタンス)変数の式シンボルを呼び出して、関数を定義する
  4. 関数を実行

クラス内の記述に式シンボルを含んでいるのがポイントというか、複雑さの原因でしょうか。

式シンボルは直接呼び出して値を確認することも出来ません。

関数化して具体値を入れてやらないと挙動がわからないあたりも、難しいところです。

このような形で、Theanoのクラスを使うことが出来ます。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2