About Me

Wednesday, 15 January 2025

Tensorflow

 

When comparing various deep learning frameworks, it’s evident that TensorFlow stands out as the preferred choice among academics, businesses, and developers.

GitHub activity for different ML frameworks(Source)

What is TensorFlow?

TensorFlow is an open-source software library designed for machine learning and artificial intelligence. While it supports a variety of tasks, it is particularly well-suited for training and inference of deep neural networks. Alongside PyTorch, TensorFlow is one of the two most widely used deep learning libraries.

Developed by Google Brain for internal research and production, the first version was released under the Apache License 2.0 in 2015. Google later introduced TensorFlow 2.0 in September 2019.

TensorFlow supports multiple programming languages, including Python, JavaScript, C++, and Java, making it versatile for various applications across different industries.

EXPLAIN TENSORFLOW

What is a Tensor?

A tensor is an n-dimensional vector or matrix used to represent various types of data. The values within a tensor have the same data type and follow a defined shape, which refers to the matrix’s dimensionality.

Machine learning deals with vast amounts of complex data. Tensors offer an efficient way to handle this diverse data without unnecessary complexity, making them ideal for such tasks. This is why TensorFlow relies entirely on tensors for computations, which is also how the framework gets its name.

But what about the “Flow” part in TensorFlow?

What is a Flow?

As we’ve seen, TensorFlow takes an input in the form of an n-dimensional array/matrix, known as tensors. This input flows through a system of several operations and comes out as output. For example, we receive many numbers as an input, representing the Bits of a photograph, and receive an output like “this is a cat”.

So this flow describes the second part of why TensorFlow is called TensorFlow.

How does TensorFlow work?

Since Python is the go-to language for machine learning, it’s no surprise that TensorFlow offers a user-friendly front-end API in Python for developing applications. However, when it comes to running these applications, TensorFlow relies on C++ for execution, as this provides much higher performance.

Benefits of using TensorFlow

  • Abstraction: The single biggest benefit TensorFlow provides for machine learning development is abstraction. Instead of dealing with the nitty-gritty details of implementing algorithms, or figuring out proper ways to hitch the output of one function to the input of another, the developer can focus on the overall logic of the application. TensorFlow takes care of the details behind the scenes.
  • Google: As it is developed by the giant Google, TensorFlow comes together with many other amazing tools and great documentation. For example, you can learn Machine Learning by doing the abundance of tutorials provided by TensorFlow and optimize your own Machine Learning models by using tools like TensorBoard, which lets you inspect and visualize many details of your model.
  • Use-cases: TensorFlow can train deep neural networks for handwritten digit classification, image recognition, word embeddings, recurrent neural networks, sequence-to-sequence models for machine translation, natural language processing, and much much more.
  • CPU and GPU support: Deep learning applications are very complicated, with the training process requiring a lot of computation. It takes a long time because of the large data size, and it involves several iterative processes, mathematical calculations, matrix multiplications, and so on. These activities take a VERY long time on a normal CPU. This is why TensorFlow supports GPUs, which significantly speeds up to training process.
  • Integration: TensorFlow can be integrated with Java and R.

Get started with TensorFlow

TensorFlow makes it easy to create ML models that can run in any environment. Learn how to use the intuitive APIs through interactive code samples.

The TensorFlow tutorials are written as Jupyter notebooks and run directly in Google Colab — a hosted notebook environment that requires no setup. At the top of each tutorial, you’ll see a Run in Google Colab button. Click the button to open the notebook and run the code yourself.

TensorFlow 2 quickstart for beginners

This short introduction uses Keras to:

  1. Load a prebuilt dataset.
  2. Build a neural network machine learning model that classifies images.
  3. Train this neural network.
  4. Evaluate the accuracy of the model.

This tutorial is a Google Colaboratory notebook. Python programs are run directly in the browser — a great way to learn and use TensorFlow. To follow this tutorial, run the notebook in Google Colab by clicking the button at the top of this page.

  1. In Colab, connect to a Python runtime: At the top-right of the menu bar, select CONNECT.
  2. To run all the code in the notebook, select Runtime > Run all. To run the code cells one at a time, hover over each cell and select the Run cell icon.

Set up TensorFlow

Import TensorFlow into your program to get started:

import tensorflow as tf
print("TensorFlow version:", tf.__version__)
2024-08-16 07:45:15.387747: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-08-16 07:45:15.408731: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-08-16 07:45:15.415209: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
TensorFlow version: 2.17.0

If you are following along in your own development environment, rather than Colab, see the install guide for setting up TensorFlow for development.

Note: Make sure you have upgraded to the latest pip to install the TensorFlow 2 package if you are using your own development environment. See the install guide for details.

Load a dataset

Load and prepare the MNIST dataset. The pixel values of the images range from 0 through 255. Scale these values to a range of 0 to 1 by dividing the values by 255.0. This also converts the sample data from integers to floating-point numbers:

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

Build a machine learning model

Build a tf.keras.Sequential model:

model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/src/layers/reshaping/flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(**kwargs)
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1723794318.490455 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.494342 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.497584 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.501312 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.512702 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.516197 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.519187 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.522647 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.526047 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.529503 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.532428 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.535893 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.771712 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.773840 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.775826 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.777872 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.779874 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.781821 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.783693 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.785644 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.787540 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.789499 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.791369 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.793317 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.831749 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.833814 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.835738 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.837736 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.839701 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.841655 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.843526 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.845500 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.847443 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.849923 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.852250 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.854736 241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355predictions = model(x_train[:1]).numpy()
predictions

Sequential is useful for stacking layers where each layer has one input tensor and one output tensor. Layers are functions with a known mathematical structure that can be reused and have trainable variables. Most TensorFlow models are composed of layers. This model uses the Flatten, Dense, and Dropout layers.

For each example, the model returns a vector of logits or log-odds scores, one for each class.

predictions = model(x_train[:1]).numpy()
predictions
array([[ 0.68130803, -0.03935227, -0.53304887,  0.22200397, -0.3079031 ,
-0.6267688 , 0.43393654, 0.5691322 , 0.31098977, 0.32141146]]
,
dtype=float32)

The tf.nn.softmax function converts these logits to probabilities for each class:

tf.nn.softmax(predictions).numpy()
array([[0.16339162, 0.07947874, 0.04851112, 0.10321827, 0.06076043,
0.0441712 , 0.12758444, 0.14605366, 0.11282429, 0.11400625]]
,
dtype=float32)

Note: It is possible to bake the tf.nn.softmax function into the activation function for the last layer of the network. While this can make the model output more directly interpretable, this approach is discouraged as it's impossible to provide an exact and numerically stable loss calculation for all models when using a softmax output.

Define a loss function for training using losses.SparseCategoricalCrossentropy:

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)Conclusion

The loss function takes a vector of ground truth values and a vector of logits and returns a scalar loss for each example. This loss is equal to the negative log probability of the true class: The loss is zero if the model is sure of the correct class.

This untrained model gives probabilities close to random (1/10 for each class), so the initial loss should be close to -tf.math.log(1/10) ~= 2.3

loss_fn(y_train[:1], predictions).numpy()
3.1196823

Before you start training, configure and compile the model using Keras Model.compile. Set the optimizer class to adam, set the loss to the loss_fn function you defined earlier, and specify a metric to be evaluated for the model by setting the metrics parameter to accuracy.

model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])

Train and evaluate your model

Use the Model.fit method to adjust your model parameters and minimize the loss:

model.fit(x_train, y_train, epochs=5)
Epoch 1/5
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1723794322.305243 241442 service.cc:146] XLA service 0x7effb8008d30 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1723794322.305276 241442 service.cc:154] StreamExecutor device (0): Tesla T4, Compute Capability 7.5
I0000 00:00:1723794322.305281 241442 service.cc:154] StreamExecutor device (1): Tesla T4, Compute Capability 7.5
I0000 00:00:1723794322.305284 241442 service.cc:154] StreamExecutor device (2): Tesla T4, Compute Capability 7.5
I0000 00:00:1723794322.305287 241442 service.cc:154] StreamExecutor device (3): Tesla T4, Compute Capability 7.5
112/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.6089 - loss: 1.3300
I0000 00:00:1723794323.392324 241442 device_compiler.h:188] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process.
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 4s 1ms/step - accuracy: 0.8622 - loss: 0.4811
Epoch 2/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9547 - loss: 0.1539
Epoch 3/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9676 - loss: 0.1107
Epoch 4/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9738 - loss: 0.0843
Epoch 5/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9764 - loss: 0.0769
<keras.src.callbacks.history.History at 0x7f0184ec7490>

The Model.evaluate method checks the model's performance, usually on a validation set or test set.

model.evaluate(x_test,  y_test, verbose=2)
313/313 - 1s - 3ms/step - accuracy: 0.9782 - loss: 0.0729
[0.07293704897165298, 0.9782000184059143]

The image classifier is now trained to ~98% accuracy on this dataset. To learn more, read the TensorFlow tutorials.

If you want your model to return a probability, you can wrap the trained model, and attach the softmax to it:

probability_model = tf.keras.Sequential([
model,
tf.keras.layers.Softmax()
])
probability_model(x_test[:5])
<tf.Tensor: shape=(5, 10), dtype=float32, numpy=
array([[1.5427084e-07, 7.5027339e-11, 3.1343968e-06, 4.6326011e-05,
8.9990645e-13, 1.5266414e-07, 2.0456495e-13, 9.9994934e-01,
2.1858141e-07, 7.8530559e-07],
[1.7771253e-08, 8.4947787e-05, 9.9989736e-01, 1.8331458e-06,
8.3026415e-15, 3.4793761e-08, 6.2480517e-08, 7.9319728e-12,
1.5733674e-05, 3.5440111e-15],
[3.3602277e-07, 9.9804592e-01, 5.7737787e-05, 5.8099768e-06,
6.3599517e-05, 2.3768812e-06, 2.3459031e-06, 1.6781164e-03,
1.4260423e-04, 1.0617223e-06],
[9.9997318e-01, 8.7561805e-11, 9.8983969e-07, 9.0878149e-10,
1.0803159e-07, 3.3033965e-07, 2.3622524e-05, 6.7567669e-07,
4.7765565e-09, 1.1131582e-06],
[1.1404303e-05, 2.4895797e-09, 6.0792736e-06, 4.9114313e-08,
9.9449867e-01, 5.9158310e-06, 2.9842497e-05, 4.8574508e-05,
8.5193824e-06, 5.3910208e-03]]
, dtype=float32)>

conclusion

Congratulations! You have trained a machine learning model using a prebuilt dataset using the Keras API.

For more examples of using Keras, check out the tutorials. To learn more about building models with Keras, read the guides. If you want learn more about loading and preparing data, see the tutorials on image data loading or CSV data loading.


more info;


N.G.Sithija Theekshana
BSc Computer Science and Information Technology
BSc Applied Physics and Electronics


Monday, 9 December 2024

How Generative AI Works


 Generative AI is one of the most exciting and transformative technologies today. From creating realistic images to generating human-like text and composing music, this field of artificial intelligence has made enormous strides in recent years. As AI evolves, generative models have become essential tools in various industries, offering new ways to create, innovate, and solve problems.

In this blog post, we will explore how generative AI works, examining the key components, technologies, and models that enable it to generate content like text, images, and more. We’ll also dive into real-world applications, ethical considerations, and the future potential of this technology.

What is Generative AI?

At its core, generative AI refers to a category of artificial intelligence models that can generate new content. Unlike traditional AI models, which are designed to classify, predict, or recognize data, generative models can create something entirely new based on the patterns they learn from the data they are trained on.

For example, a generative AI model trained on a vast amount of text data could produce a coherent and contextually relevant paragraph when prompted. Similarly, a model trained on images could generate new, never-before-seen visuals based on text descriptions. This ability to create new, original outputs is what sets generative AI apart from other forms of AI.

In today’s technology landscape, generative AI is used across multiple domains. It powers chatbots, content generators, image creation tools, music composition software, and more. It’s revolutionizing industries like entertainment, design, and marketing, enabling faster content creation and more personalized user experiences.

The Key Technologies Behind Generative AI

Generative AI models rely on several foundational technologies to create new content. These include neural networks, machine learning, and deep learning. Let’s break them down:

1. Neural Networks

Neural networks are computational models inspired by the human brain. They consist of layers of interconnected nodes (neurons), each performing mathematical operations on input data. Neural networks are designed to recognize patterns in data by adjusting the connections (weights) between neurons during training.

For example, when generating text, a neural network processes the input (like a sentence) and learns how words and phrases typically follow each other. Over time, it gets better at predicting what comes next based on patterns in the training data.

2. Machine Learning

Machine learning is a subset of AI that allows models to learn from data without being explicitly programmed. In the context of generative AI, machine learning algorithms are used to train models on large datasets so they can understand and replicate patterns. The more data a model is trained on, the better it becomes at generating accurate and realistic content.

In generative AI, machine learning techniques such as supervised learning and reinforcement learning are often used to improve the quality of generated outputs.

3. Deep Learning

Deep learning is a specialized branch of machine learning that focuses on training neural networks with many layers, hence the term “deep” learning. Deep learning models are particularly effective in tasks that involve large amounts of complex data, such as image recognition, natural language processing (NLP), and generative content creation.

Deep learning is what allows generative AI models to create sophisticated content like high-quality images and convincing text. Models such as GPT-3, DALL-E, and StyleGAN leverage deep learning techniques to generate content that closely resembles human creativity.

How Generative AI Models Create Content

Generative AI works by learning from a large dataset, understanding the underlying patterns, and then using that knowledge to generate new data that follows those patterns. This process typically involves two phases: training and generation.

1. Training Phase

During the training phase, a generative AI model is exposed to a large dataset. For example:

  • A language model might be trained on a massive collection of text, including books, articles, and websites.

  • A model designed to generate images might be trained on thousands of labeled images, learning how objects and scenes are typically depicted.

The model learns to recognize patterns in this data—such as the structure of sentences in text or the distribution of colors and shapes in images. The goal is for the model to internalize these patterns so it can apply them later to create new content.

2. Generation Phase

Once the model is trained, it enters the generation phase, where it creates new content. For example, if you provide a text prompt to a language model, it will generate a coherent piece of text by predicting one word at a time based on the patterns it learned during training. In the case of an image generation model like DALL-E, the model uses the text prompt to create a new image from scratch.

In both cases, the AI doesn’t simply copy and paste existing content; instead, it synthesizes new outputs based on the learned patterns, making each piece of content unique.

Types of Generative AI Models





There are several types of generative AI models, each with its own strengths and applications. The two most widely used are Generative Adversarial Networks (GANs) and autoregressive models.

1. Generative Adversarial Networks (GANs)

GANs are a class of generative models that consist of two neural networks: the generator and the discriminator. These two networks are trained simultaneously and play a game against each other.

  • The generator creates new data (e.g., images, text).

  • The discriminator evaluates whether the data is real (from the training set) or fake (generated by the generator).

The goal is for the generator to create content that is so realistic that the discriminator can’t tell the difference. Over time, both networks improve, resulting in high-quality outputs. GANs are widely used for image generation and creative content creation.

Example: StyleGAN StyleGAN is a popular GAN model developed by NVIDIA that can generate highly realistic images of people, animals, and objects. The model is able to manipulate fine details in images, such as facial features or lighting, enabling creators to generate lifelike images that didn’t exist before.

2. Autoregressive Models

Autoregressive models generate content one step at a time by predicting the next part of the output based on the preceding context. These models are often used for text generation and are known for their ability to produce coherent and contextually relevant content.

Example: GPT-3 GPT-3 (Generative Pretrained Transformer 3) is an autoregressive model that generates text one word at a time, using the context provided in the input prompt. It has been trained on a diverse range of text, allowing it to generate everything from news articles to poetry, all while maintaining a natural flow.

Ethical Considerations and Potential Impacts

As with any powerful technology, generative AI comes with ethical considerations and potential societal impacts. Some key concerns include:

  • Misinformation: The ability of AI to generate realistic text, images, and videos raises concerns about deepfakes and misinformation. For example, AI-generated news articles or videos could spread false information.

  • Intellectual Property: Who owns the content generated by AI? This question is particularly relevant in creative industries, where AI is used to produce art, music, and literature.

  • Bias: AI models can inherit biases present in the training data. For example, an AI model trained on biased data may generate biased content, which could reinforce harmful stereotypes or exclusionary practices.

Addressing these ethical concerns will be crucial as generative AI continues to evolve and become more widespread.

The Future of Generative AI

Generative AI is still in its early stages, but its potential is enormous. In the future, we can expect advancements in several areas:

  • More sophisticated models: Future generative models will likely be even more powerful, capable of generating even more realistic and diverse content.

  • Better fine-tuning: As models become more customizable, we’ll see more personalized AI tools that can create content tailored to individual users or industries.

  • Ethical frameworks: As generative AI becomes more integrated into society, we’ll likely see new regulations and ethical frameworks designed to address concerns about misuse and bias.

Ultimately, the future of generative AI holds immense promise, transforming the way we create and interact with content.

Conclusion

Generative AI is a powerful technology that’s changing the way we create and interact with digital content. By leveraging neural networks, machine learning, and deep learning, these models are capable of producing text, images, music, and more. GANs and autoregressive models are at the forefront of generative AI, each with unique strengths and applications.

While generative AI offers numerous benefits, including enhanced creativity, productivity, and automation, it also presents ethical challenges that must be addressed as the technology continues to advance. As we look to the future, the potential for generative AI to revolutionize industries and create new opportunities is boundless.

Monday, 8 July 2024

Understanding the K-Nearest Neighbors Algorithm (A Beginner's Guide part 7)

Understanding the K-Nearest Neighbors Algorithm (A Beginner's Guide)

Machine learning algorithms can seem complex, but breaking them down into simpler terms can make them more approachable. One such algorithm is the K-Nearest Neighbors (K-NN) algorithm, which is popular for its simplicity and effectiveness. In this blog, we'll explore what K-NN is, how it works, and some practical applications.

What is K-Nearest Neighbors?

K-Nearest Neighbors (K-NN) is a supervised learning algorithm used for classification and regression tasks. In simple terms, K-NN classifies data points based on the 'votes' of their nearest neighbors. It doesn't make any assumptions about the underlying data distribution, making it a non-parametric algorithm.

How Does K-NN Work?


The K-Nearest Neighbors algorithm operates based on the idea that data points that are close to each other tend to have similar properties or belong to the same class. Here’s a detailed step-by-step process of how K-NN works:

Step-by-Step Process

  1. Choose the Number of Neighbors (K)

    • The first step is to choose the number of neighbors, K. This is the number of closest data points the algorithm will consider when making a prediction. The choice of K can significantly impact the algorithm's performance.
  2. Calculate the Distance

    • For a given data point (test point), calculate the distance between this point and all other points in the training dataset. The most common distance metric used is the Euclidean distance, but other metrics like Manhattan distance or Minkowski distance can also be used.

    The Euclidean distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) in a 2D space is calculated as:

    Euclidean Distance=(x2x1)2+(y2y1)2\text{Euclidean Distance} = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}

    For higher-dimensional spaces, the formula is:

    Euclidean Distance=i=1n(xiyi)2\text{Euclidean Distance} = \sqrt{\sum_{i=1}^n (x_i - y_i)^2}

    where nn is the number of features.

  3. Identify the K-Nearest Neighbors

    • Once the distances are calculated, sort all the distances and identify the KK data points that are the closest to the test point. These are the K-nearest neighbors.
  4. Vote for the Class (for Classification)

    • For classification tasks, each of the K-nearest neighbors “votes” for their class, and the class with the most votes is assigned to the test point. This is known as majority voting.

    For example, if K=5 and 3 of the nearest neighbors belong to class A and 2 belong to class B, the test point is classified as class A.

  5. Calculate the Average (for Regression)

    • For regression tasks, the algorithm takes the average of the values of the K-nearest neighbors and assigns this average as the predicted value for the test point.

    For example, if K=5  and the values of the nearest neighbors are 10, 12, 15, 11, and 13, the predicted value for the test point would be:

    Predicted Value=(10+12+15+11+13)/5 =12.2

Detailed Example

Let's walk through a detailed example to illustrate these steps. Suppose we have a dataset of fruits with two features: weight and color (represented as a numerical value), and we want to classify a new fruit.

Training Data


Step-by-Step Process
  1. Choose k

    • Let's choose K=3
  2. Calculate the Distance


  • Identify the K-Nearest Neighbors

    • Sort the distances: 5 (Apple 1), 5.83 (Orange 3), 10 (Apple 3).
    • The 3 nearest neighbors are Apple 1, Orange 3, and Apple 3.
  • Vote for the Class

    • Among the 3 nearest neighbors, 2 are apples and 1 is an orange.
    • The new fruit is classified as an apple based on majority voting.

    By following these steps, K-NN provides a straightforward and intuitive way to classify new data points based on their similarity to existing data points.

    Example


    Let's say we want to classify whether a fruit is an apple or an orange based on its features like weight and color. We have a dataset with these features and their corresponding labels (apple or orange). To classify a new fruit, K-NN will:

    1. Calculate the distance between the new fruit and all other fruits in the dataset.
    2. Select the K-nearest fruits (e.g., K=3).
    3. Determine the majority class among these K-nearest fruits.
    4. Assign the new fruit to this majority class.


    Choosing the Right K

    Choosing the right value for K is crucial. A small K can be sensitive to noise in the data, while a large K can smooth out the predictions but may lose important details. A common approach is to use cross-validation to determine the optimal K value for your specific dataset.


    Advantages and Disadvantages

    Advantages

    • Simplicity: K-NN is easy to understand and implement.
    • No training phase: It doesn't require a training phase, making it fast for small datasets.
    • Versatility: Can be used for both classification and regression tasks.

    Disadvantages

    • Computationally expensive: For large datasets, the distance calculation can be time-consuming.
    • Storage requirements: Requires storing the entire dataset.
    • Sensitive to irrelevant features: Performance can degrade if irrelevant features are included.

    Practical Applications

    K-NN is used in various applications such as:

    • Recommendation systems: Suggesting products or content based on similar users' preferences.
    • Image recognition: Classifying images based on similarity to known images.
    • Medical diagnosis: Predicting diseases based on patient symptoms and historical data.

    Conclusion

    The K-Nearest Neighbors algorithm is a powerful yet simple tool in the machine learning toolbox. By understanding how it works and its applications, you can effectively use K-NN for various tasks. Remember to choose the right value of K and preprocess your data appropriately to achieve the best results.


    Sithija Theekshana 

    (bsc in Computer Science and Information Technology)

    (bsc in Applied Physics and Electronics)


    linkedin ;- www.linkedin.com/in/sithija-theekshana-008563229

    Tensorflow

      When comparing various deep learning frameworks, it’s evident that TensorFlow stands out as the preferred choice among academics, business...