'How to input a history of vectors to LSTM based network instead of a single vector?

I am trying to predict a simple pattern using LSTM based network. I input a single vector and get the output vector with the same shape as a prediction. How can I use a history of vectors as input (matrix) to cover a long pattern and get a single vector as output? What would be the output shape if I used a matrix as input?

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import random
import pandas as pd
import matplotlib.pyplot as plt

class Channel:

    def __init__(self, number_of_channels, switching_probability, activation_seq):
        self.number_of_channels = number_of_channels
        self.switching_probability = switching_probability
        self.activation_seq = activation_seq
        self.active_set = 0

    def getState(self):
        # Generate state vector
        state = np.zeros(self.number_of_channels)
        for i in range(self.number_of_channels):

            if i == self.activation_seq[self.active_set]:
                state[i] = 1
        if random.random() <= self.switching_probability:
            self.active_set = (self.active_set + 1) % len(self.activation_seq)
        return state

class LTSMNetwork(object):
    def __init__(self, num_channels, num_hidden_neurons, learning_rate, time_steps):
        self.num_channels = num_channels
        self.num_hidden_neurons = num_hidden_neurons
        self.learning_rate = learning_rate
        self.time_steps = time_steps

    def lstm_model(self):
        self.model = tf.keras.Sequential()
        self.model.add(tf.keras.layers.LSTM(batch_input_shape=\
                                            (1, self.time_steps, self.num_channels), 
                                            units=self.num_hidden_neurons[0], 
                                            activation='tanh', recurrent_activation='sigmoid', 
                                            return_sequences=False, stateful=True))

        hidden_layer = tf.keras.layers.Dense(units=self.num_hidden_neurons[1], activation=tf.nn.sigmoid)
        self.model.add(hidden_layer)
        self.model.add(tf.keras.layers.Dense(units=self.num_channels, name="output_layer", activation=tf.nn.tanh))
        self.model.compile(optimizer=tf.optimizers.Adam(learning_rate=self.learning_rate), 
                            loss='mse', metrics=['binary_accuracy'])
        return self.model

if __name__ =="__main__":
    tf.compat.v1.disable_eager_execution()
    tf.compat.v1.experimental.output_all_intermediates(True)
    num_channels = 4
    activation_pattern = [0, 1, 2, 3]
    switching_prob = 1
    num_hidden_neurons = [200, 150]
    learning_rate = 0.001
    time_steps = 1
    training_sample = 1
    sample_length = 1
    channel = Channel(number_of_channels=num_channels, 
                                switching_probability=switching_prob, activation_seq=activation_pattern)

    lstm_network = LTSMNetwork(num_channels=num_channels, num_hidden_neurons=num_hidden_neurons, 
                                learning_rate=learning_rate, time_steps=time_steps)
    model_a = lstm_network.lstm_model()
    model_b = lstm_network.lstm_model()
    model_a.summary()
    number_of_iteration = 1000
    state_vector = channel.getState()

    for i in range(number_of_iteration):
        observation_vector = np.reshape(state_vector, (training_sample, sample_length, num_channels))
        next_state_vector = channel.getState()
        model_b.set_weights(model_a.get_weights())
        prediction = model_b.predict(x=observation_vector)
        target_vector = np.reshape(next_state_vector, (sample_length, num_channels))
        model_a.fit(observation_vector, target_vector, verbose=0, batch_size=1)
        state_vector = next_state_vector
        print(f'state vector:{state_vector}')
        print(f'next state: {next_state_vector}')


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source