0

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

  • Your error log is `input_shape=(3129, 100, 1)`, but your code is `input_shape=(x_encoded.shape[1], x_encoded.shape[2])`. Why both of them are different? Maybe you paste the old stuff instead the latest one? – Thariq Nugrohotomo Jul 19 '22 at 11:48
  • cause i was trying a lot of differents things to make it work and i went back to the initial code, its okay – Guillermo Muñoz Jul 20 '22 at 23:50
  • I think that's making harder for us to help you. Because the code and the error message do not match! We can't run your code since we don't have your data, and even worse we may be given the wrong error message or even the wrong code. – Thariq Nugrohotomo Jul 22 '22 at 09:18
  • yeah u are right but the error is practically the same – Guillermo Muñoz Jul 25 '22 at 10:59
  • Sorry, but I don't think so. I think it's impossible for your code to throw that error message. `input_shape=(x_encoded.shape[1], x_encoded.shape[2])` should be already fixed that error. I believe you should get different error message with the code you've provided. – Thariq Nugrohotomo Jul 26 '22 at 04:09

2 Answers2

0

I think you should check out the shape of the input_data, particularly the shape of x_encoded. I request you to check whether X is included or not because in this line:

model.fit(X, y, epochs=150, batch_size=256, callbacks=desired_callbacks)

you've given X. :) may help you.

gsv
  • 74
  • 1
  • 12
0

In this x_encoded = np_utils.to_categorical(x_data), the utils command is to convert the given integer sequence into binary class matrix. try

tf.keras.utils.to_categorical(
    y, num_classes=None, dtype='float32'
)

where you can determine the output class size by giving num_class value. Now, in your input, it considers the default value is the (largest number in input vector + 1) Which can cause the size of the x_encoded changes according to the input vector. So better give a defined num_class value. This MAY help you. :)

visit this https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical to get understand

gsv
  • 74
  • 1
  • 12