2016-07-07 67 views
2

我試圖運行該腳本lstm_ptb.py但它拋出一個TypeError以下行:Theano:ifelse類型錯誤

shrink_factor = ifelse(T.gt(norm_gparams,max_grad_norm),max_grad_norm/norm_gparams,1.) 

這是該行正在努力實現:

if norm_gparams > max_grad_norm: 
    shrink_factor = max_grad_norm/norm_gparams 
else: 
    shrink_factor = 1. 

它說:

TypeError: The two branches should have identical types, but they are TensorType(float64, scalar) and TensorType(float32, scalar) respectively. This error could be raised if for example you provided a one element list on the then branch but a tensor on the else branch

如何解決錯誤請?謝謝

回答

0

您的問題引起1.else部分。默認情況下,它被指定爲float32類型。你只需要將其轉換:

shrink_factor = ifelse(T.gt(norm_gparams,max_grad_norm),max_grad_norm/norm_gparams,np.float64(1.)) 

或轉換max_grad_norm/norm_gparams值:

shrink_factor = ifelse(T.gt(norm_gparams,max_grad_norm),(max_grad_norm/norm_gparams).astype('float32'),1.) 

所以兩者值具有相同類型的