2016-04-13 56 views
1

當我嘗試運行我的LSTM時出現以下錯誤程序(用於可變長度輸入)。TypeError:scan'scan_fn'....'TensorType(float64,col)'和'TensorType(float64,matrix)'的內部圖形不一致

TypeError: Inconsistency in the inner graph of scan 'scan_fn' : an input and an output are associated with the same recurrent state and should have the same type but have type 'TensorType(float64, col)' and 'TensorType(float64, matrix)' respectively.

我的程序是基於LSTM例如,對於IMDB情感分析問題,這裏:http://deeplearning.net/tutorial/lstm.html。我的數據不是imdb,但它是傳感器數據。

我分享了我的源代碼:lstm_var_length.py和數據:data.npz。 (點擊文件)

從上面的錯誤和一些谷歌搜索讓我明白,我有我的函數中的矢量/矩陣尺寸的問題。下面是其中出現此問題的函數定義:

def lstm_layer(shared_params, input_ex, options): 
""" 
LSTM Layer implementation. (Variable Length inputs) 

Parameters 
---------- 
shared_params: shared model parameters W, U, b etc 
input_ex: input example (say dimension: 36 x 100 i.e 36 features and 100 time units) 
options: Neural Network model options 

Output/returns 
---------------- 
output of each lstm cell [h_0, h_1, ..... , h_t] 
""" 

def slice(param, slice_no, height): 
    return param[slice_no*height : (slice_no+1)*height, :] 

def cell(wxb, ht_1, ct_1): 
    pre_activation = tensor.dot(shared_params['U'], ht_1) 
    pre_activation += wxb 

    height = options['hidden_dim'] 
    ft = tensor.nnet.sigmoid(slice(pre_activation, 0, height)) 
    it = tensor.nnet.sigmoid(slice(pre_activation, 1, height)) 
    c_t = tensor.tanh(slice(pre_activation, 2, height)) 
    ot = tensor.nnet.sigmoid(slice(pre_activation, 3, height)) 

    ct = ft * ct_1 + it * c_t 
    ht = ot * tensor.tanh(ct) 

    return ht, ct 

wxb = tensor.dot(shared_params['W'], input_ex) + shared_params['b'] 
num_frames = input_ex.shape[1] 
result, updates = theano.scan(cell, 
           sequences=[wxb.transpose()], 
           outputs_info=[tensor.alloc(numpy.asarray(0., dtype=floatX), 
                 options['hidden_dim'], 1), 
              tensor.alloc(numpy.asarray(0., dtype=floatX), 
                 options['hidden_dim'], 1)], 
           n_steps=num_frames) 

return result[0] # only ht is needed 


def build_model(shared_params, options): 
""" 
Build the complete neural network model and return the symbolic variables 

Parameters 
---------- 
shared_params: shared, model parameters W, U, b etc 
options: Neural Network model options 

return 
------ 
x, y, f_pred_prob, f_pred, cost 
""" 

x = tensor.matrix(name='x', dtype=floatX) 
y = tensor.iscalar(name='y') # tensor.vector(name='y', dtype=floatX) 

num_frames = x.shape[1] 
# lstm outputs from each cell 
lstm_result = lstm_layer(shared_params, x, options) 
# mean pool from the lstm cell outputs 
pool_result = lstm_result.sum(axis=1)/(1. * num_frames) 
# Softmax/Logistic Regression 
pred = tensor.nnet.softmax(tensor.dot(shared_params['softmax_W'], pool_result) + 
          shared_params['softmax_b']) 
# predicted probability function 
theano.printing.debugprint(pred) 
f_pred_prob = theano.function([x], pred, name='f_pred_prob', mode='DebugMode') # 'DebugMode' <-- Problem seems to occur at this point 
# predicted class 
f_pred = theano.function([x], pred.argmax(axis=0), name='f_pred') 
# cost of the model: -ve log likelihood 
offset = 1e-8 # an offset to prevent log(0) 
cost = -tensor.log(pred[y-1, 0] + offset) # y = 1,2,...n but indexing is 0,1,..(n-1) 

return x, y, f_pred_prob, f_pred, cost 

上述錯誤造成試圖編譯f_pred_prob theano功能時。

異常和調用堆棧低於:

File "/home/inblueswithu/Documents/Theano_Trails/lstm_var_length.py", line 450, in 
    main() 
    File "/home/inblueswithu/Documents/Theano_Trails/lstm_var_length.py", line 447, in main 
    train_lstm(model_options, train, valid) 
File "/home/inblueswithu/Documents/Theano_Trails/lstm_var_length.py", line 314, in train_lstm 
    (x, y, f_pred_prob, f_pred, cost) = build_model(shared_params, options) 
File "/home/inblueswithu/Documents/Theano_Trails/lstm_var_length.py", line 95, in build_model 
    f_pred_prob = theano.function([x], pred, name='f_pred_prob', mode='DebugMode') # 'DebugMode' 
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function.py", line 320, in function 
    output_keys=output_keys) 
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 479, in pfunc 
    output_keys=output_keys) 
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 1777, in orig_function 
    defaults) 
File "/usr/local/lib/python2.7/dist-packages/theano/compile/debugmode.py", line 2571, in create 
    storage_map=storage_map) 
File "/usr/local/lib/python2.7/dist-packages/theano/gof/link.py", line 690, in make_thunk 
    storage_map=storage_map)[:3] 
File "/usr/local/lib/python2.7/dist-packages/theano/compile/debugmode.py", line 1809, in make_all 
    no_recycling) 
File "/usr/local/lib/python2.7/dist-packages/theano/scan_module/scan_op.py", line 730, in make_thunk 
    self.validate_inner_graph() 
File "/usr/local/lib/python2.7/dist-packages/theano/scan_module/scan_op.py", line 249, in validate_inner_graph 
    (self.name, type_input, type_output)) 
TypeError: Inconsistency in the inner graph of scan 'scan_fn' : an input and an output are associated with the same recurrent state and should have the same type but have type 'TensorType(float64, col)' and 'TensorType(float64, matrix)' respectively.

我一直在做的所有調試了一個星期,也沒有找到問題。我懷疑theano.scan中outputs_info的初始化是問題,但是當我刪除第二個維度(1)時,即使在達到f_pred_prob函數(接近lstm_result)之前,我也會收到切片函數的錯誤。我不確定問題出在哪裏。

通過將數據文件放置在與python源文件相同的目錄中,可以簡單地執行此程序,可以重新創建此問題。

請幫我一把。

感謝&問候, inblueswithu

回答

0

我想,我發現這個問題。我不得不重新檢查矩陣的所有維度。我仍然需要仔細檢查我的代碼。一旦完成,我會把新的代碼。

謝謝。

1

使用

outputs_info=[tensor.unbroadcast(tensor.alloc(numpy.asarray(0., dtype=floatX), 
               options['hidden_dim'], 1),1), 
       tensor.unbroadcast(tensor.alloc(numpy.asarray(0., dtype=floatX), 
               options['hidden_dim'], 1),1)] 

代替了原來的outputs_info。

這是因爲tensor.alloc(numpy.asarray(0., dtype=floatX),options['hidden_dim'], 1)的第二個dim是1,那麼theano會自動使其可以廣播,並將張量變量包裝爲col而不是矩陣。這此錯誤消息

TypeError: Inconsistency in the inner graph of scan 'scan_fn' : an input and an output are associated with the same recurrent state and should have the same type but have type 'TensorType(float64, col)' and 'TensorType(float64, matrix)' respectively. 

而且theano.unbroadcast'TensorType(float64, col)'避免了這個問題。