You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Keras中拼接不同形状张量:构造指定形式的LSTM输入

Solution for Concatenating Tensors for LSTM Input in Keras

Got it, let's break down how to achieve this in Keras (specifically TensorFlow Keras, the standard implementation now). Your goal is to create a sequence input for LSTM that keeps the first two elements from tensor1, then uses a concatenation of tensor1's third element and tensor2's second element as the third item. Here's how to implement this, both with raw tensor operations and within a Keras model:

1. Raw Tensor Operations (for standalone tensor processing)

If you're working with pre-defined tensors (not building a model graph), you can use TensorFlow's native tensor operations:

import tensorflow as tf

# Assume tensor1 has shape (batch_size, 3, feat_dim1)
# Assume tensor2 has shape (batch_size, 2, feat_dim2)

# Extract the first two time steps from tensor1 (keep 3D shape)
step_0 = tensor1[:, 0:1, :]  # Shape: (batch_size, 1, feat_dim1)
step_1 = tensor1[:, 1:2, :]  # Shape: (batch_size, 1, feat_dim1)

# Extract the third time step from tensor1 and second from tensor2
t1_step2 = tensor1[:, 2:3, :]  # Shape: (batch_size, 1, feat_dim1)
t2_step1 = tensor2[:, 1:2, :]  # Shape: (batch_size, 1, feat_dim2)

# Concatenate along the feature dimension (axis=-1)
step_2 = tf.concat([t1_step2, t2_step1], axis=-1)  # Shape: (batch_size, 1, feat_dim1 + feat_dim2)

# Combine all steps into the final LSTM input sequence
lstm_input = tf.concat([step_0, step_1, step_2], axis=1)  # Shape: (batch_size, 3, feat_dim1 + feat_dim2)

Key Notes:

  • We use slicing like [:, 0:1, :] instead of [:, 0, :] to preserve the 3D tensor shape (batch, timesteps, features), which is required for LSTM input.
  • axis=-1 targets the feature dimension for concatenation, while axis=1 combines the individual time steps into a single sequence.

2. Keras Functional API (for model building)

If you're integrating this into a Keras model (e.g., using the Functional API), use Lambda layers to extract tensor slices and Concatenate layers to combine them:

from tensorflow.keras import layers, Model

# Define input shapes (adjust feat_dim1/feat_dim2 to your actual feature counts)
feat_dim1 = 16
feat_dim2 = 8

input_tensor1 = layers.Input(shape=(3, feat_dim1))
input_tensor2 = layers.Input(shape=(2, feat_dim2))

# Extract individual steps using Lambda layers
step_0 = layers.Lambda(lambda x: x[:, 0:1, :])(input_tensor1)
step_1 = layers.Lambda(lambda x: x[:, 1:2, :])(input_tensor1)
t1_step2 = layers.Lambda(lambda x: x[:, 2:3, :])(input_tensor1)
t2_step1 = layers.Lambda(lambda x: x[:, 1:2, :])(input_tensor2)

# Concatenate features for the third step
step_2 = layers.Concatenate(axis=-1)([t1_step2, t2_step1])

# Combine all steps into the LSTM input sequence
lstm_input = layers.Concatenate(axis=1)([step_0, step_1, step_2])

# Pass to LSTM layer (adjust units to your needs)
lstm_output = layers.LSTM(units=64)(lstm_input)

# Build and compile the model
model = Model(inputs=[input_tensor1, input_tensor2], outputs=lstm_output)
model.compile(optimizer='adam', loss='mse')

This approach ensures the tensor manipulations are part of the model graph, so they'll work seamlessly during training and inference.

内容的提问来源于stack exchange,提问作者user9505772

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:40:21