对高维向量的非线性映射

时间:2022-09-11 23:46:13

I am learning Keras and need help on the following. I currently have a sequence of floats in lists X and Y. What I need to do is to have a non-linear mapping to map each element to a vector of higher dimension following the below equation.

我正在学习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).

I found that Pytorch implementation for the above is

我发现上面的Pytorch实现是

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

Currently I've tried the concat and tanh in numpy and it seems that is not what I want here.

目前我已经尝试了在numpy的concat和tanh,这似乎不是我想要的。

Can you help me implement the above using Keras.

你能帮我用Keras实现上面的操作吗?

1 个解决方案

#1


1  

Based on what you have there.

基于你所拥有的。

This is what I would do in keras. Im going to assume that you just want your to concatenate your inputs before you feed them into the model.

这就是我在喀纳斯要做的事。我将假设您只是希望在将输入连接到模型之前将输入连接起来。

So we'll do it with numpy. Note

我们用numpy来做。请注意

something like :

喜欢的东西:

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)


....

#1


1  

Based on what you have there.

基于你所拥有的。

This is what I would do in keras. Im going to assume that you just want your to concatenate your inputs before you feed them into the model.

这就是我在喀纳斯要做的事。我将假设您只是希望在将输入连接到模型之前将输入连接起来。

So we'll do it with numpy. Note

我们用numpy来做。请注意

something like :

喜欢的东西:

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)


....