简体   繁体   中英

What should I do for using theano scan in deep Learning

I want to use then scan for a tensor3 calculation in my DNN, we can say this tensor3 is a 3D matrix, I want each layer of this matrix go through the T.nnet.softmax(T.dot(v, W)+b) calculation, the W , and b should be the fixed, so I could do the update for later. The following is my coding:

XS = T.tensor3("XS") 
W = T.matrix(name="W")
b = T.vector(name="b") 

results, updates = theano.scan(lambda XS: T.nnet.softmax(T.dot(XS,W) + b),  
               sequences=[XS],  
               outputs_info=None)

result = results  
Mutiply = theano.function(inputs=[XS, W, b], outputs=[result]) 

#initialization of output of layer2
w_o = init_weights((1089, 10))
b_o = init_weights((10,))

myFile_data = h5py.File('/Users/XIN/Masterthesis/Torch_Script/mnist-cluttered/train_data.h5', 'r')
myFile_label= h5py.File('/Users/XIN/Masterthesis/Torch_Script/mnist-cluttered/train_target.h5', 'r')
data = myFile_data['data'][...]
label = myFile_label['target'][...]
data = data.reshape(100000, 10000) 
trX = data
trY = label
X_s = downsampling(trX)
X_nine = load_data_train(trX, trX.shape[0], 9)
X_nine = X_nine.transpose((((2,0,1))))

p_x_nine = Mutiply(X_nine, w_o, b_o)[0]

but the results show this error:

runfile('/Users/XIN/Masterthesis/keras/thean_scan_test.py', wdir='/Users/XIN/Masterthesis/keras')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/XIN/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)
  File "/Users/XIN/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
    builtins.execfile(filename, *where)
  File "/Users/XIN/Masterthesis/keras/thean_scan_test.py", line 115, in <module>
    p_x_nine = Mutiply(X_nine, w_o, b_o)[0]
  File "/Users/XIN/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 786, in __call__
    allow_downcast=s.allow_downcast)
  File "/Users/XIN/anaconda/lib/python2.7/site-packages/theano/tensor/type.py", line 86, in filter
    'Expected an array-like object, but found a Variable: '
TypeError: ('Bad input argument to theano function with name "/Users/XIN/Masterthesis/keras/thean_scan_test.py:92"  at index 1(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?')

I check the X_nine type is: np array but the w_o , and b_o is theano type. So what should I do to modify this code.

get the solution by my mentor, coding it like following:

XS = T.tensor3("XS") 
w_o = init_weights((1089, 10))
b_o = init_weights((10,))

results, updates = theano.scan(lambda XS: T.nnet.softmax(T.dot(XS,w_o) + b_o),  
                               sequences=[XS],  
                               outputs_info=None)

result = results  
Mutiply = theano.function(inputs=[XS], outputs=[result]) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM