python - Python:类型错误'float'对象不可迭代
问题描述
我的代码是:
for ep in range(10):
for x, y in tqdm(train_iterator.gen_batches(batch_size=64,
data_type="train")):
x_embed = embedder(tokenizer(str_lower(x)))
y_onehot = onehotter(classes_vocab(y))
cls.train_on_batch(x_embed, y_onehot)
结果:
<ipython-input-30-3f8c38399ce9> in <module>()
2 for x, y in tqdm(train_iterator.gen_batches(batch_size=64,
3 data_type="train")):
----> 4 x_embed = embedder(tokenizer(str_lower(x)))
5 y_onehot = onehotter(classes_vocab(y))
6 cls.train_on_batch(x_embed, y_onehot)
1 frames
/usr/local/lib/python3.6/dist-packages/deeppavlov/models/preprocessors/str_lower.py in str_lower(batch)
31 return batch.lower()
32 else:
---> 33 return list(map(str_lower, batch))
TypeError: 'float' object is not iterable
我试图将其更改为 ep = int [float] 但这也不起作用。
解决方案
str_lower()
将字符串作为参数或可迭代类型并调用.lower()
它。但是在您的代码x
中是浮点类型。因此,无论何时作为参数调用它都会返回此错误list()
:x
>>> list(3.14)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
list(3.14)
TypeError: 'float' object is not iterable
所以你要么想要:
- 确保
x
是一个字符串 - 不打电话
str_lower(x)
推荐阅读
- angular - 如何从主机外部配置子组件列表?
- android - 我可以将共享首选项存储到 Android 应用中的缓存目录吗?
- node.js - 使用 Heroku 进行 SendGrid 域身份验证
- java - 为什么我无法解码来自具有 Java 环境的 Android 设备的字符串?
- javascript - React 流程图:自动布局
- ignite - Apache Ignite .Net 生成 QueryEntity
- python - 如何获取使用 ImageDataGenerator 为双输入 CNN 模型构建的数据集的标签?
- node.js - 使用一个命令为 discord.js 中的所有通道设置慢模式
- sql - 如何在 SQL 查询中使用基于布尔数据类型的 LIKE 运算符?
- java - 计算网格中的方块