TypeError: 'float' object cannot be interpreted as an integer


一. 问题背景

       最近想实践“字嵌入+LSTM+CRF”的分词方法,由于比较懒散, 没有找到字向量的资源,然后,就自己用word2vec和知乎的一次评测数据训练了一份字向量。然后,在搭建LSTM的过程中,执行一下代码时:

  1.  
    data = np.array(data)
  2.  
    data = data.reshape((100/2, 16, 8))

        遇到了这个问题:

TypeError: 'float' object cannot be interpreted as an integer

       emmm,这是咋回事, 100/2明明是整数啊

二. 原因

       后来在网上查阅了一些资料,发现原来是python3的问题, python2是不存在这种问题滴,参考的文章链接请看参考

       具体是怎么回事呢?

       原来是这样的,在python2中, '整数 / 整数 = 整数',以上面的 100 / 2 就会等于 50, 并且是整数。

       而在python3中, ‘整数/整数 = 浮点数’, 也就是100 / 2 = 50.0, 不过,使用 '//'就可以达到原python2中'/'的效果。

三. 解决方案

       既然知道原因了, 解决方案就有很多了, 可以进行强制转换,把浮点型转变成整型,也可以直接使用'//'进行运算。

四. 参考

https://blog.csdn.net/amoscn/article/details/78321137

感谢大大的博客。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 'float' object cannot be interpreted as an integer python error: TypeError: cannot serialize '_io.TextIOWrapper' object TypeError: cannot unpack non-iterable NoneType object问题 关于Object数组强转成Integer数组的问题:Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#' TypeError: cannot use a string pattern on a bytes-like object的解决办法 bootstrap导航条报错 Uncaught TypeError: Cannot convert object to primitive value python--报错:TypeError: cannot unpack non-iterable NoneType object TypeError: 'NoneType' object is not subscriptable TypeError: Object(…) is not a function
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM