Hello, i build an traduction ai using tensorflow now i have tokenize the dataset with my…
- User
Hello, i build an traduction ai using tensorflow now i have tokenize the dataset with my code and i use google colab cells by cells code I can not build a model optimized for ai translation your goal is to build the cell of the model with keras modl build and model compile for the best performance in translation :
%%time !pip install -q -U tensorflow-text !pip install datasets !pip install -q tensorflow_datasets !pip install pydot !cd /content !clear import tensorflow as tf import tensorflow_text as tf_text import tensorflow_datasets as tfds import numpy as np import matplotlib.pyplot as plt import requests import functools import collections import os import pathlib import re import string import tempfile import time import matplotlib.pyplot as plt import os import re import shutil import string import tensorflow as tf
from tensorflow.keras import layers from tensorflow.keras import losses import pydot from datasets import load_dataset
dataset = load_dataset(“Helsinki-NLP/opus_books”, “en-fr”) data = dataset[“train”]
french_sentences = [example[“fr”] for example in data[“translation”][:127085]] english_sentences = [example[“en”] for example in data[“translation”][:127085]] dataset = tf.data.Dataset.from_tensor_slices((french_sentences, english_sentences))
french_sentences_decoded = [] english_sentences_decoded = []
for french_sentence, english_sentence in dataset.take(127085): french_sentences_decoded.append(“b '”+french_sentence.numpy().decode(‘utf-8’)) english_sentences_decoded.append(“b '”+english_sentence.numpy().decode(‘utf-8’))
print(“Nombre de phrases en français :”, len(french_sentences_decoded)) print(“Nombre de phrases en anglais :”, len(english_sentences_decoded))
train_fr = french_sentences train_en = english_sentences
from tensorflow_text.tools.wordpiece_vocab import bert_vocab_from_dataset as bert_vocab
bert_tokenizer_params = dict(lower_case=True) reserved_tokens = [“[PAD]”, “[UNK]”, “[START]”, “[END]”]
bert_vocab_args = { ‘vocab_size’: 8000, ‘reserved_tokens’: reserved_tokens, ‘bert_tokenizer_params’: bert_tokenizer_params, ‘learn_params’: {}, }
%%time en_vocab = bert_vocab.bert_vocab_from_dataset( tf.data.Dataset.from_tensor_slices(english_sentences).batch(1000).prefetch(2), **bert_vocab_args )
%%time fr_vocab = bert_vocab.bert_vocab_from_dataset( tf.data.Dataset.from_tensor_slices(french_sentences).batch(1000).prefetch(2), **bert_vocab_args )
def write_vocab_file(filepath, vocab): with open(filepath, ‘w’) as f: for token in vocab: print(token, file=f) write_vocab_file(‘en_vocab.txt’, en_vocab) write_vocab_file(‘fr_vocab.txt’, fr_vocab)
fr_tokenizer = tf_text.BertTokenizer(‘fr_vocab.txt’, **bert_tokenizer_params) en_tokenizer = tf_text.BertTokenizer(‘en_vocab.txt’, **bert_tokenizer_params)
Tokenize the examples -> (batch, word, word-piece)
en_tokenizere = en_tokenizer.tokenize(“hello how are you Vadim”)
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.