【问题标题】:index out of bounds while predicting in keras在 keras 中预测时索引超出范围
【发布时间】:2018-09-14 08:58:43
【问题描述】:

我正在尝试在 keras 中自定义一个特殊的注意力层。但是我很困惑为什么在尝试了很多方法后总是会出现这个错误。

Traceback (most recent call last):
 File "D:/Users/LawLi/PyCharmProjects/fixed_talentDNA/adx.py", line 52, in <module>
   print(model.predict([tensor1, tensor2, indices]))  # (bs1, sl1, sl2)
 File "D:\Users\LawLi\Anaconda3\lib\site-packages\keras\engine\training.py", line 1172, in predict
   steps=steps)
 File "D:\Users\LawLi\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 293, in predict_loop
   ins_batch = slice_arrays(ins, batch_ids)
 File "D:\Users\LawLi\Anaconda3\lib\site-packages\keras\utils\generic_utils.py", line 507, in slice_arrays
   return [None if x is None else x[start] for x in arrays]
 File "D:\Users\LawLi\Anaconda3\lib\site-packages\keras\utils\generic_utils.py", line 507, in <listcomp>
   return [None if x is None else x[start] for x in arrays]
IndexError: index 4 is out of bounds for axis 0 with size 4

这是我关于自定义层的测试代码,代码中包含一个预测示例。可以直接运行。

from keras.layers import *
from keras.models import Model
from keras.utils import to_categorical
from keras.layers.merge import *


class CustomLayer(Layer):
    def __init__(self, **kwargs):
        self.supports_masking = True
        super(CustomLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        assert len(input_shape) == 3
        super(CustomLayer, self).build(input_shape)

    def compute_mask(self, inputs, mask=None):
        return None

    def call(self, x, mask=None):
        tensor1, tensor2, ind = x[0], x[1], x[2]  # (bs1, sl1, wd) (bs2, sl2, wd) (bs1, bs2. sl1, sl2)
        tensor2 = K.permute_dimensions(tensor2, [0, 2, 1])
        align = K.dot(tensor1, tensor2)
        align = K.permute_dimensions(align, [0, 2, 1, 3])  # (bs1, bs2, sl1, sl2)
        align = align + ind
        align = K.max(align, axis=1)
        align = K.sum(align, axis=2)
        align = K.softmax(align, axis=1)
        weighted_ans = tensor1 * K.expand_dims(align, 2)
        return K.sum(weighted_ans, axis=1)

    def compute_output_shape(self, input_shape):
        t1_shape, t2_shape = input_shape[0], input_shape[1]
        return t1_shape[0], t1_shape[1], t1_shape[2]


# model example
t1 = Input(shape=(7, 3))
t2 = Input(batch_shape=(4, 6, 3))
t3 = Input(shape=(4, 7, 6))
output = CustomLayer()([t1, t2, t3])
model = Model([t1, t2, t3], output)

# data example
tensor1 = np.random.rand(10, 7, 3)  # (bs1, sl1, wd)
tensor2 = np.random.rand(4, 6, 3)  # (bs2, sl2, wd)
indices = np.array([0, 1, 3, 2, 0, 1, 2, 2, 3, 1])  # (bs1, 1)
indices = to_categorical(indices, num_classes=4) * 999 - 999  # (bs1, bs2)
indices = np.expand_dims(indices, axis=2)
indices = np.expand_dims(indices, axis=3)
indices = np.repeat(indices, 7, axis=2).repeat(6, axis=3)

print(model.predict([tensor1, tensor2, indices]))  # (bs1, sl1, wd)

谢谢你的帮助。

【问题讨论】:

  • 你能打印indices = to_categorical(indices, num_classes=4)的输出吗?它可能编码为 [1,2,3,4] 而不是您所期望的 [0,1,2,3]。
  • 我只是打印你所说的那一行。输出是一个形状为 (10, 4) 的热矩阵,如下所示:[0. 1. 0. 0.][0. 0. 0. 1.]...[0. 0. 1. 0.][1. 0. 0. 0.]
  • 嗯,如果你在weighted_ans = tensor1 * K.expand_dims(align, 2)之后立即添加print(weighted_ans.shape),它会在第一次调用指令时输出(?, 7, 3),到时候应该还没有确定吗?

标签: python keras deep-learning artificial-intelligence


【解决方案1】:

预测函数不能接受轴 0 大小不同的输入。所以我尝试了另一个名为 predict_on_batch() 的函数

【讨论】:

    猜你喜欢
    • 2016-06-28
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2017-07-06
    • 2020-01-17
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多