我正在研究LSTM。
输出是分类的。
其格式为[[t11,t12,t13],[t21,t22,t23]
我能够针对一维数组进行操作,但发现很难针对二维数组进行操作。
from keras.utils import to_categorical
print(to_categorical([[9,10,11],[10,11,12]]))
输出
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
有两个不同的输入,每个输入都有3个时间步长,但是在输出中它们全部合并在一起。
我需要它,
[[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]],
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]]
答案 0 :(得分:3)
我意识到我可以通过重塑来实现自己想要的目标,
print(a.reshape(2,3,13))
[[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]]
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]]
答案 1 :(得分:3)
如果形状很怪异,请尝试将其设为1D,使用该函数,然后将其重新塑形:
originalShape = myData.shape
totalFeatures = myData.max() + 1
categorical = myData.reshape((-1,))
categorical = to_categorical(categorical)
categorical = categorical.reshape(originalShape + (totalFeatures,))
答案 2 :(得分:0)
在重塑时会出现错误,因为最高的类索引是12,因此有13个类(0、1,...,12)。为了进一步避免此类错误,您可以通过调用one_hot.reshape(sparse.shape + [-1])
来让Numpy推断这些尺寸,其中one_hot
是由to_categorical()
和sparse
原始编码的矢量生成的单编码矢量。