In [1]:
import theano
import theano.tensor as T
from theano import function
# logistic function
x = T.dmatrix('x')
s = 1 / (1 + T.exp(-x))
logistic = function([x], s) # elementwise
logistic([[-2, -1], [1,2]])
Out[1]:
In [2]:
# multiple output
a, b = T.dmatrices('a', 'b')
diff = a - b
abs_diff = abs(diff)
diff_squared = diff ** 2
f = function([a,b], [diff, abs_diff, diff_squared])
f([[1,2], [5,1]], [[10,-2], [3,1]])
Out[2]:
In [3]:
# setting default value
from theano import Param
x, y = T.dscalars('x', 'y')
z = x + y
f = function([x, Param(y, default = 1)], z)
f(33)
Out[3]:
In [4]:
f(31,2)
Out[4]:
In [5]:
# naming paramators
x, y = T.dscalars('x', 'y')
z = x + y
f = function([x, Param(y, default = 1, name = 'y_value')], z)
f(33, y_value = 5)
Out[5]:
In [6]:
# shared variables
# accumulator is a process
# state is an output
from theano import shared
state = shared(0)
inc = T.iscalar('inc')
accumulator = function([inc], state, updates = [(state, state+inc)])
In [7]:
state.get_value()
Out[7]:
In [8]:
accumulator(1)
Out[8]:
In [9]:
state.get_value()
Out[9]:
In [10]:
accumulator(300)
Out[10]:
In [11]:
state.get_value()
Out[11]:
In [12]:
state.set_value(-1)
In [13]:
state.get_value()
Out[13]:
In [14]:
accumulator(3)
Out[14]:
In [15]:
state.get_value()
Out[15]:
In [16]:
decrementor = function([inc], state, updates=[(state, state-inc)])
decrementor(2)
Out[16]:
In [17]:
state.get_value()
Out[17]:
In [18]:
# givens paramator
up_state = state * 2 + inc
foo = T.scalar(dtype = state.dtype)
skip_shared = function([inc, foo], up_state, givens = [(state, foo)])
skip_shared(1,3)
Out[18]:
In [19]:
skip_shared(1,3)
Out[19]:
In [20]:
# old state is still there and we do not use it
state.get_value()
Out[20]:
In [21]:
accumulator(300)
Out[21]:
In [22]:
state.get_value()
Out[22]:
In [23]:
# output is the same because we use foo instead of state
skip_shared(1,3)
Out[23]:
In [3]:
# random variable
# basic example
from theano.tensor.shared_randomstreams import RandomStreams
from theano import function
srng = RandomStreams(seed = 234)
rv_u = srng.uniform((2,2))
rv_n = srng.normal((2,2))
f = function([], rv_u)
g = function([], rv_n, no_default_updates=True)
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)
In [4]:
# fはアップデートされるので毎回違う
f_val1 = f()
f_val1
Out[4]:
In [5]:
f_val2 = f()
f_val2
Out[5]:
In [6]:
# gはアップデートしないようにしているので毎回同じ
g_val1 = g()
g_val1
Out[6]:
In [7]:
g_val2 = g()
g_val2
Out[7]:
In [8]:
nearly_zeros()
Out[8]:
In [2]:
# numpy
# randn
import numpy
a = numpy.random
a.randn(2)
Out[2]:
In [26]:
# randint
# max, matrix shape
a.randint(5, size = (3,4))
Out[26]:
In [6]:
# こんなのあるよね
a = 20 > 9
a
Out[6]:
In [1]:
# example
# logistic regression
# 教師あり学習ってやつ
# まだ微妙にわかってないので、後で戻ってこよう
import numpy
import theano
import theano.tensor as T
rng = numpy.random
N = 400
feats = 784
D = (rng.randn(N, feats), rng.randint(size = N, low = 0, high = 2))
# サンプル数400で、変数が784個ある(全てが標準正規分布に従って出力される)
# 各サンプルに対して結果変数として0,1のどちらかが報告されている
# この二つの情報をまとめたものがD(tuple)
training_steps = 10000
x = T.matrix("x") # 変数を入れる箱
y = T.vector("y") # 結果を入れる箱
w = theano.shared(rng.randn(feats))
b = theano.shared(0., name = "b") # bは定数項?
print("Initial Value:")
print(w.get_value())
print(b.get_value())
# wは係数
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) # dot is inner product
prediction = p_1 > 0.5 # 論理値として、p_1が0.5よりも大きいと1を出力する(振り分け)
xent = -y * T.log(p_1) - (1 - y) * T.log(1 - p_1) # 尤度関数の逆
# 下のcost functionはgenralized logistic regressionのコスト関数
cost = xent.mean() + 0.01 * (w ** 2).sum()
gw, gb = T.grad(cost, [w,b]) # gradient descentをやるよ
# compile
train = theano.function(inputs = [x, y], outputs = [prediction, xent]
,updates = ((w, w - 0.1 * gw), (b, b - 0.1 * gb))) # 0.1 is a step
predict = theano.function(inputs = [x], outputs = prediction)
# train
for i in range(training_steps):
pred, err = train(D[0], D[1]) # pred とerrなんていないんだけど
print("Final model:") # 推定されたモデル
print(w.get_value())
print(b.get_value())
print("target values for D:") # 本当の結果
print(D[1])
print("prediction on D:") # 推測されたモデルに基づいて、与えられた変数の値を元に2項判別した結果
print(predict(D[0]))
In [30]:
len(D[1])
Out[30]:
In [ ]: