简体   繁体   English

将单位矩阵连接到每个向量

[英]Concatenate identity matrix to each vector

I want to modify my input by adding several different suffixes to the input vectors.我想通过向输入向量添加几个不同的后缀来修改我的输入。 For example, if the (single) input is [1, 5, 9, 3] I want to create three vectors (stored as matrix) like this:例如,如果(单个)输入是[1, 5, 9, 3]我想像这样创建三个向量(存储为矩阵):

[[1, 5, 9, 3, 1, 0, 0],
 [1, 5, 9, 3, 0, 1, 0],
 [1, 5, 9, 3, 0, 0, 1]]

Of course, this is just one observation so the input to the model is (None, 4) in this case.当然,这只是一个观察结果,因此在这种情况下(None, 4)模型的输入是(None, 4) The simple way is to prepare the input data somewhere else (numpy most probably) and adjust the shape of input accordingly.简单的方法是在其他地方准备输入数据(最有可能是numpy)并相应地调整输入的形状。 That I can do but I would prefer doing it inside TensorFlow/Keras.我可以做,但我更喜欢在 TensorFlow/Keras 中做。

I have isolated the problem into this code:我已将此问题隔离到此代码中:

import keras.backend as K
from keras import Input, Model
from keras.layers import Lambda


def build_model(dim_input: int, dim_eye: int):
    input = Input((dim_input,))
    concat = Lambda(lambda x: concat_eye(x, dim_input, dim_eye))(input)
    return Model(inputs=[input], outputs=[concat])


def concat_eye(x, dim_input, dim_eye):
    x = K.reshape(x, (-1, 1, dim_input))
    x = K.repeat_elements(x, dim_eye, axis=1)
    eye = K.expand_dims(K.eye(dim_eye), axis=0)
    eye = K.tile(eye, (-1, 1, 1))
    out = K.concatenate([x, eye], axis=2)
    return out


def main():
    import numpy as np

    n = 100
    dim_input = 20
    dim_eye = 3

    model = build_model(dim_input, dim_eye)
    model.compile(optimizer='sgd', loss='mean_squared_error')

    x_train = np.zeros((n, dim_input))
    y_train = np.zeros((n, dim_eye, dim_eye + dim_input))
    model.fit(x_train, y_train)


if __name__ == '__main__':
    main()

The problem seems to be in the -1 in shape argument in tile function.问题似乎出在tile函数中的shape参数-1中。 I tried to replace it with 1 and None .我试图用1None替换它。 Each has its own error:每个都有自己的错误:

  • -1 : error during model.fit -1model.fit期间model.fit

     tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected multiples[0] >= 0, but got -1
  • 1 : error duting model.fit 1 : 误差model.fit

     tensorflow.python.framework.errors_impl.InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [32,3,20] vs. shape[1] = [1,3,3]
  • None : error during build_model : None :在build_model期间build_model

     Failed to convert object of type <class 'tuple'> to Tensor. Contents: (None, 1, 1). Consider casting elements to a supported type.

You need to use K.shape() instead to get the symbolic shape of input tensor.您需要使用K.shape()来获取输入张量的符号形状 That's because the batch size is None and therefore passing K.int_shape(x)[0] or None or -1 as a part of the second argument of K.tile() would not work:这是因为批量大小为None ,因此将K.int_shape(x)[0]None-1作为K.tile()的第二个参数的K.tile()将不起作用:

eye = K.tile(eye, (K.shape(x)[0], 1, 1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM