我正在尝试运行脚本 lstm_ptb.py但它为以下行抛出 TypeError:
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 theelse
branch
请问如何解决这个错误?谢谢
最佳答案
您的问题是由 else
部分的 1.
引起的。默认情况下,它分配为 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.)
所以两个值具有相同的类型
关于python - Theano: ifelse 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38254359/