Как исправить некую неизвестную проблему формы данных при подборе RNN-моделирования? - PullRequest
0 голосов
/ 31 мая 2019

У меня были проблемы в конце кода, model.fit, как показано ниже. Я полагаю, что сообщение об ошибке означает форму данных, но я не знаю.

from keras.layers import SimpleRNN, Embedding, Dense, LSTM
from keras.models import Sequential
from keras.preprocessing.sequence import pad_sequences

vocab_size = len(word_index)+1   # number of words
max_len = 157                    
data = pad_sequences(x, maxlen=max_len)
print("data shape: ", data.shape)
data shape:  (3012, 157)
data
array([[    0,     0,     0, ...,    51,    10,    36],
       [    0,     0,     0, ...,  1129,  4994,   920],
       [    0,     0,     0, ...,  5004,  5005,   364],
       ...,
       [    0,     0,     0, ..., 17364, 17365, 17366],
       [    0,     0,     0, ...,   114,   176,    54],
       [    0,     0,     0, ..., 17371, 17372,  1227]])
x_test = data[n_of_train:]
y_test = y[n_of_train:]   
x_train = data[:n_of_train]
y_train = y[:n_of_train]

print(x_test.shape)
print(y_test.shape)
print(x_train.shape)
print(y_train.shape)
print(type(x_test))
print(type(y_test))
print(type(x_train))
print(type(y_train))   
(603, 157)
(603,)
(2409, 157)
(2409,)
<class 'numpy.ndarray'>
<class 'pandas.core.series.Series'>
<class 'numpy.ndarray'>
<class 'pandas.core.series.Series'>
model = Sequential()
model.add(Embedding(vocab_size,32))
model.add(SimpleRNN(32)) 
# model.add(Dense(7, input_dim=3, init='uniform', activation='softmax'))
model.add(Dense(5, activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])

history = model.fit(x_train, y_train, epochs=2, batch_size=32, validation_data=(x_test,y_test),verbose=1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-1b1364437bdb> in <module>
----> 1 history = model.fit(x_train, y_train, epochs=2, batch_size=32, validation_data=(x_test,y_test),verbose=1)

C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    950             sample_weight=sample_weight,
    951             class_weight=class_weight,
--> 952             batch_size=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    787                 feed_output_shapes,
    788                 check_batch_axis=False,  # Don't enforce the batch size.
--> 789                 exception_prefix='target')
    790 
    791             # Generate sample-wise weight values given the `sample_weight` and

C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    136                             ': expected ' + names[i] + ' to have shape ' +
    137                             str(shape) + ' but got array with shape ' +
--> 138                             str(data_shape))
    139     return data
    140 

ValueError: Error when checking target: expected dense_1 to have shape (5,) but got array with shape (1,)

...