Josie
Josie

Reputation: 131

Theano: ifelse TypeError

I am trying to run the script lstm_ptb.py but it is throwing a TypeError for the following line:

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

This is what this line is trying to achieve:

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

It says:

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

How to resolve the error please? Thanks

Upvotes: 3

Views: 600

Answers (1)

malioboro
malioboro

Reputation: 3281

Your problem caused by 1. in else part. By default it assigned as float32 type. You just need to convert it:

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

or convert the max_grad_norm/norm_gparams value:

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

so both value have the same type

Upvotes: 1

Related Questions