【发布时间】:2022-01-13 09:39:56
【问题描述】:
我有一个 5 维时间序列的输入:
a = [[8,3],[2] , [4,5],[1], [9,1],[2]...]#total 100 timestamps. For each element, dims 0,1 are numerical data and dim 2 is a numerical encoding of a category. This is per sample, 3200 samples
该类别有 3 个可能的值 (0,1,2)
我想构建一个神经网络,使最后一个维度(类别)通过输出大小为 8 的嵌入层,然后连接回前两个维度(数字数据)。
所以,这将是这样的:
input1 = keras.layers.Input(shape=(2,)) #the numerical features
input2 = keras.layers.Input(shape=(1,)) #the encoding of the categories. this part will be embedded to 5 dims
x2 = Embedding(input_dim=1, output_dim = 8)(input2) #apply it to every timestamp and take only dim 3, so [2],[1], [2]
x = concatenate([input1,x2]) #will get 10 dims at each timepoint, still 100 timepoints
x = LSTM(units=24)(x) #the input has 10 dims/features at each timepoint, total 100 timepoints per sample
x = Dense(1, activation='sigmoid')(x)
model = Model(inputs=[input1, input2] , outputs=[x]) #input1 is 1D vec of the width 2 , input2 is 1D vec with the width 1 and it is going through the embedding
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['acc']
)
我该怎么做? (最好在 keras 中)? 我的问题是如何将嵌入应用到每个时间点? 意思是,如果我有 1000 个时间点,每个时间点有 3 个昏暗,我需要将其转换为 1000 个时间点,每个时间点有 8 个昏暗(嵌入层应该将 input2 从(1000X1)转换为(1000X8)
【问题讨论】:
-
@AloneTogether 是的(我还在帖子中添加了另一层,因为我希望它们在嵌入后一起进入 LSTM)
-
@AloneTogether 我有 3200 个样本,每个都是 100X5 的 ndarray
-
@AloneTogether 也许我的解释很差,但我的意思是最后 3 个特征将一起进入嵌入,并将转换为 8 个特征,这些特征是这 3 个的嵌入。因此,例如,如果嵌入层是e,所以e([8,6,3]可以是[1,2,3,4,5,6,7,8]
-
对不起,但正如 tensorflow 文档明确指出的那样,嵌入不是将形状
(1000, 3)转换为(1000, 8)的正确层。请看@Andrew Wei 的回答。但请注意,那里没有发生任何类型的编码!但是你之前做的编码不正确吗? -
@pythonic833 所以我不确定解决方案是什么 - 我不能放置 Dense 层,因为每个时间戳都会有不同的参数集,所以它没有意义。我能做什么?
标签: python tensorflow keras deep-learning neural-network