python - Theano: ifelse 类型错误

标签 python theano

我正在尝试运行脚本 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 the else 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/

相关文章:

python - 获取 Firestore 集合大小

python - 无法导入 moviepy.editor

image-processing - 当测试和训练数据集来自不同来源时,为什么测试准确性保持不变并且二元分类中的准确性不会增加

python - 简单千层面网络输出很慢

machine-learning - 神经网络模型架构每像素分类

python - 如何继承multiprocessing.Pipe?

python - Pandas 在任何地方造句

python - 可以比较字符串格式的日期时间还是应该将它们转换为日期时间对象?

python - 如何使用 keras RNN 对数据集中的文本进行分类?

theano - 从多个向量创建矩阵