I figured it out. Hope this helps other newbie like myself oneday.

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




#linear regression

#data gen

d_set=np.random.uniform(-100.,100.,(50,5)).astype(theano.config.floatX)
w_ans=np.random.uniform(-10.,10,(5,2)).astype(theano.config.floatX)
b1_real=5
b2_real=9
b1=np.random.normal(5.,1.)
b2=np.random.normal(9.,1.)
b=np.asarray([b1,b2]).astype(theano.config.floatX)

#stack bias for addition
tmpb=b
for i in range(d_set.shape[0]-1):
    tmpb=np.concatenate((tmpb,b))
tmpb=np.asarray(tmpb)

ansr=np.dot(d_set,w_ans).astype(theano.config.floatX)



d_setT=theano.shared(d_set)
ansrT=theano.shared(ansr)


W=theano.shared(np.zeros((5,2),dtype=theano.config.floatX),'W')
b=theano.shared(np.zeros((1,2),dtype=theano.config.floatX),'b')


def forward(a,b,c):
return T.dot(a,b)+c


def run_once(in_X, in_Y, WW, bb):
    predicted_result=forward(in_X,WW,bb)
    c=T.sqr(in_Y-predicted_result).sum()
    grad_W=T.grad(cost=c,wrt=WW)
    grad_b=T.grad(cost=c,wrt=bb)
    W=WW-(0.00001*grad_W)
    b=bb-(0.00001*grad_b)
    return [W,b,c]
    
def run_one_epoch(Ww, bb):
    [W,b,coost],_=theano.scan(fn=run_once, sequences=[d_setT, ansrT], 
outputs_info=[Ww,bb,None])
    return [coost[-1], W[-1], b[-1]]


num_epoch=T.iscalar()
[cost_at_end_of_each_epoch, W, 
b],_=theano.scan(fn=run_one_epoch,n_steps=num_epoch, 
outputs_info=[None,W,b])
multiple_epoch=theano.function([num_epoch],cost_at_end_of_each_epoch)
final_results=multiple_epoch(100)

print final_results


On Tuesday, September 27, 2016 at 9:10:37 AM UTC+2, Sungmin Aum wrote:
>
> Hello,
>
> I looked through tutorials and wanted to experiment with nested scan. 
> However, it seems I cannot figure out how to use just simple scan properly, 
> despite digging through internet for hours.
> Could anyone please give me some pointer?
> Thank you.
>
> Below is the code  and error message
> =============
> import theano
> import theano.tensor as T 
> import numpy as np 
>
> #linear regression
>
> #data gen
>
> d_set=np.random.uniform(-100.,100.,(50,5)).astype(theano.config.floatX)
> w_ans=np.random.uniform(-10.,10,(5,2)).astype(theano.config.floatX)
> b1_real=5
> b2_real=9
> b1=np.random.normal(5.,1.)
> b2=np.random.normal(9.,1.)
> b=np.asarray([b1,b2]).astype(theano.config.floatX)
>
> #stack bias for addition
> tmpb=b
> for i in range(d_set.shape[0]-1):
>     tmpb=np.concatenate((tmpb,b))
> tmpb=np.asarray(tmpb)
>
> ansr=np.dot(d_set,w_ans)+b
>
>
> #symb var init
> X=T.fmatrix('X')
> Y=T.fmatrix('Y')
> x=T.fvector('x')
> ans=T.fvector('target')
>
>
> W=theano.shared(np.zeros((5,2),dtype=theano.config.floatX),'W')
> b=theano.shared(np.zeros((1,2),dtype=theano.config.floatX),'b')
>
> model=T.dot(x,W)+b
>
> cost=T.sqr(ans-model).sum()
>
> grad_W=T.grad(cost=cost,wrt=W)
> grad_b=T.grad(cost=cost,wrt=b)
> W_up=W-(0.00001*grad_W)
> b_up=b-(0.00001*grad_b)
> updates=[(W,W_up),(b,b_up)]
>
>
> f=theano.function([x,ans],cost,updates=updates)
>
> '''
> def run_epoch(x,y):
>     for i in xrange(50):
>         output=f(x[i],y[i])
>         #print i
>         if i==49:
>             print output
> for i in xrange(100):
>     run_epoch(d_set,ansr)
> '''
> #above worked, now trying to do same with scan, but doesn't work
>
>
> def run_once(in_X,in_Y):
>     c=f(in_X,in_Y)
>     print c
>     return c
>     
> def scan_run_epoch(x,y):
>     o_val,_=theano.scan(fn=run_once,sequences=[X,Y],n_steps=X.shape[0])
>     return o_val[-1]
>
> for i in xrange(100):
>     scan_run_epoch(d_set,ansr)
>
>
>
> ===============
> Using gpu device 0: GeForce GTX 750 Ti (CNMeM is disabled, cuDNN 5103)
> Traceback (most recent call last):
> File "d:\Lib\theano_test.py", line 73, in <module>
> scan_run_epoch(d_set,ansr)
> File "d:\Lib\theano_test.py", line 69, in scan_run_epoch
> o_val,_=theano.scan(fn=run_once,sequences=[X,Y],n_steps=X.shape[0])
> File 
> "C:\SciSoft\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\lib\site-packages\theano-0.9.0.dev2-py2.7.egg\theano\scan_module\scan.py",
>  
> line 761, in scan
> condition, outputs, updates = scan_utils.get_updates_and_outputs(fn(*args))
> File "d:\Lib\theano_test.py", line 64, in run_once
> c=f(in_X,in_Y)
> File 
> "C:\SciSoft\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\lib\site-packages\theano-0.9.0.dev2-py2.7.egg\theano\compile\function_module.py",
>  
> line 788, in __call__
> allow_downcast=s.allow_downcast)
> File 
> "C:\SciSoft\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\lib\site-packages\theano-0.9.0.dev2-py2.7.egg\theano\tensor\type.py",
>  
> line 87, in filter
> 'Expected an array-like object, but found a Variable: '
> TypeError: ('Bad input argument with name "X[t]" to theano function with 
> name "d:\\Lib\\theano_test.py:48" at index 0 (0-based)', 'Expected an 
> array-like object, but found a Variable: maybe you are trying to call a 
> function on a (possibly shared) variable instead of a numeric array?')
>
>
>
>
>
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to