i have this full code but i have an error in th last step could u plz
Question:
i have this full code but i have an error in th last step could u plz help me
this is the code:
from tensorflow.keras.layers import LayerNormalization, Layer, Dense, ReLU, Dropout
# Implementing the Add & Norm Layer
class AddNormalization(Layer):
def __init__(self, **kwargs):
super(AddNormalization, self).__init__(**kwargs)
self.layer_norm = LayerNormalization() # Layer normalization layer
def call(self, x, sublayer_x):
# The sublayer input and output need to be of the same shape to be summed
add = x + sublayer_x
# Apply layer normalization to the sum
return self.layer_norm(add)
# Implementing the Feed-Forward Layer
class FeedForward(Layer):
def __init__(self, d_ff, embed_size, **kwargs):
super(FeedForward, self).__init__(**kwargs)
self.fully_connected1 = Dense(d_ff) # First fully connected layer
self.fully_connected2 = Dense(embed_size) # Second fully connected layer
self.activation = ReLU() # ReLU activation layer
def call(self, x):
# The input is passed into the two fully-connected layers, with a ReLU in between
x_fc1 = self.fully_connected1(x)
return self.fully_connected2(self.activation(x_fc1))
# Implementing the Encoder Layer
class EncoderLayer(Layer):
def __init__(self, h, d_k, d_v, embed_size, d_ff, rate, **kwargs):
super(EncoderLayer, self).__init__(**kwargs)
self.multihead_attention = MultiHeadAttention(h, d_k, d_v, embed_size)
self.dropout1 = Dropout(rate)
self.add_norm1 = AddNormalization()
self.feed_forward = FeedForward(d_ff, embed_size)
self.dropout2 = Dropout(rate)
self.add_norm2 = AddNormalization()
def call(self, x, padding_mask, training):
# Multi-head attention layer
multihead_output = self.multihead_attention(x, x, x, padding_mask)
# Expected output shape = (batch_size, sequence_length, embed_size)
# Add in a dropout layer
multihead_output = self.dropout1(multihead_output, training=training)
# Followed by an Add & Norm layer
addnorm_output = self.add_norm1(x, multihead_output)
# Expected output shape = (batch_size, sequence_length, embed_size)
# Followed by a fully connected layer
feedforward_output = self.feed_forward(addnorm_output)
# Expected output shape = (batch_size, sequence_length, embed_size)
# Add in another dropout layer
feedforward_output = self.dropout2(feedforward_output, training=training)
# Followed by another Add & Norm layer
return self.add_norm2(addnorm_output, feedforward_output)
# Implementing the Encoder
class Encoder(Layer):
def __init__(self, vocab_size, sequence_length, h, d_k, d_v, embed_size, d_ff, n, rate, **kwargs):
super(Encoder, self).__init__(**kwargs)
self.pos_encoding = PositionEmbeddingFixedWeights(sequence_length, vocab_size, embed_size)
self.dropout = Dropout(rate)
self.encoder_layer = [EncoderLayer(h, d_k, d_v, embed_size, d_ff, rate) for _ in range(n)]
def call(self, input_sentence, padding_mask, training):
# Generate the positional encoding
pos_encoding_output = self.pos_encoding(input_sentence)
# Expected output shape = (batch_size, sequence_length, embed_size)
# Add in a dropout layer
x = self.dropout(pos_encoding_output, training=training)
# Pass on the positional encoded values to each encoder layer
for i, layer in enumerate(self.encoder_layer):
x = layer(x, padding_mask, training)
return x
from tensorflow.keras.layers import Layer, Dropout
# Implementing the Decoder Layer
class DecoderLayer(Layer):
def __init__(self, h, d_k, d_v, embed_size, d_ff, rate, **kwargs):
super(DecoderLayer, self).__init__(**kwargs)
self.multihead_attention1 = MultiHeadAttention(h, d_k, d_v, embed_size)
self.dropout1 = Dropout(rate)
self.add_norm1 = AddNormalization()
self.multihead_attention2 = MultiHeadAttention(h, d_k, d_v, embed_size)
self.dropout2 = Dropout(rate)
self.add_norm2 = AddNormalization()
self.feed_forward = FeedForward(d_ff, embed_size)
self.dropout3 = Dropout(rate)
self.add_norm3 = AddNormalization()
def call(self, x, encoder_output, lookahead_mask, padding_mask, training):
# Multi-head attention layer
multihead_output1 = self.multihead_attention1(x, x, x, lookahead_mask)
# Expected output shape = (batch_size, sequence_length, embed_size)
# Add in a dropout layer
multihead_output1 = self.dropout1(multihead_output1, training=training)
# Followed by an Add & Norm layer
addnorm_output1 = self.add_norm1(x, multihead_output1)
# Expected output shape = (batch_size, sequence_length, embed_size)
# Followed by another multi-head attention layer
multihead_output2 = self.multihead_attention2(addnorm_output1, encoder_output, encoder_output, padding_mask)
# Add in another dropout layer
multihead_output2 = self.dropout2(multihead_output2, training=training)
# Followed by another Add & Norm layer
addnorm_output2 = self.add_norm1(addnorm_output1, multihead_output2)
# Followed by a fully connected layer
feedforward_output = self.feed_forward(addnorm_output2)
# Expected output shape = (batch_size, sequence_length, embed_size)
# Add in another dropout layer
feedforward_output = self.dropout3(feedforward_output, training=training)
# Followed by another Add & Norm layer
return self.add_norm3(addnorm_output2, feedforward_output)
# Implementing the Decoder
class Decoder(Layer):
def __init__(self, vocab_size, sequence_length, h, d_k, d_v, embed_size, d_ff, n, rate, **kwargs):
super(Decoder, self).__init__(**kwargs)
self.pos_encoding = PositionEmbeddingFixedWeights(sequence_length, vocab_size, embed_size)
self.dropout = Dropout(rate)
self.decoder_layer = [DecoderLayer(h, d_k, d_v, embed_size, d_ff, rate) for _ in range(n)]
def call(self, output_target, encoder_output, lookahead_mask, padding_mask, training):
# Generate the positional encoding
pos_encoding_output = self.pos_encoding(output_target)
# Expected output shape = (number of sentences, sequence_length, embed_size)
# Add in a dropout layer
x = self.dropout(pos_encoding_output, training=training)
# Pass on the positional encoded values to each encoder layer
for i, layer in enumerate(self.decoder_layer):
x = layer(x, encoder_output, lookahead_mask, padding_mask, training)
return x
and i have this CSV file i wanna i work with this code , i have input_text and target_text
#Reading the CSV
df=pd.read_csv("/content/output.csv")
df.nunique()
#split the dataset
input_text = df['English'].tolist()
target_text = df['Arabic'].tolist()
# Function to add start and end tags to a target_text:Arabic_text
def add_start_end_tags(sentence):
if sentence.startswith("
return sentence
else:
return "
# Add start and end tags to each Arabic sentence in target_text
target_text = [add_start_end_tags(sentence) for sentence in target_text]
def add_start_end_tags(sentence):
if sentence.startswith("
return sentence
else:
return "
# Add start and end tags to each English sentence in input_text
input_text = [add_start_end_tags(sentence) for sentence in input_text]
# Preprocessing the input_text: English text
# Tokenization
tokenizer = Tokenizer()
tokenizer.fit_on_texts(input_text)
input_sequences = tokenizer.texts_to_sequences(input_text)
# Padding
input_seq_length = max(len(seq) for seq in input_sequences)
input_sequences_padded = pad_sequences(input_sequences, maxlen=input_seq_length, padding='post')
# Vocabulary size
enc_vocab_size = len(tokenizer.word_index) + 1
print("Input Vocabulary size:", enc_vocab_size)
print("Max sequence length:", input_seq_length)
input_seq = np.array(input_text)
target_seq = np.array(target_text)
h = 8 # Number of self-attention heads
d_k = 64 # Dimensionality of the linearly projected queries and keys
d_v = 64 # Dimensionality of the linearly projected values
d_ff = 2048 # Dimensionality of the inner fully connected layer
embed_size = 512 # Dimensionality of the model sub-layers' outputs
n = 6 # Number of layers in the encoder stack
batch_size = 64 # Batch size based on the number of input sequences
dropout_rate = 0.1 # Frequency of dropping the input units in the dropout layers
#all above code is work with me only i have a error here
encoder = Encoder(enc_vocab_size, input_seq_length, h, d_k, d_v, embed_size, d_ff, n, dropout_rate)
enc_output = encoder(input_seq, None, True)
print(enc_output)
"UnimplementedError Traceback (most recent call last)
1 encoder = Encoder(enc_vocab_size, input_seq_length, h, d_k, d_v, embed_size, d_ff, n, dropout_rate)
----> 2 enc_output = encoder(input_seq, None, True)
3 print(enc_output)
2 frames
27 def call(self, inputs):
28 position_indices = tf.range(tf.shape(inputs)[-1])
---> 29 embedded_words = self.word_embedding_layer(inputs)
30 embedded_indices = self.position_embedding_layer(position_indices)
31 return embedded_words + embedded_indices
UnimplementedError: Exception encountered when calling layer 'embedding' (type Embedding).
{{function_node __wrapped__Cast_device_/job:localhost/replica:0/task:0/device:CPU:0}} Cast string to int32 is not supported [Op:Cast]
Call arguments received by layer 'embedding' (type Embedding):
• inputs=tf.Tensor(shape=(24638,), dtype=string)"
decoder = Decoder(dec_vocab_size, input_seq_length, h, d_k, d_v, embed_size, d_ff, n, dropout_rate)
dec_output = decoder(target_seq, enc_output, None, True)
print(dec_output)
could u solve it plzzzz
for input data
Input Vocabulary size: 26063 Max sequence length: 227
target data
Target vocabulary size: 60405 Max sequence length: 227
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill