如何在 python 中将一维 numpy 数组标签编码为二维

问题描述 投票:0回答:1

我正在使用骨架数据构建一个用于人类行为识别的注意力模型。我想使数据集和标签适合模型,但我的标签是一维的。这是我的数据和标签尺寸:

x_train.shape = (431, 779)

x_test.shape = (430, 779)

y_train.shape = (431,)

y_test.shape = (430,)

为了适合模型,所有 x_trian、x_test、y_train 和 y_test 都应该有两个维度。

这是我的模特:

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)

但是标签的维度应该是:(431, 27) 因为我们有 27 个动作要识别。

我用下面的代码来转换标签的尺寸,但是我得到了错误:

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:以 10 为底的 int() 的无效文字:'a10'

如何使用动作类的数量将我的标签编码为二维?如果有人能帮助我,我将不胜感激。

python tensorflow keras deep-learning
1个回答
0
投票

如果我们想将一维数组增加到二维数组,我们可以为其添加一个维度

shape

y_train.shape += (1,)
y_test.shape += (1,)

但是,如果第二个维度需要有 27 个标签(因为我不确定你是如何得到你的初始

y_train
变量而不是它的 27 列维度......无论如何)然后制作一个新的
numpy
零数组和填充它:

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] = ...
© www.soinside.com 2019 - 2024. All rights reserved.