python - Sample_weights Keras 模型 - IndexError:数组索引过多
问题描述
我有一个相当不平衡的数据集,我想在其中对一些数据进行不同的权衡,以便使用 Keras 实现我的神经网络。
我发现我可以使用 sample_weights 。
我的代码如下所示:
sample_weight = np.ones(shape=(len(y_train),))
sample_weight[y_train < 1] = 2.0
history = model.fit(x_train, y_train, batch_size=32, sample_weight=sample_weight, epochs=100, validation_data=(x_val, y_val))
但它给了我第 2 行的以下错误: IndexError: too many indices for array
如果我打印我的 y_train,它看起来像这样:
Ertrag
41799 0.979252
48595 1.000000
50681 1.000000
51678 1.000000
4896 1.000000
是因为y_train中的索引列吗?
提前致谢!
解决方案
该错误可能是由 y_train 和 sample_weight 的维度差异引起的。以下是故障排除的想法:
- 打印并检查 y_train (
len(y_train)
) 的长度,看看返回的形状是否符合您的预期 len(sample_weight)
打印并检查 sample_weight ( )的长度- (1)和(2)的输出应该相同
sample_weight[y_train < 1] = 2.0
才能工作
推荐阅读
- apache-kafka - 使用 Kafka Connect 读取 AVRO 编码消息(由 KSQL 流创建)时出现问题
- python - ctypes LoadLibrary() 在哪里搜索 OS X 上的库?
- sql - 多对多关系有什么问题?
- sql - 替换 SQL Select 语句中的返回值
- python - 在 Python 中加密和解密字符串,以匹配旧的 VB.NET 代码
- mysql - 存储过程试图插入两个表:错误 1136
- sql - SQL Oracle 代码 - 没有获得唯一值
- javascript - Gatsby 热重载不会在 Hello World 入门项目中重载
- python - 是否有 Python 函数来刷新仪表板应用程序?
- ruby-on-rails - 从数组中删除键并使其持久的最快方法