Keras中拼接不同形状张量:构造指定形式的LSTM输入
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=-1targets the feature dimension for concatenation, whileaxis=1combines 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

