Closed dupsys closed 7 years ago
Hey @dupsys,
This is simply an error saying the input shape to your model is different than the shape you specify in the model itself.
This line batch_input_shape=(maxSequenceLength, len(char_2_id),1)
will, in your case, end up with the shape (173, 38, 1). Note here that because you use batch_input_shape, you are saying the batch_size will be 173 and the data will be in the shape (38,1).
However, the input you are trying to give the network is (32, 173, 38). This is a batch size of 32.
I believe what you want to do unless I'm misunderstanding your task is the following:
batch_input_shape=(None, maxSequenceLength, len(char_2_id))
Hi @kentsommer, Thank you so much for the response. I am completely new in this environment but i will catch up very soon.
I insert the batch_input_shape as suggested but get an error saying:
If an RNN is stateful, a complete input_shape must be provided (including batch size).
print('Building training model...') hiddenStateSize = 256 hiddenLayerSize = 256 timesteps = 8
model = Sequential()
model.add(LSTM(hiddenStateSize, return_sequences=True, stateful=True, batch_input_shape=(None, maxSequenceLength, len(char_2_id)),forget_bias_init='one')) model.add(Dropout(0.3)) model.add(TimeDistributed(Dense(hiddenLayerSize))) model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(Dense(len(char_2_id)))) model.add(TimeDistributed(Activation('softmax')))
%time model.compile(loss='categorical_crossentropy', optimizer = RMSprop(lr=0.001), metrics=['accuracy'])
print(model.summary())
print("Sample input Batch size:"), print(inputChars[0:32, :, :].shape) print("Sample input Batch labels (nextChars):"), print(nextChars[0:32, :, :].shape) outputs = model.predict(inputChars[0:32, :, :]) print("Output Sequence size:"), print(outputs.shape)
@dupsys
Ah sorry, I forgot about that. Then because you are feeding in a batch_size of 32, you should just need to set it to: batch_input_shape=(32, maxSequenceLength, len(char_2_id))
Then, what will be my batch_size input in my fitting model because i have 10000 samples:
h = model.fit(inputChars, nextChars, batch_size = 32, nb_epoch = 50, show_accuracy=True, verbose=1)
because, i have this error: In a stateful network, you should only pass inputs with a number of samples that can be divided by the batch size. Found: 10000 samples
@dupsys
I suggest you take a deeper look at the documentation from which you are pulling your code:
http://www.cs.virginia.edu/~vicente/recognition/notebooks/kerasLSTM.html
Specifically section 5.
@kentsommer
okay, thanks.
Hi @kentsommer,
I am trying to implement this paper https://arxiv.org/pdf/1605.03481.pdf with the following code.
`# define the symbol alphabet, including the padding symbol '#' and create mapping of unique chars to integers chars = 'X'+ ''.join(sorted({c for s in raw_text for c in s})) char_to_int = {c:i for i,c in enumerate(chars)}
n_chars = len(raw_text)
max_length = max(map(len, raw_text.split()))
def encode_string(s): return [char_to_int[c] for c in s] def decode_string(a): return ''.join(chars[i] for i in a) def encode_and_pad(words): return pad_sequences(list(map(encode_string, words)), padding='post', maxlen=max_length)
padded_input = encode_and_pad(chars)
X_train, X_test = train_test_split(padded_input, test_size=0.20, random_state=seed) X_train = sequence.pad_sequences(X_train, maxlen=max_length) X_test = sequence.pad_sequences(X_test, maxlen=max_length)
sequence_length = X_train.shape[1] vocabulary_size = len(chars) embedding_dim = 256 decoder_size = 128
num_filters = 256 drop = 0.5
nb_epoch = 10 batch_size = 5
inputs = Input(shape=(sequence_length, vocabulary_size), name='input', dtype='float32')
conv = Convolution1D(num_filters, filter_length=filter_sizes[0], border_mode='valid', activation='relu',input_shape=(sequence_length, vocabulary_size))(inputs) pool = MaxPooling1D(pool_length= filter_sizes[1])(conv) conv1 = Convolution1D(num_filters, filter_length=filter_sizes[0], border_mode='valid', activation='relu')(pool) pool2 = MaxPooling1D(pool_length= filter_sizes[1])(conv1) conv3 = Convolution1D(num_filters, filter_length=filter_sizes[0], border_mode='valid', activation='relu')(pool2) conv4 = Convolution1D(num_filters, filter_length=filter_sizes[0], border_mode='valid', activation='relu')(conv3)
encoding_layer = LSTM(conv4, embedding_dim, return_sequences=True) rep_in = RepeatVector(encoding_layer) l_decoder_1 = LSTM(rep_in, decoder_size, name='decoder1') l_decoder_2 = LSTM(l_decoder_1, decoder_size, name='decoder2') l_reshape = Reshape(l_decoder_2, (-1,[2]), name='l_reshape')`
The objective is to reshape decoders LSTM to define the actual input text or synonym replacement of the early text. Running this code, i got the following:
TypeError Traceback (most recent call last)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
Building our model using an LSTM Recurrent Network
print('Building training model...') hiddenStateSize = 256 hiddenLayerSize = 256 timesteps = 8
model = Sequential() model.add(LSTM(hiddenStateSize, return_sequences = True, stateful=True, batch_input_shape=(maxSequenceLength, len(char_2_id),1),forget_bias_init='one')) model.add(Dropout(0.3)) model.add(TimeDistributed(Dense(hiddenLayerSize))) model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(Dense(len(char_2_id)))) model.add(TimeDistributed(Activation('softmax')))
%time model.compile(loss='categorical_crossentropy', optimizer = RMSprop(lr=0.001), metrics=['accuracy'])
Print the details about the network model
print(model.summary())
Test a simple prediction on a batch for this model.
print("Sample input Batch size:"), print(inputChars[0:32, :, :].shape) print("Sample input Batch labels (nextChars):"), print(nextChars[0:32, :, :].shape) outputs = model.predict(inputChars[0:32, :, :]) print("Output Sequence size:"), print(outputs.shape)
I got this error: ValueError: Error when checking : expected lstm_input_3 to have shape (173, 38, 1) but got array with shape (32, 173, 38)