Monday, December 25, 2023

Mastering NumPy: A Step-by-Step Learning Guide

Step 1: Installing NumPy

pip install numpy

Step 2: Importing NumPy

import numpy as np

Step 3: NumPy Arrays

1D Array

arr_1d = np.array([1, 2, 3])

2D Array

arr_2d = np.array([[1, 2, 3], [4, 5, 6]])

3D Array

arr_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

Step 4: Array Operations

Element-wise Addition

result = arr_1d + arr_1d

Element-wise Multiplication

result = arr_2d * arr_2d

Dot Product of Matrices

result = np.dot(arr_2d, arr_2d.T)

Step 5: Saving and Loading Data

Saving Data

np.save('my_array', arr_1d)

Loading Data

loaded_array = np.load('my_array.npy')

Wednesday, December 20, 2023

Creating and Using Virtual Environments in Visual Studio Code on macOS

Virtual environments are crucial for managing Python project dependencies. In this tutorial, we'll explore how to create and activate a virtual environment within Visual Studio Code on macOS.

Step 1: Open Visual Studio Code

Launch Visual Studio Code and open your Python project.

Step 2: Open a New Terminal

Go to the "View" menu and select "Terminal" or press Ctrl + ` to open a new terminal in Visual Studio Code.

Step 3: Create a Virtual Environment

python3 -m venv venv

This creates a virtual environment named venv. Make sure you have Python 3 installed; if not, use Homebrew to install it (brew install python).

Step 4: Activate the Virtual Environment

source venv/bin/activate

Your terminal prompt should change, indicating that you are now in the virtual environment.

Step 5: Install Dependencies (Optional)

While in the virtual environment, use pip to install project-specific dependencies:

pip install package_name

Step 6: Deactivate the Virtual Environment

Once done, deactivate the virtual environment:

deactivate

This returns you to the global Python environment.

Conclusion

Creating and using virtual environments in Visual Studio Code provides a clean space for managing project dependencies, crucial for smooth development.

Happy coding!

Sunday, August 13, 2023

Amazon Bedrock - Rocks





In this article, I aim to demystify AWS Bedrock, shedding light on its core functionalities, the array of currently available Foundation Models (FMs), and their versatile applications across various use cases.

1. What is Amazon Bedroack?

Amazon Bedrock is the latest machine learning platform that was released by Amazon Web Services(AWS) that enables users an easy way to build and scale generative AI applications with foundation models, or “FMs” for short. This service accelerates the development of generative AI applications using foundational models without managing any physical infrastructure or the high operational overhead that usually comes with it. You can choose from different foundation models to cater for your particular use case such as from AI21 Labs, Anthropic, Stability AI, or internally from AWS. You can privately customize FMs using your organization’s data and utilize AWS tools and capabilities that you are familiar with, which empower you to deploy scalable, reliable as well as highly secure generative AI applications.

2. So what can Amazon Bedrock do?

Amazon Bedrock is a machine learning platform that is similar with Amazon SageMaker. Both of the services have multiple sub-components/services, but the latter is more complicated and is primarily used by Machine Learning Engineers to build and train custom machine learning models. This is different from Amazon Bedrock which is meant to be a more user-friendly platform for building and scaling generative AI applications through the use of foundation models (FMs).

Amazon Bedrock can be used in various use cases such as:
Text generation : You can generate new pieces of original written content like short stories, social media posts, articles, web page copy, or even school essays.
Chatbots: Amazon Bedrock is capable of building conversational interfaces like chatbots and virtual assistants to improve the user experience for your clients. It is possible that this platform can provide a direct integration with Amazon Lex (a chatbot service in AWS).
Search: This service will allow its users to easily search, find or even synthesize information to quickly answer various questions from a large collection of diverse data.
Text summarization: You can use this service to summarize textual content – from blog posts, essays, books, documents and others to get a concise summary of the subject matter – removing the need to read the full content in verbose.
Image generation: Amazon Bedrock can create a realistic and artistic picture of different subjects, environments, and scenes that you specify.
Personalization: This AI service can also personalize the way you deal with your customer – allowing your users to search exactly what they’re looking for with more relevant and contextual product recommendations, which is way better than just mere keyword matching.

3. What on earth is a Foundation Model (FM), anyway?

According to an insightful article from Stanford, Foundation Models are defined as "models trained on comprehensive data that can be flexibly adapted to a wide spectrum of subsequent tasks."

Another perspective comes from the Center for Research on Foundation Models (CRFM) at Stanford University: In recent times, a remarkably successful approach to constructing AI systems has emerged: Develop a single model through extensive data training and tailor it to suit numerous applications. This model earns the title of a foundation model.

To put it simply, a Foundation Model is akin to a bedrock – it can be shaped and molded to serve various functions, unlike the traditional approach of constructing a highly specialized Machine Learning model for a specific use case. This concept encourages collaboration and unity among diverse contributors, enabling a singular model to be repurposed and employed with enhanced efficiency. Rather than investing countless hours in training a bespoke model designed for a single task, the emphasis shifts to constructing a pliable and versatile model capable of executing a multitude of tasks.

4. How can I access and try the new Amazon Bedrock service?

To date, the Amazon Bedrock service is not yet available in the AWS Management Console, it is yet to be in general availability. However, you can express interest to get early access. The currently available FMs are:
Amazon - Titan Text(auto NLP), Titan Embeddings (search).
AI21 - Jurassic 2 (generate multilingual text)
Anthropic - Claude 2(automate Q&A, conversation)
Stability AI - Stable Diffusion(generate Images)
Cohere - Command and Embed (text generation 100+ languages)

Once you have selected the provider and model, it is important to understand the inference configuration. These configuration inputs are important to drive the desired results using the API. Common inference parameter definitions:

Randomness and Diversity - Foundation models support the following parameters to control randomness and diversity in the response.
Temperature – Large language models use probability to construct the words in a sequence. For any given next word, there is a probability distribution of options for the next word in the sequence. When you set the temperature closer to zero, the model tends to select the higher-probability words. When you set the temperature further away from zero, the model may select a lower-probability word.

In technical terms, the temperature modulates the probability density function for the next tokens, implementing the temperature sampling technique. This parameter can deepen or flatten the density function curve. A lower value results in a steeper curve with more deterministic responses, and a higher value results in a flatter curve with more random responses.

Top K – Temperature defines the probability distribution of potential words, and Top K defines the cut off where the model no longer selects the words. For example, if K=50, the model selects from 50 of the most probable words that could be next in a given sequence. This reduces the probability that an unusual word gets selected next in a sequence. In technical terms, Top K is the number of the highest-probability vocabulary tokens to keep for Top- K-filtering - This limits the distribution of probable tokens, so the model chooses one of the highest- probability tokens.

Top P – Top P defines a cut off based on the sum of probabilities of the potential choices. If you set Top P below 1.0, the model considers the most probable options and ignores less probable ones. Top P is similar to Top K, but instead of capping the number of choices, it caps choices based on the sum of their probabilities. For the example prompt "I hear the hoof beats of ," you may want the model to provide "horses," "zebras" or "unicorns" as the next word. If you set the temperature to its maximum, without capping Top K or Top P, you increase the probability of getting unusual results such as "unicorns." If you set the temperature to 0, you increase the probability of "horses." If you set a high temperature and set Top K or Top P to the maximum, you increase the probability of "horses" or "zebras," and decrease the probability of "unicorns."

Length - The following parameters control the length of the generated response.

Response length – Configures the minimum and maximum number of tokens to use in the generated response.

Length penalty – Length penalty optimizes the model to be more concise in its output by penalizing longer responses. Length penalty differs from response length as the response length is a hard cut off for the minimum or maximum response length.

In technical terms, the length penalty penalizes the model exponentially for lengthy responses. 0.0 means no penalty. Set a value less than 0.0 for the model to generate longer sequences, or set a value greater than 0.0 for the model to produce shorter sequences.

Repetitions - The following parameters help control repetition in the generated response.

Repetition penalty (presence penalty) – Prevents repetitions of the same words (tokens) in responses. 1.0 means no penalty. Greater than 1.0 decreases repetition.

I hope you now have a good understanding of Amazon Bedrock service and how you can use it. Thank you for reading, and I hope you found this information informative and valuable.

Monday, July 31, 2023

Demystify GenAI on AWS

When embarking on my AI journey, I was overwhelmed by the abundance of information, and it took me considerable time to navigate and construct a clear mental map. 


This blog is entirely based on my personal experience and understanding, which continues to evolve as I delve deeper into the subject. However, I firmly believe that grasping the fundamentals and mastering the basics is essential before getting into more advanced concepts and drawing meaningful inferences, as is the case with any subject."


I am excited to share my experiences and insights with others. Today, let's begin with GenAI on AWS, the focal point of discussions in contemporary software solutions, particularly within the cloud domain.


As you delve into GenAI, you might encounter various topics such as deep learning, natural language processing, computer vision, and more. Each of these areas plays a significant role in advancing AI technologies and applications. Exploring how GenAI can be leveraged within cloud solutions can lead to exciting possibilities and insights.


Please feel free to share your thoughts, findings, and any challenges you come across during your exploration of GenAI. Remember that AI is an ever-evolving field, and sharing your experiences can contribute to the collective knowledge and understanding of this cutting-edge technology.


I believe two key points are crucial when understanding any new concept or technology: a) comprehending what the technology is and b) understanding how this technology will be used or consumed (which refers to its practical use cases).


First, let's explore a fascinating technology called GenAI, which stands for Generative AI. It's like having a super creative robot that functions as an artist or a musician. Unlike a regular robot that follows fixed rules, this one can generate original creations, such as drawings, stories, or even music! The second part focuses on the diverse consumers who leverage this remarkable technology for various use cases.


As AWS is my primary domain of expertise, I will concentrate on showcasing what AWS has to offer in this area while explaining the concepts. This focus will enable me to present practical, hands-on examples using AWS AI/ML Services in my future blogs. Considering the rapid evolution of this field, it becomes imperative to stay updated on the new services being added or modified, thereby ensuring that AI remains easily accessible for end users to adopt and leverage.


Now, let's attempt to answer a few fundamental questions from the AWS perspective and then delve deeper into each of these areas. By doing so, we'll gain a better understanding of how AWS makes GenAI both fantastic and enjoyable to use!


  1. What is generative AI? 

Generative AI is a type of artificial intelligence that can create new content and ideas, including conversations, stories, images, videos, and music.

  1. What are foundation models (FM)? 

Generative AI is powered by very large machine learning models that are pre-trained on vast collections of data, and are commonly referred to as foundation models, or FMs. 

  1. What types of foundation models are currently available?*  

There are currently three main foundation model types currently available: 

  1. Text-to-text: These natural language processing models can summarise text, extract information, respond to questions, and create content such as blogs or product descriptions. An example is sentence auto-completion.

  2. Text-to-embeddings: These FMs compare user search bar input with index data and connect the dots between the two. The result is more accurate and relevant.

  3. Multimodal: These emerging foundation models can generate images based on a user's natural language text input.  

  1. What Generative AI solutions AWS currently offers?

AWS currently offers four main GenAI solutions for customers:

  1. Amazon Bedrock (LLM to transform existing business function viz operations, customer support, marketing): The easiest way to build and scale generative AI applications with FMs:

    1. Amazon - Titan Text(auto NLP), Titan Embeddings (search).  

    2. AI21 - Jurassic 2 (generate multilingual text

    3. Anthropic - Claude 2(automate Q&A, conversation)  

    4. Stability AI - Stable Diffusion(generate Images

    5. Cohere - Command and Embed (text generation 100+ languages

  2. AWS Inferentia and AWS Trainium Amazon Elastic Compute Cloud (Amazon EC2) instances : The best price-performance infrastructure for training and inference in the cloud

  3. Amazon CodeWhisperer (Developer Productivity): A built-in generative AI coding companion that helps customers build applications faster and more securely  

  4. FMs with Amazon SageMaker JumpStart(DataScience Team, ML builders building FM): Access and fine-tuning of a wide selection of FMs with Amazon SageMaker 


  1. What are the areas where these can be applied with the current offering?

Below are a few example scenarios that strongly correlate with generative AI opportunities.

  1. Text: Summarising or automating content creation 

  2. Images: Generating images, creating avatars  

  3. Audio: Summarising, generating, or converting text in audio  

  4. Video: Generating or editing videos  

  5. Code: Generating code, optimising code 

  6. Chatbots: Automating customer service and more 

  7. ML platforms: Applications and ML platforms  

  8. Search: AI-powered insights or vector search  

  9. Gaming: Generative AI gaming studios or applications 

  10. Data: Synthesizing, designing, collecting, or summarising data 


  1. What are the business use cases which can leverage these AI solutions?

Below, I've listed some of the business use cases that can make use of the existing AI solutions. Please note that this is not an exhaustive list, but it serves to provide context for some of the potential business use cases.

  1. Enhancing customer experience -  

    1. Chatbots and virtual assistance 

    2. Agent assist 

    3. Contact centre analytics 

  2. Employee or Developer productivity

    1. Conversational search 

    2. Text, image and video generation 

    3. Code generation 

    4. Text summarization  

  3. Improve business operations 

    1. Document processing 

    2. Content moderation   

    3. Anomaly detection  

  4. Enhancing creativity 

    1. Image generation for web pages 

    2. Storyboarding 

    3. Image enhancement  

    4. Animation creation  


I hope you now have a good understanding of GenAI and the available services in AWS. As mentioned earlier, we will have follow-on discussions that delve into specific areas of AI. Thank you for reading, and I hope you found this information informative and valuable.