<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[AI and fintech notes]]></title><description><![CDATA[Sharing my thoughts on AI and fintech topics. ]]></description><link>https://milenkraev.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!A-zC!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4c56ee2-9c47-43db-b896-e49e18ba9085_144x144.png</url><title>AI and fintech notes</title><link>https://milenkraev.substack.com</link></image><generator>Substack</generator><lastBuildDate>Thu, 07 May 2026 19:24:41 GMT</lastBuildDate><atom:link href="https://milenkraev.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Milen Kraev]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[milenkraev@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[milenkraev@substack.com]]></itunes:email><itunes:name><![CDATA[Milen Kraev]]></itunes:name></itunes:owner><itunes:author><![CDATA[Milen Kraev]]></itunes:author><googleplay:owner><![CDATA[milenkraev@substack.com]]></googleplay:owner><googleplay:email><![CDATA[milenkraev@substack.com]]></googleplay:email><googleplay:author><![CDATA[Milen Kraev]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Understanding Python List Comprehensions]]></title><description><![CDATA[A Simple Guide]]></description><link>https://milenkraev.substack.com/p/understanding-python-list-comprehensions</link><guid isPermaLink="false">https://milenkraev.substack.com/p/understanding-python-list-comprehensions</guid><dc:creator><![CDATA[Milen Kraev]]></dc:creator><pubDate>Wed, 16 Oct 2024 12:15:25 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!A-zC!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4c56ee2-9c47-43db-b896-e49e18ba9085_144x144.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>List comprehensions are one of Python's most elegant features, allowing you to create lists in a concise and readable way. Rather than using multiple lines of code with loops and conditionals to build a list, a list comprehension lets you do the same thing in just one line. This can make your code not only shorter but also more efficient.</p><p>At its core, a list comprehension is a way to generate a new list by applying an expression to each item in an existing list, range, or any other iterable. The basic idea is to take a structure you&#8217;re looping over and apply some transformation or filtering criteria to create a new list. The resulting syntax is simple and clean, often replacing what might take several lines of code with a single, readable statement.</p><p>Imagine you want to create a list of squared numbers for a given range, say from 1 to 5. Using a traditional <code>for</code> loop, you would write something like this:</p><p><code>squares = []</code></p><p><code>for x in range(1, 6):</code></p><p><code>  squares.append(x ** 2)</code></p><p>This loop goes through each number in the range, squares it, and appends the result to the list.</p><p>With list comprehensions, this process is reduced to a single line:</p><p><code>squares = [x ** 2 for x in range(1, 6)]</code></p><p>In this case, <code>x ** 2</code> is the expression being applied to each item, and <code>for x in range(1, 6)</code> is the loop that iterates over the numbers from 1 to 5. The list comprehension simplifies the process while achieving the same result, giving you a list of squared numbers: <code>[1, 4, 9, 16, 25]</code>.</p><p>This is a more concise and readable way to achieve the same goal.</p><p>This feature shines especially when you need to apply a quick transformation or filter some data. For example, if you only want even numbers from a sequence, you can add a simple condition to the list comprehension. Instead of writing extra lines to check each element and decide whether to include it, you simply add an <code>if</code> clause at the end. The result is a filtered and transformed list with minimal code.</p><p>Let&#8217;s look at an example to demonstrate this concept.</p><p>Suppose you want to filter out only the even numbers from a range of numbers, say from 1 to 10. With a traditional <code>for</code> loop, you&#8217;d need to check each number and decide whether to append it to the list:</p><p><code>even_numbers = []</code></p><p><code>for x in range(1, 11):</code></p><p><code>  if x % 2 == 0:</code></p><p><code>    even_numbers.append(x)</code></p><p>Here, you&#8217;re using an <code>if</code> statement inside the loop to filter out the odd numbers.</p><p>With a list comprehension, this filtering process is much simpler. You can add the condition directly at the end of the comprehension:</p><p><code>even_numbers = [x for x in range(1, 11) if x % 2 == 0]</code></p><p>In this version, <code>x for x in range(1, 11)</code> iterates over the numbers from 1 to 10, and the condition <code>if x % 2 == 0</code> filters out only the even numbers. The result is a concise, single-line expression that gives you a list of even numbers: <code>[2, 4, 6, 8, 10]</code>.</p><p>This makes your code both more compact and easier to read.</p><p>Though list comprehensions can be very powerful, it&#8217;s important to balance efficiency with readability. The simplicity and speed they offer can make them tempting to use for everything. But if your transformation logic becomes too complex, it may be better to use a traditional loop. Sometimes, fitting everything into one line can make the code harder to understand, especially for someone reading it for the first time.</p><p>In general, list comprehensions are ideal for tasks that involve basic transformations or filtering. They let you focus on <em>what</em> you want the result to be rather than <em>how</em> to achieve it, which is a key part of writing Pythonic code. However, for more complicated logic or operations involving multiple steps, sticking to a more explicit approach using loops can make your code clearer.</p>]]></content:encoded></item><item><title><![CDATA[AutoTokenizer]]></title><description><![CDATA[Hugging Face and what they are doing]]></description><link>https://milenkraev.substack.com/p/autotokenizer</link><guid isPermaLink="false">https://milenkraev.substack.com/p/autotokenizer</guid><dc:creator><![CDATA[Milen Kraev]]></dc:creator><pubDate>Tue, 27 Aug 2024 13:52:27 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!A-zC!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4c56ee2-9c47-43db-b896-e49e18ba9085_144x144.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hugging Face is a leading company in artificial intelligence (AI) and natural language processing (NLP) that has become synonymous with open-source tools and community-driven AI development. Founded in 2016, Hugging Face initially began as a chatbot app aimed at casual conversation but quickly shifted focus to become a central hub for AI research, particularly in language models.</p><p>At the heart of Hugging Face&#8217;s offerings is the Transformers library, an open-source project that has dramatically transformed the use of deep learning models in NLP. This library provides pre-trained models that can be easily fine-tuned for a variety of tasks, such as text classification, translation, and question answering. By doing so, Hugging Face has made cutting-edge NLP technologies more accessible, allowing developers and researchers to utilize powerful models without needing extensive computational resources or deep machine learning expertise.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://milenkraev.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI and fintech notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>A key feature of Hugging Face&#8217;s platform is the Model Hub, a repository where users can share, discover, and use models created by the community. This collaborative approach has nurtured a vibrant ecosystem where researchers and developers contribute models trained on diverse datasets, covering a wide range of languages and applications. Hugging Face has also expanded its support beyond NLP to include models for tasks in computer vision and audio processing.</p><p>Hugging Face has developed various tools and frameworks that simplify the deployment and scaling of machine learning models in production environments. The Hugging Face Hub, for instance, offers a cloud-based platform for hosting models, making it easy for users to integrate AI into their applications through APIs. The company also provides services like the Inference API, which allows developers to run models at scale without worrying about infrastructure management.</p><p>Hugging Face's influence extends deeply into the research community, where their tools have been instrumental in pushing the boundaries of AI. The company regularly collaborates with academic institutions and research labs, and their models frequently set benchmarks in NLP tasks. Hugging Face is also committed to promoting ethical AI, encouraging conversations about biases in models and the responsible use of AI technologies.</p><p>Hugging Face is a trailblazer in AI and NLP, building a rich ecosystem of open-source tools, community-driven model sharing, and accessible AI services. Their contributions have significantly lowered the barriers to entry in AI, empowering developers, researchers, and organizations to harness the potential of machine learning in innovative and impactful ways.</p><p>The <code>AutoTokenizer</code> is an integral component of the Hugging Face Transformers library, designed to simplify the process of preparing text data for use with different transformer models. Tokenization, the process of converting text into a format that a machine learning model can understand, is crucial in natural language processing (NLP). Each transformer model, whether it be BERT, GPT, or others, has its own specific way of tokenizing text. The <code>AutoTokenizer</code> class streamlines this process by automatically selecting the correct tokenizer based on the model you are using.</p><p>When working with transformer models, the text must first be broken down into smaller units, often words or subwords, which are then converted into numerical representations (tokens) that the model can process. Tokenizers also handle various other tasks such as adding special tokens required by the models (like <code>[CLS]</code> and <code>[SEP]</code> for BERT), padding sequences to the same length, and managing the attention masks that tell the model which parts of the input it should focus on. The <code>AutoTokenizer</code> automates all of these tasks, allowing users to seamlessly transition between different models without needing to worry about the specifics of each tokenizer.</p><p>To use the <code>AutoTokenizer</code>, one simply needs to specify the name of the model they intend to use, and the class will automatically load the appropriate tokenizer. This is particularly useful because it abstracts away the complexities involved in tokenization, making it easier for developers and researchers to experiment with different models. For example, if you switch from BERT to GPT-2, the <code>AutoTokenizer</code> will automatically adjust to use the correct tokenization method for GPT-2, including handling the distinct tokenization process and special tokens that GPT-2 requires.</p><p>Additionally, the <code>AutoTokenizer</code> is designed to be highly flexible and customizable. While it typically selects the default tokenizer associated with a given model, users can fine-tune the tokenizer settings according to their specific needs. This might include adjusting the vocabulary, changing the tokenization strategy (such as switching from word-level to subword-level tokenization), or modifying the special tokens used. Despite this flexibility, the <code>AutoTokenizer</code> maintains a user-friendly interface, making these adjustments straightforward.</p><p>In summary, the <code>AutoTokenizer</code> is a powerful tool within the Hugging Face Transformers library that simplifies the tokenization process for a wide range of transformer models. By automatically selecting the appropriate tokenizer and managing the intricacies of tokenization, it allows users to focus on developing and fine-tuning models without getting bogged down in the technical details of how each model processes text. This automation and ease of use make the <code>AutoTokenizer</code> an essential component for anyone working with transformer models in NLP.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://milenkraev.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI and fintech notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[How your model is fed]]></title><description><![CDATA[Tokenization]]></description><link>https://milenkraev.substack.com/p/how-your-model-is-fed</link><guid isPermaLink="false">https://milenkraev.substack.com/p/how-your-model-is-fed</guid><dc:creator><![CDATA[Milen Kraev]]></dc:creator><pubDate>Fri, 23 Aug 2024 12:11:02 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!A-zC!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4c56ee2-9c47-43db-b896-e49e18ba9085_144x144.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A tokenizer in natural language processing (NLP) plays a key role in preparing raw text for machine learning models. It starts by taking the input text, which can be anything from a single sentence to an entire document, and breaking it down into smaller units called tokens. Depending on the tokenizer's design and the model it's paired with, these tokens might be words, subwords, or even individual characters.</p><p>After tokenizing the text, the tokenizer converts each token into a numerical identifier, known as a token ID, using a predefined vocabulary. This vocabulary is essentially a list of all the tokens the model has been trained to recognize. By mapping tokens to their corresponding IDs, the tokenizer transforms the text into a sequence of numbers that the machine learning model can process.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://milenkraev.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI and fintech notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Beyond simple tokenization, the tokenizer also handles a range of preprocessing tasks. It might add special tokens to mark the start or end of a sentence, indicate the separation between different parts of the input, or provide clues for sentence segmentation. The tokenizer can also pad sequences to ensure they all have the same length, which is necessary for batch processing in models that require fixed-size inputs.</p><p>If the text is too long, the tokenizer can truncate it to fit within a specified maximum length, ensuring it works smoothly with the model's architecture. In tasks that involve text pairs, like question-answering, the tokenizer encodes the pairs in a way that the model can understand their relationship.</p><p>Once the tokenizer has done its work, it outputs a dictionary of tensors&#8212;arrays of numbers that represent the token IDs and other necessary information, such as attention masks. This output is then fed into a machine learning model for tasks like text classification, translation, or summarization. In essence, the tokenizer transforms human language into a format that models can understand and work with effectively.</p><p>The <code>AutoTokenizer</code> in NLP is a versatile tool that simplifies the process of preparing text data for machine learning models. It automatically selects the appropriate tokenizer class based on the model you're using, making it easier to work with different models without needing to manually configure the tokenizer.</p><p>When you load an <code>AutoTokenizer</code> with a specific model name, it initializes the tokenizer that is best suited for that model. For instance, if you load the tokenizer for a BERT model, it will choose the <code>BertTokenizer</code>, and if you're using a T5 model, it will pick the <code>T5Tokenizer</code>. This flexibility allows you to switch between models seamlessly while ensuring that the text processing is always optimized for the particular architecture of the model you're using.</p><p>The <code>AutoTokenizer</code> handles all the usual tasks of a tokenizer, such as splitting text into tokens, converting those tokens into numerical IDs based on the model&#8217;s vocabulary, and adding any special tokens required by the model. It also manages tasks like padding sequences to a uniform length, truncating overly long inputs, and handling paired inputs for tasks like question-answering or text similarity.</p><p>One of the biggest advantages of using <code>AutoTokenizer</code> is its ability to abstract away the complexities of different tokenizer implementations. You don't need to worry about the specific details of each model's tokenizer; the <code>AutoTokenizer</code> automatically configures itself to work with the model you've chosen, ensuring that your input text is processed correctly.</p><p>Once the text has been tokenized and processed by the <code>AutoTokenizer</code>, it outputs a set of tensors&#8212;numeric representations of the text that the machine learning model can then use for various tasks, such as classification, translation, or text generation. This automation and flexibility make the <code>AutoTokenizer</code> an essential tool for efficiently preparing text data in NLP workflows.</p><p>There are several alternatives to <code>AutoTokenizer</code> that you can consider depending on your specific needs in natural language processing (NLP).</p><p>One alternative is to use model-specific tokenizers, such as <code>BertTokenizer</code>, <code>GPT2Tokenizer</code>, or <code>T5Tokenizer</code>. These tokenizers are tailored to the architecture of their respective models. Instead of relying on <code>AutoTokenizer</code>, you can directly choose the tokenizer associated with the model you're using. This approach provides more control and allows access to features unique to each model&#8217;s tokenizer.</p><p>Another option is manual tokenization using general-purpose NLP libraries. For example, SpaCy is a powerful NLP library that offers comprehensive tokenization along with other text processing capabilities like part-of-speech tagging, named entity recognition, and dependency parsing. SpaCy&#8217;s tokenization is highly customizable and can be a good choice if you need more fine-grained control over how text is processed.</p><p>You could also consider the <code>nltk</code> library, which is another well-established tool in the NLP community. <code>nltk</code> provides basic tokenization methods, such as word and sentence tokenizers, and can be useful for simpler tokenization tasks or when working within traditional NLP pipelines.</p><p>Another option is the <code>SentencePiece</code> tokenizer, which is often used for training tokenizers on custom datasets. SentencePiece can be used with models like BERT and GPT, and it&#8217;s particularly useful when you need to create subword tokenization from scratch for a new language or domain-specific vocabulary.</p><p>Finally, the <code>Tokenizer</code> class from the <code>Keras</code> library can be used for tokenizing text when working in TensorFlow-based environments. This tokenizer is simple and effective for many standard machine learning tasks, especially when training neural networks for text classification or sequence modeling.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://milenkraev.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI and fintech notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Train your first mini model. ]]></title><description><![CDATA[Working example.]]></description><link>https://milenkraev.substack.com/p/train-your-first-mini-model</link><guid isPermaLink="false">https://milenkraev.substack.com/p/train-your-first-mini-model</guid><dc:creator><![CDATA[Milen Kraev]]></dc:creator><pubDate>Tue, 20 Aug 2024 12:34:46 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!A-zC!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4c56ee2-9c47-43db-b896-e49e18ba9085_144x144.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the upcoming posts, I&#8217;ll be diving into several concepts related to machine learning. At first, some of these ideas might seem disconnected, but don't worry&#8212;I&#8217;ve got you covered. To set the stage, I'm sharing a simple model example here. I&#8217;ll walk you through the different approaches used in this example step by step. If it feels a bit overwhelming at first, don&#8217;t lose heart&#8212;every line will be explained in due course.</p><p><strong>import tensorflow as tf</strong><br><strong>from transformers import TFAutoModelForSeq2SeqLM, AutoTokenizer</strong><br><strong>from sklearn.model_selection import train_test_split</strong><br><strong>import pandas as pd</strong></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://milenkraev.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI and fintech notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>This code snippet sets up the necessary libraries for a machine learning project focused on natural language processing (NLP). It begins by importing TensorFlow, a powerful framework for building and training deep learning models. Next, it brings in components from the transformers library, specifically tools for working with sequence-to-sequence models and tokenizing text. Additionally, the train_test_split function from Scikit-learn is imported, which is essential for splitting data into training and testing sets. Finally, the pandas library is imported, providing tools for data manipulation and analysis, particularly when working with structured data in tables. Together, these imports prepare the environment for developing, training, and evaluating a machine learning model in the context of NLP tasks.</p><p><strong>training_data = pd.read_csv (r'yourpath.yourfile.csv.csv')</strong></p><p>This line of code reads a CSV file containing training data into a pandas DataFrame. The pd.read_csv() function is used to load the data from the specified file path (r'yourpath.yourfile.csv'). The raw string notation (r'') ensures that any backslashes in the file path are treated correctly. Once loaded, the data is stored in the training_data variable, which can then be used for further processing, analysis, or model training.</p><p><strong>X = training_data.drop(columns=['Column for the training data']).applymap(str)</strong></p><p><strong>X_combined = X.apply(lambda row: ' '.join(row), axis=1)</strong></p><p><strong>y = training_data['Work notes (Internal)'].tolist()</strong></p><p><strong>train_texts, val_texts, train_summaries, val_summaries = train_test_split(</strong><br><strong>    X_combined.tolist(), y, test_size=0.2, random_state=42)</strong></p><p>This code processes the training data by first removing the 'Column for the training data' column and converting the remaining data to strings. The next step combines all the columns in each row into a single string, creating a text representation for each data entry. The 'Column for the training data' column is then extracted as the target variable. Finally, the code splits the combined text data and the corresponding target values into training and validation sets using an 80-20 split. This prepares the data for model training by separating it into inputs (the combined text) and outputs ('Column for the training data') while also reserving a portion of the data for validation.</p><p><strong>model_name = "t5-small"</strong><br><strong>tokenizer = AutoTokenizer.from_pretrained(model_name)</strong><br><strong>model = TFAutoModelForSeq2SeqLM.from_pretrained(model_name)</strong></p><p>This code initializes a sequence-to-sequence model using the "t5-small" architecture. First, the model name "t5-small" is defined. Then, the AutoTokenizer is loaded with the pre-trained tokenizer specific to this model, which handles converting text into tokens that the model can understand. Finally, the TFAutoModelForSeq2SeqLM is loaded with the pre-trained "t5-small" model, which is ready to be fine-tuned or used for tasks like text summarization, translation, or other sequence-to-sequence tasks.</p><p><strong>def tokenize_data(texts, summaries):</strong><br><strong>    inputs = tokenizer(texts, max_length=512, truncation=True, padding='max_length', return_tensors="tf")</strong><br><strong>    targets = tokenizer(summaries, max_length=150, truncation=True, padding='max_length', return_tensors="tf")</strong><br><strong>    return inputs, targets</strong></p><p><strong>train_inputs, train_targets = tokenize_data(train_texts, train_summaries)</strong><br><strong>val_inputs, val_targets = tokenize_data(val_texts, val_summaries)</strong></p><p>This code defines a function called tokenize_data that tokenizes both the input texts and summaries using the pre-trained tokenizer. The function processes the texts by truncating them to a maximum length (512 for inputs and 150 for summaries) and padding them to ensure consistent input sizes. The tokenized outputs are returned as TensorFlow tensors. After defining the function, the code tokenizes the training and validation data by passing the respective texts and summaries through the tokenize_data function, resulting in tokenized inputs and targets for both the training and validation sets.</p><p><strong>train_dataset = tf.data.Dataset.from_tensor_slices((dict(train_inputs), train_targets['input_ids']))</strong><br><strong>val_dataset = tf.data.Dataset.from_tensor_slices((dict(val_inputs), val_targets['input_ids']))</strong></p><p>This code snippet converts the tokenized data into TensorFlow Datasets. Specifically, it uses tf.data.Dataset.from_tensor_slices to create datasets from the tokenized input and target data. For train_dataset, it takes the tokenized input data (train_inputs) and the target input IDs from train_targets and combines them into a dataset. Similarly, val_dataset is created from the tokenized validation inputs (val_inputs) and the target input IDs from val_targets. The result is two TensorFlow Datasets, one for training and one for validation, which can be used for model training and evaluation.</p><p><strong>batch_size = 8</strong><br><strong>train_dataset = train_dataset.shuffle(len(train_texts)).batch(batch_size)</strong><br><strong>val_dataset = val_dataset.batch(batch_size)</strong></p><p>This code snippet prepares the TensorFlow Datasets for training and validation by shuffling and batching the data. For train_dataset, the data is first shuffled using the shuffle method, which randomizes the order of the samples to improve training performance. The shuffle buffer size is set to the length of the training texts to ensure thorough mixing. Then, the data is batched with a batch_size of 8, meaning each batch will contain 8 samples. The val_dataset is batched with the same batch size of 8, but it is not shuffled, as shuffling is typically not necessary for validation data. This setup is essential for efficient training and evaluation of the model.</p><p><strong>model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=3e-5),</strong><br><strong>              loss=model.compute_loss)  # Use the model's built-in loss function</strong></p><p><strong>model.fit(train_dataset, validation_data=val_dataset, epochs=3)</strong></p><p><strong>model.save_pretrained("fine_tuned_t5")</strong></p><p>This code snippet configures and trains the model, then saves the fine-tuned version. The model.compile function sets up the training process with the Adam optimizer and a learning rate of 3&#215;10&#8722;53 \times 10^{-5}3&#215;10&#8722;5. It uses the model's built-in loss function for training. The model.fit function trains the model using the prepared train_dataset, validates it with val_dataset, and runs for 3 epochs. Finally, model.save_pretrained("fine_tuned_t5") saves the trained model to the specified directory, "fine_tuned_t5", for later use or deployment.</p><p><strong>new_incident_data = pd.read_csv(r'yourpath.yourfile.csv')</strong><br><strong>new_incident_text = ' '.join(new_incident_data.applymap(str).iloc[0])</strong></p><p>This code snippet reads a new CSV file containing incident data into a pandas DataFrame. The pd.read_csv() function loads the data from the specified path (r'yourpath.yourfile.csv'). Then, it combines all the columns in the first row of the DataFrame into a single string. The applymap(str) converts all values to strings, and iloc[0] selects the first row. The join method concatenates these string values into one long text string, representing the combined data of the first incident.</p><p><strong>input_text = "summarize: " + new_incident_text</strong><br><strong>input_ids = tokenizer.encode(input_text, return_tensors="tf", max_length=512, truncation=True)</strong></p><p><strong>summary_ids = model.generate(input_ids, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)</strong><br><strong>summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)</strong></p><p><strong>print("Original Text:", new_incident_text)</strong><br><strong>print("Generated Summary:", summary)</strong></p><p>This code snippet generates a summary for a new incident description using the fine-tuned model. It starts by prepending the text "summarize: " to the new incident data, creating a prompt for the model. The tokenizer.encode function converts this input text into token IDs, ensuring that it fits within the maximum length of 512 tokens.</p><p>The model.generate function then produces a summary of the input text. It specifies parameters such as a maximum summary length of 150 tokens, a minimum length of 40 tokens, a length penalty of 2.0 to discourage overly long summaries, and uses beam search with 4 beams to improve the quality of the generated summary. The early_stopping=True parameter helps terminate the generation process when the model predicts the end of the sequence.</p><p>Finally, tokenizer.decode converts the generated summary token IDs back into a readable string, and both the original text and the generated summary are printed.</p><p>This example demonstrates a model trained to summarize incident reports from ServiceNow in CSV format. The core idea is to provide a summary of an incident. While it's a simple task, it's a solid foundation for understanding the overall process. Feel free to experiment with the code and run it yourself. Try to grasp what each line does, as these small steps will eventually lead you to train your own models one day.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://milenkraev.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI and fintech notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Clone yourself with an AI. ]]></title><description><![CDATA[How to start from zero.]]></description><link>https://milenkraev.substack.com/p/clone-yourself-with-an-ai</link><guid isPermaLink="false">https://milenkraev.substack.com/p/clone-yourself-with-an-ai</guid><dc:creator><![CDATA[Milen Kraev]]></dc:creator><pubDate>Fri, 09 Aug 2024 14:18:23 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!A-zC!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4c56ee2-9c47-43db-b896-e49e18ba9085_144x144.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The rapid advancement of AI technology is raising safety concerns, and everyone is asking, "Where is the limit?" But let's stay positive. Isn&#8217;t it great to have a virtual twin that handles tedious tasks for you? The ultimate goal of corporations is to automate everything and maximize their profits. This should also apply to individuals. Imagine if some AI models could learn exactly what you know and work on your behalf while you still earn the money. You could focus on your health, schedule vacations, and tailor your activities to your needs.</p><p>I believe the future of AI belongs not just to big tech companies or a select few but to everyone. We can start taking small steps towards this inclusive future right now.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://milenkraev.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI and fintech notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>So, what are the basics you can start learning to build your own AI model? Even if you have very limited programming experience, it's a great way to gain some skills. Try to make it a hobby: read articles like this one, and then use ChatGPT to ask about any words or concepts you don&#8217;t understand.</p><p>Let's get started with the following basics:</p><p>Python</p><p>Python is a versatile and beginner-friendly programming language known for its simple, easy-to-read syntax. It's a great starting point for anyone new to coding because it emphasizes readability and reduces the complexity of writing code. Python can be used for a wide range of applications, from web development to data analysis and artificial intelligence. Its large and supportive community also means you'll have plenty of resources and libraries at your disposal to help you learn and build projects. Whether you&#8217;re interested in creating websites, analyzing data, or automating tasks, Python provides the tools and simplicity to get you started on the right foot.</p><p>Pandas</p><p>Pandas is a powerful and user-friendly library in Python that makes working with data much easier. It&#8217;s especially helpful for beginners who want to manipulate and analyze data without getting bogged down by complex coding. With Pandas, you can quickly organize your data into tables, perform calculations, and generate reports with just a few lines of code. It&#8217;s perfect for tasks like cleaning up messy data, combining datasets, and visualizing information. Thanks to its intuitive design and extensive documentation, Pandas allows you to focus on extracting insights from your data rather than struggling with technical details.</p><p>Numpy</p><p>NumPy is a fundamental library in Python for scientific computing, and it's a fantastic tool for beginners interested in working with numerical data. It provides powerful data structures, like arrays, that let you perform complex mathematical operations efficiently. Unlike regular lists in Python, NumPy arrays can handle large amounts of data quickly and allow for operations like addition, multiplication, and more, to be done in a single line of code. It also offers a range of functions for tasks such as linear algebra, statistical analysis, and random number generation. With its straightforward syntax and broad functionality, NumPy is an essential tool for anyone diving into data science or numerical analysis.</p><p>TensorFlow</p><p>TensorFlow is a popular library developed by Google for building and training machine learning models. It&#8217;s especially useful for beginners who want to dive into artificial intelligence and deep learning. TensorFlow simplifies complex processes by providing high-level tools and flexible frameworks that let you create neural networks and perform tasks like image recognition or language translation. Its user-friendly design helps you experiment with and optimize models while handling the heavy lifting of computations. Whether you're just starting out or looking to build sophisticated AI systems, TensorFlow offers the resources and support you need to get started and advance in the world of machine learning.</p><p>These three libraries are a great starting point. Dive into the any information related and make sure to ask ChatGPT if you have any questions along the way.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://milenkraev.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI and fintech notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Coming soon]]></title><description><![CDATA[This is AI and fintech notes.]]></description><link>https://milenkraev.substack.com/p/coming-soon</link><guid isPermaLink="false">https://milenkraev.substack.com/p/coming-soon</guid><dc:creator><![CDATA[Milen Kraev]]></dc:creator><pubDate>Mon, 10 Apr 2023 17:48:09 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!A-zC!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4c56ee2-9c47-43db-b896-e49e18ba9085_144x144.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is AI and fintech notes.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://milenkraev.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://milenkraev.substack.com/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item></channel></rss>