llSourcell / tensorflow_chatbot

Tensorflow chatbot demo by @Sirajology on Youtube
1.45k stars 806 forks source link

ERROR IN #91

Closed reddyprasade closed 3 weeks ago

reddyprasade commented 6 years ago

Mode : train

Preparing data in working_dir Tokenizing data in working_dir/data/train.enc Tokenizing data in working_dir/data/train.dec Tokenizing data in working_dir/data/test.enc Tokenizing data in working_dir/data/test.enc Creating 3 layers of 256 units. WARNING:tensorflow:At least two cells provided to MultiRNNCell are the same object and will share weights. Traceback (most recent call last): File "C:/Users/user/tensorflow chatbot/execute.py", line 304, in train() File "C:/Users/user/tensorflow chatbot/execute.py", line 122, in train model = create_model(sess, False) File "C:/Users/user/tensorflow chatbot/execute.py", line 89, in create_model model = seq2seq_model.Seq2SeqModel( gConfig['enc_vocab_size'], gConfig['dec_vocab_size'], _buckets, gConfig['layer_size'], gConfig['num_layers'], gConfig['max_gradient_norm'], gConfig['batch_size'], gConfig['learning_rate'], gConfig['learning_rate_decay_factor'], forward_only=forward_only) File "C:\Users\user\tensorflow chatbot\seq2seq_model.py", line 121, in init self.outputs, self.losses = tf.nn.seq2seq.model_with_buckets( AttributeError: module 'tensorflow.nn' has no attribute 'seq2seq'

ouiem07 commented 5 years ago

Did u find a solution ?

reddyprasade commented 3 weeks ago

Did u find a solution ? `import numpy as np import tensorflow as tf from tensorflow.keras import layers from tensorflow.keras.utils import to_categorical

Function to create the sequence-to-sequence model

def create_model(enc_vocab_size, dec_vocab_size, num_layers, units):

Encoder

encoder_inputs = layers.Input(shape=(None,), name='encoder_input')
encoder_embedding = layers.Embedding(enc_vocab_size, units)(encoder_inputs)

encoder_lstm = encoder_embedding
for _ in range(num_layers):
    encoder_lstm, state_h, state_c = layers.LSTM(units, return_state=True, return_sequences=True)(encoder_lstm)

encoder_states = [state_h, state_c]

# Decoder
decoder_inputs = layers.Input(shape=(None,), name='decoder_input')
decoder_embedding = layers.Embedding(dec_vocab_size, units)(decoder_inputs)

decoder_lstm = decoder_embedding
for _ in range(num_layers):
    decoder_lstm, _, _ = layers.LSTM(units, return_sequences=True, return_state=True)(decoder_lstm, initial_state=encoder_states)

decoder_dense = layers.Dense(dec_vocab_size, activation='softmax')
decoder_outputs = decoder_dense(decoder_lstm)

# Define the model
model = tf.keras.Model([encoder_inputs, decoder_inputs], decoder_outputs)
return model

Example data preparation

def prepare_data(num_samples=1000, max_sequence_length=10, vocab_size=1000):

Generate random integer sequences as example data

encoder_input_data = np.random.randint(1, vocab_size, size=(num_samples, max_sequence_length))
decoder_input_data = np.random.randint(1, vocab_size, size=(num_samples, max_sequence_length))
decoder_target_data = np.random.randint(1, vocab_size, size=(num_samples, max_sequence_length))

# Convert target data to categorical (one-hot encoding)
decoder_target_data = to_categorical(decoder_target_data, num_classes=vocab_size)

return encoder_input_data, decoder_input_data, decoder_target_data

Training the model

def train_model(): enc_vocab_size = 1000 dec_vocab_size = 1000 num_layers = 3 units = 256 batch_size = 64 epochs = 10

# Prepare data
encoder_input_data, decoder_input_data, decoder_target_data = prepare_data()

# Create the model
model = create_model(enc_vocab_size, dec_vocab_size, num_layers, units)
model.compile(optimizer='adam', loss='categorical_crossentropy')

# Train the model
model.fit([encoder_input_data, decoder_input_data], decoder_target_data, 
          batch_size=batch_size, epochs=epochs, validation_split=0.2)

return model

Inference function for decoding

def decode_sequence(model, input_seq, max_decoder_length):

Create a separate encoder model

encoder_model = tf.keras.Model(model.input[0], model.layers[2].output)

# Create a separate decoder model
decoder_state_input_h = layers.Input(shape=(256,))
decoder_state_input_c = layers.Input(shape=(256,))
decoder_inputs = model.input[1]
decoder_embedding = model.layers[3](decoder_inputs)
decoder_lstm = model.layers[4](decoder_embedding, initial_state=[decoder_state_input_h, decoder_state_input_c])
decoder_outputs = model.layers[5]( decoder_lstm)
decoder_model = tf.keras.Model([decoder_inputs] + [decoder_state_input_h, decoder_state_input_c], decoder_outputs)

# Encode the input sequence
encoder_states = encoder_model.predict(input_seq)

# Generate empty target sequence of length 1
target_seq = np.zeros((1, 1))
target_seq[0, 0] = 1  # Start token (assuming 1 is the start token)

# Sampling loop for a batch of sequences
stop_condition = False
decoded_sentence = []

while not stop_condition:
    output_tokens, h, c = decoder_model.predict([target_seq] + encoder_states)

    # Sample a token
    sampled_token_index = np.argmax(output_tokens[0, -1, :])
    decoded_sentence.append(sampled_token_index)

    # Exit condition: either hit max length or find stop token
    if sampled_token_index == 0 or len(decoded_sentence) > max_decoder_length:  # Assuming 0 is the stop token
        stop_condition = True

    # Update the target sequence (of length 1)
    target_seq = np.zeros((1, 1))
    target_seq[0, 0] = sampled_token_index

    # Update states
    encoder_states = [h, c]

return decoded_sentence

Main execution

if name == "main": model = train_model()

Example input for decoding (random input for demonstration)

input_seq = np.random.randint(1, 1000, size=(1, 10))
decoded_sentence = decode_sequence(model, input_seq, max_decoder_length=10)
print("Decoded sentence:", decoded_sentence)`