theano
에 scan
기능을 더 잘 이해하려고 노력하고 있습니다. 제 생각에이 코드는 document에 기반한 for
루프처럼 동작합니다. 선형 회귀를 수행 할 때 무게와 편향을 찾기 위해 아주 간단한 작업 예제를 만들었습니다.Theano 선형 회귀에서`for` 루프 대신에`scan`을 사용합니다
#### Libraries
# Third Party Libraries
import numpy as np
import theano
import theano.tensor as T
# not intended for mini-batch
def gen_data(num_points=50, slope=1, bias=10, x_max=50):
f = lambda z: slope * z + bias
x = np.zeros(shape=(num_points), dtype=theano.config.floatX)
y = np.zeros(shape=(num_points), dtype=theano.config.floatX)
for i in range(num_points):
x_temp = np.random.uniform()*x_max
x[i] = x_temp
y[i] = f(x_temp) + np.random.normal(scale=3.0)
return (x, y)
#############################################################
#############################################################
train_x, train_y = gen_data(num_points=50, slope=2, bias=5)
epochs = 50
# Declaring variable
learn_rate = T.scalar(name='learn_rate', dtype=theano.config.floatX)
x = T.vector(name='x', dtype=theano.config.floatX)
y = T.vector(name='y', dtype=theano.config.floatX)
# Variables that will be updated
theta = theano.shared(np.random.rand(), name='theta')
bias = theano.shared(np.random.rand(), name='bias')
hyp = T.dot(theta, x) + bias
cost = T.mean((hyp - y)**2)/2
f_cost = theano.function(inputs=[x, y], outputs=cost)
grad_t, grad_b = T.grad(cost, [theta, bias])
train = theano.function(inputs=[x, y, learn_rate], outputs=cost,
updates=((theta, theta-learn_rate*grad_t),
(bias, bias-learn_rate*grad_b)))
print('weight: {}, bias: {}'.format(theta.get_value(), bias.get_value()))
for i in range(epochs): # Try changing this to a `scan`
train(train_x, train_y, 0.001)
print('------------------------------')
print('weight: {}, bias: {}'.format(theta.get_value(), bias.get_value()))
나는 theano.scan
기능이 for
루프를 변경하려면,하지만이 만든 모든 시도는 다음 후 1 오류 메시지가 나왔고.