X = []
Y = []
for line in document:
words = line.split()
line_length = len(words)
if line_length > 1: # Lines with 1 word or less are excluded
input_sequence = [word_to_index.get(words[0], 0)] # First word as input
output_sequence = [word_to_index.get(word, 0) for word in words[1:max_sequence_length]] # Remaining words as output
# Pad shorter sequences with zeros
while len(output_sequence) < max_sequence_length - 1:
output_sequence.append(0)
X.append(input_sequence)
Y.append(output_sequence)
Is this the correct way or? I just want my model to be able to create a poem of less than 10-12 words as each element in posted list of string, and I am trying to use NLP for it. word_to_index is just assigning numbers to each words in document. How can I do this? When I proceeded with this, it showed some error in dimension in loss computation.
Any further tips for this? How should I move forward to get the poem generator?
The dataset looks like this
The X and Y train are of the shapes as this
This gives the error
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1377, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1360, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1349, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1127, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1185, in compute_loss
return self.compiled_loss(
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/compile_utils.py", line 277, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.10/dist-packages/keras/src/losses.py", line 143, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.10/dist-packages/keras/src/losses.py", line 270, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.10/dist-packages/keras/src/losses.py", line 2221, in categorical_crossentropy
return backend.categorical_crossentropy(
File "/usr/local/lib/python3.10/dist-packages/keras/src/backend.py", line 5575, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 16) and (None, 1, 16) are incompatible
I just want my model to take a word input and create a one line output poem of 5-7-5 syllables count or just the appropriate one liner poem for now that can be about 10 to 15 words around. How should I approach this?
I tried the approach but got errors in the shape. I am finding it hard to modify it as I think I am doing something wrong already! Any help is appreciated!
Could you just check what's the shape of your input data? Try changing the shape of your input data. The error shows problem with the dimension. Maybe while training your model it requires a particular dimension in which the input must be formatted and changing the shape of the input data might help. If the error still persists maybe you send that again. Thanks