8

次のような入力があります。

[
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
...]

shape(1, num_samples, num_features)の、および次のようなラベル:

[
[0, 1]
[1, 0]
[1, 0]
...]

の形状(1, num_samples, 2)

ただし、次の Keras コードを実行しようとすると、次のエラーが発生します: ValueError: Error when checking model target: expected dense_1 to have 2 dimensions, but got array with shape (1, 8038, 2). 私が読んだことから、これは私のラベルが単なる整数ではなく 2D であるという事実に起因しているようです。これは正しいですか?もしそうなら、Keras でワンホット ラベルを使用するにはどうすればよいですか?

コードは次のとおりです。

num_features = 463
trX = np.random(8038, num_features)
trY = # one-hot array of shape (8038, 2) as described above

def keras_builder():  #generator to build the inputs
    while(1):
        x = np.reshape(trX, (1,) + np.shape(trX))
        y = np.reshape(trY, (1,) + np.shape(trY))
        print(np.shape(x)) # (1, 8038, 463)
        print(np.shape(y)) # (1, 8038, 2)
        yield x, y

model = Sequential()
model.add(LSTM(100, input_dim = num_features))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit_generator(keras_builder(), samples_per_epoch = 1, nb_epoch=3, verbose = 2, nb_worker = 1)

上記のエラーがすぐにスローされます。

Traceback (most recent call last):
  File "file.py", line 35, in <module>
    model.fit_generator(keras_builder(), samples_per_epoch = 1, nb_epoch=3, verbose = 2, nb_worker = 1)
  ...
ValueError: Error when checking model target: expected dense_1 to have 2 dimensions, but got array with shape (1, 8038, 2)

ありがとうございました!

4

1 に答える 1