Im starting to learn about neural networks and recently im trying to create a text-generator neural network. Im learning from sites and code from other neural networks and i created some functional code.
Now im trying to improve that Neural Network with things like one hot encode in the input shape (I dont know why i only do it because one of the sites where i was learning told me to try) and im having lots of problem in the input.
import numpy as np
import sys
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
filename = "los3.txt"
raw_text = open(filename).read()
raw_text = open(filename).read()
raw_text = raw_text.lower()
#Crear mapeo de caracteres únicos a enteros, y un mapeo inverso
chars = sorted(list(set(raw_text)))
char_to_int = dict((c, i) for i, c in enumerate(chars))
#Sumarizamos los datos cargados
n_chars = len(raw_text)
n_vocab = len(chars)
print("Total caracters: ", n_chars)
print("Total vocabulario: ", n_vocab)
seq_length = 100
x_data = []
y_data = []
# loop through inputs, start at the beginning and go until we hit
# the final character we can create a sequence out of
for i in range(0, n_chars - seq_length, 1):
# Define input and output sequences
# Input is the current character plus desired sequence length
in_seq = raw_text[i:i + seq_length]
# Out sequence is the initial character plus total sequence length
out_seq = raw_text[i + seq_length]
# We now convert list of characters to integers based on
# previously and add the values to our lists
x_data.append([char_to_int[char] for char in in_seq])
x_encoded = np_utils.to_categorical(x_data)
y_data.append(char_to_int[out_seq])
n_patterns = len(x_encoded)
print ("Total Patterns:", n_patterns)
print(x_encoded.shape)
y = np_utils.to_categorical(y_data)
#shape de la matriz encoded= (3129, 100, 39)
x_encoded= x_encoded/float(n_vocab)
#Define la LSTM model
model = Sequential()
model.add(LSTM(256, input_shape=(x_encoded.shape[1], x_encoded.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(256))
model.add(Dropout(0.2))
model.add(LSTM(128))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
filepath = "model_weights_saved.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor="loss", verbose=1, save_best_only=True, mode="min")
desired_callbacks = [checkpoint]
model.fit(x_encoded, y, epochs=150, batch_size=256, callbacks=desired_callbacks)
Then i get this error
Total caracters: 3229
Total vocabulario: 39
Total Patterns: 3129
(3129, 100, 39)
(3129, 100, 39)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [44], in <cell line: 55>()
53 #Define la LSTM model
54 model = Sequential()
---> 55 model.add(LSTM(256, input_shape=(3129, 100, 1), return_sequences=True))
56 model.add(Dropout(0.2))
57 model.add(LSTM(256))
File E:\Analisis de datos\anaconda\envs\py37\lib\site-packages\tensorflow\python\training\tracking\base.py:587, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
585 self._self_setattr_tracking = False # pylint: disable=protected-access
586 try:
--> 587 result = method(self, *args, **kwargs)
588 finally:
589 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
File E:\Analisis de datos\anaconda\envs\py37\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
File E:\Analisis de datos\anaconda\envs\py37\lib\site-packages\keras\engine\input_spec.py:214, in assert_input_compatibility(input_spec, inputs, layer_name)
212 ndim = shape.rank
213 if ndim != spec.ndim:
--> 214 raise ValueError(f'Input {input_index} of layer "{layer_name}" '
215 'is incompatible with the layer: '
216 f'expected ndim={spec.ndim}, found ndim={ndim}. '
217 f'Full shape received: {tuple(shape)}')
218 if spec.max_ndim is not None:
219 ndim = x.shape.rank
ValueError: Input 0 of layer "lstm_15" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 3129, 100, 1)
My array automaticaly pass from ndim=3 to ndim=4 being the first dimension=none i tried lots of things but i dont know why i get this errors or how can i fix the input