Search code examples
pythontensorflowkerasdeep-learning

how to encode one dimension numpy array labels to two dimensions in python


I'm building an attention model for human action recognition using skeleton data. I want to fit the dataset and labels to the model but my labels are one dimension. Here are my data and labels dimensions:

x_train.shape = (431, 779)

x_test.shape = (430, 779)

y_train.shape = (431,)

y_test.shape = (430,)

For fitting to the model all x_trian, x_test, y_train, and y_test should have two dimensions.

And here is my model:

import tensorflow as tf
from tensorflow import keras

inputs = keras.Input(shape=(x_train.shape[0], x_train.shape[1]))
lstm = keras.layers.LSTM(128, return_sequences=True)(inputs)
attention = keras.layers.Attention()([lstm, lstm], return_attention_scores=True)
attention_output = attention[0]
dense = keras.layers.Dense(64, activation='relu')(attention_output)
outputs = keras.layers.Dense(num_classes, activation='softmax')(dense)
model = keras.Model(inputs=inputs, outputs=outputs)

But the dimensions of labels should be: (431, 27) because we have 27 actions to recognize.

I used the below code to convert the dimensions of labels, but I got error:

from tensorflow.keras.utils import to_categorical

num_classes = 27

y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)

ValueError: invalid literal for int() with base 10: 'a10'

How can I encode my labels to two dimensions with number of action classes? I would be so grateful if anyone can help me.


Solution

  • If the labels are supposed to be the same across columns (and you just want to increase the second dimension to 27 columns) we can use numpy's append function:

    import numpy as np
    y_train = np.zeros(431).astype(float)
    print(f'original shape: {y_train.shape}')
    _y_train = y_train
    _y_train.shape += (1,)
    for _ in range(26):
        _y_train = np.append(_y_train, y_train, axis=1)
    print(f'new shape: {_y_train.shape}')
    

    Outputs:

    original shape: (431,)
    new shape: (431, 27)
    

    or if the second dimension of 27 columns is not just supposed to be repeated labels and you want to populate it:

    import numpy as np
    _y_train = np.zeros((431, 27)).astype(float)
    for row in range(_y_train.shape[0]):
        for col in range(_y_train.shape[1]):
            _y_train[row, col] = ...