如何在TensorFlow的NumpyReader中插入多变量字典?
Multivariate Setup for
tf.contrib.timeseries.NumpyReader Got it, let's walk through the multivariate case since you already have the univariate implementation working. The core idea stays similar—you just need to adjust how you structure your VALUES tensor to hold multiple features per time step, while keeping TIMES as a 1D vector like before.
First, let's recap your univariate code to set the context (I filled in the missing timestamp piece for clarity):
import numpy as np from tensorflow.contrib.timeseries.python.timeseries import NumpyReader status = [] time = [] # sensorstatus为列表提供数据 for ss in sensorstatus: status.append(int(ss.status)) time.append(ss.time.timestamp()) # Assuming this is how you fetch timestamps # Univariate reader initialization reader = NumpyReader({ "times": np.array(time), # Shape: [sequence_length] "values": np.array(status) # Shape: [sequence_length] })
Adapting for Multivariate Data
Say you have multiple features to track (e.g., sensor status, temperature, humidity) for each time step. Here's how to structure your data:
- Collect features per time step: For each entry in
sensorstatus, gather all your features into a single list/array instead of just one value. - Shape your arrays: Convert your collected data into numpy arrays where:
TIMESremains a 1D array with shape[sequence_length](one timestamp per time step)VALUESbecomes a 2D array with shape[sequence_length, num_features](each row is a time step, each column is a feature)
Here's a complete example:
import numpy as np from tensorflow.contrib.timeseries.python.timeseries import NumpyReader time_steps = [] multivariate_features = [] # Iterate through your sensor data for ss in sensorstatus: # Capture the timestamp for this time step time_steps.append(ss.time.timestamp()) # Pack all features for this time step into a single list current_step_features = [ int(ss.status), # Feature 1: sensor status float(ss.temperature),# Feature 2: temperature reading float(ss.humidity) # Feature 3: humidity reading ] multivariate_features.append(current_step_features) # Convert to numpy arrays with correct shapes times_array = np.array(time_steps) # Shape: (sequence_length,) values_array = np.array(multivariate_features) # Shape: (sequence_length, 3) # Initialize the NumpyReader for multivariate data multivariate_reader = NumpyReader({ "times": times_array, "values": values_array }) # Optional: Verify shapes to make sure everything is correct print(f"TIMES shape: {times_array.shape}") # Should look like (X,) where X is your sequence length print(f"VALUES shape: {values_array.shape}") # Should look like (X, 3) for 3 features
Key Notes to Avoid Issues
- Consistent feature count: Every time step must have the same number of features—don't mix entries with 2 features and others with 3, or you'll get shape errors.
- Data type consistency: Try to keep feature values in the same data type (e.g., all floats) to avoid unexpected behavior during model training.
- TensorFlow compatibility: The
NumpyReaderhandles the 2DVALUEStensor natively, so downstream time series models (like ARIMA or LSTM-based models) will accept this input format without extra conversion.
内容的提问来源于stack exchange,提问作者Mikko P
相关产品推荐
相关产品推荐

