Search code examples
pythontensorflow

Trying to concatenate a row to a matrix in tensorflow


I have time series data in 4 channels and am trying to generate a sequence of length N using my model. I am determining N from the input data supplied to my sequence generation function:

def generate_sequence(self, input_data):
    predicted_sequence = tf.convert_to_tensor(input_data, dtype=tf.float32)
    data_shape = predicted_sequence.shape
    for i in range(len(predicted_sequence)):
        model_input = tf.reshape(predicted_sequence, shape=data_shape)
        result = self.model(model_input)
        predicted_sequence = tf.concat([predicted_sequence[:, 1:, :], result], 0)
    return predicted_sequence

This causes the following error:

ConcatOp : Dimension 1 in both shapes must be equal: shape[0] = [1,1439,4] vs. shape[1] = [1,1,4] [Op:ConcatV2] name: concat

This seems to suggest that I am using the wrong method to generate my sequence (I naively wrote this function assuming that tensorflow tensors would behave like numpy arrays). In my loop I start with my input data:

[[[a1, b1, c1, d1]
  [a2, b2, c2, d2]
  ...
  [aN, bN, cN, dN]]

and I generate a prediction using my model:

[[aP1, bP1, cP1, dP1]]

My intention at this point is to remove the first entry in the input data, as it is the oldest row of data, and add the predicted data to the end:

[[[a2, b2, c2, b2]
  [a3, b3, c3, d3]
  ...
  [aN, bN, cN, dN]
  [aP1, bP1, cP1, dP1]]]

From here the loop is run until the entire sequence contains predictions for the next N rows of data.

Is there another tensorflow method that is better suited to this or am I missing something in the tf.concat method?

Any help would be greatly appreciated.


Solution

  • Everything is good except the axis of concatenation. It should be axis=1.

    def generate_sequence(self, input_data):
    predicted_sequence = tf.convert_to_tensor(input_data, dtype=tf.float32)
    data_shape = predicted_sequence.shape
    for i in range(len(predicted_sequence)):
        model_input = tf.reshape(predicted_sequence, shape=data_shape)
        result = self.model(model_input)
        predicted_sequence = tf.concat([predicted_sequence[:, 1:, :], result], axis=1)
    return predicted_sequence
    

    The rule of thumb is "all the tensors should possess the same shape in all the axes except the concatenating axis"