代码之家  ›  专栏  ›  技术社区  ›  Klaus

高维向量的非线性映射

  •  3
  • Klaus  · 技术社区  · 7 年前

    我正在学习Keras,需要以下方面的帮助。目前,我在列表X和Y中有一系列浮点。我需要做的是使用非线性映射,将每个元素映射到以下等式后面的高维向量。

    pos(i) = tanh(W.[concat(X[i],Y[i]])
    #where W is a learnable weight matrix, concat performs the concatenation and pos(i) is a vector of 16x1. (I'm trying to create 16 channel inputs for a CNN).
    

    我发现上面的Pytorch实现是

    m = nn.linear(2,16)
    input = torch.cat(X[i],Y[i])    
    torch.nn.functional.tanh(m(input))
    

    目前我在努比尝试过海螺和棕褐色,但这似乎不是我想要的。

    你能帮助我使用Keras实现上述功能吗。

    1 回复  |  直到 7 年前
        1
  •  1
  •   parsethis    7 年前

    根据你那里的情况。

    这就是我在keras会做的。我将假设您只想在将输入输入输入到模型中之前将其连接起来。

    所以我们要用numpy来做。笔记

    类似于:

    import numpy as np
    from keras.model import Dense, Model,Input
    X = np.random.rand(100, 1)
    Y = np.random.rand(100, 1)
    y = np.random.rand(100, 16)
    # concatenate along the features in numpy
    XY = np.cancatenate(X, Y, axis=1)
    
    
    # write model
    in = Input(shape=(2, ))
    out = Dense(16, activation='tanh')(in)
    # print(out.shape) (?, 16)
    model = Model(in, out)
    model.compile(loss='mse', optimizer='adam')
    model.fit(XY, y)
    
    
    ....