def train_model(data, n_a=50, max_iter = 100000): # Get the list of characters chars = list(set(data)) # Get the dictionary size (number of characters) vocab_size = len(chars) # Get encoding and decoding dictionaries chars_to_encoding = encode_chars(chars) encoding_to_chars = decode_chars(chars) # Get dataset as a list of names and strip, then shuffle the dataset data = data.split('n') data = [x.strip() for x in data] np.random.shuffle(data) # Define n_x, n_y parameters n_x, n_y = vocab_size, vocab_size # Initialize the hidden state # a_prev = initialize_hidden_state(n_a) a_prev = np.zeros((n_a, 1)) # Initialize the parameters parameters = initialize_parameters(n_a, n_x, n_y) # for k in parameters.keys(): # print('{}: tipo {}, Datatype {}'.format(k, type(parameters[k]), parameters[k].dtype)) # Get current loss function value loss_now = get_initial_loss(vocab_size, len(data)) # Perform max_iter iteration to train the model's parameters for iter in range(max_iter): # print(iter) # Get the index of the name to pick name_idx = iter % len(data) example = data[name_idx] # Convert encoded and decoded example into a list example_chars = [char for char in example] example_encoded = [chars_to_encoding[char] for char in example] # Create training input X. The value None is used to consider the first input character # as a vector of zeros X = [None] + example_encoded # Create the label vector Y by appending the 'n' encoding to the end of the vector Y = example_encoded + [chars_to_encoding['n']] # Perform one step of the optimization cycle: # 1. Forward propagation # 2. Backward propagation # 3. Gradient clipping # 4. Parameters update loss_tmp, parameters, a_prev = RNN_optimization(X, Y, a_prev, parameters, alpha=0.01, vocab_size=vocab_size) # for k in parameters.keys(): # print('{}: tipo {}, Datatype {}'.format(k, type(parameters[k]), parameters[k].dtype)) loss_now = smooth(loss_now, loss_tmp) return parameters