analitics

Pages

Showing posts with label Colab. Show all posts
Showing posts with label Colab. Show all posts

Friday, April 19, 2024

Python 3.10.12 : Colab quantum circuits with qiskit - part 046.

I've added another introductory example to my GitHub repository with Google Colab notebooks on how to use quantum circuits with the Python package called qiskit and the IBM Quantum Platform.
I used the IBM Quantum Platform and it provides an A.P.I symbol so that it can be used with the source code.
You can find this notebook at this catafest_061.ipynb repo file.

Friday, February 23, 2024

Saturday, February 17, 2024

Python 3.10.12 : Few example for CUDA and NVCC - part 044.

NVCC use CUDA C/C++ source code and allows developers to write high-performance GPU-accelerated applications by leveraging the power of NVIDIA GPUs for parallel processing tasks.
Today I test some simple examples with this tool on Google Colab using the nvcc4jupyter python package.
You need to install it with the pip and know how to use the CUDA C/C++ source code, or use the basic example from documentation.
pip install nvcc4jupyter
I change some source code because is need to install this library and I don't have time to learn and test.
But this will allow me to test better, because on my desktop I don't have a good hardware.
This is the source I change and I cut the source code linked on error_handling.h.
This is the changed source code , you can see more on my GitHub repo for Google Colab ! !
#include 
//#include "error_handling.h"

const int DSIZE = 4096;
const int block_size = 256;

// vector add kernel: C = A + B
__global__ void vadd(const float *A, const float *B, float *C, int ds){
    int idx = threadIdx.x + blockIdx.x * blockDim.x;
    if (idx < ds) {
        C[idx] = A[idx] + B[idx];
    }
}

int main(){
    float *h_A, *h_B, *h_C, *d_A, *d_B, *d_C;

    // allocate space for vectors in host memory
    h_A = new float[DSIZE];
    h_B = new float[DSIZE];
    h_C = new float[DSIZE];

    // initialize vectors in host memory to random values (except for the
    // result vector whose values do not matter as they will be overwritten)
    for (int i = 0; i < DSIZE; i++) {
        h_A[i] = rand()/(float)RAND_MAX;
        h_B[i] = rand()/(float)RAND_MAX;
    }

    // allocate space for vectors in device memory
    cudaMalloc(&d_A, DSIZE*sizeof(float));
    cudaMalloc(&d_B, DSIZE*sizeof(float));
    cudaMalloc(&d_C, DSIZE*sizeof(float));
    //cudaCheckErrors("cudaMalloc failure"); // error checking

    // copy vectors A and B from host to device:
    cudaMemcpy(d_A, h_A, DSIZE*sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(d_B, h_B, DSIZE*sizeof(float), cudaMemcpyHostToDevice);
    //cudaCheckErrors("cudaMemcpy H2D failure");

    // launch the vector adding kernel
    vadd<<<(DSIZE+block_size-1)/block_size, block_size>>>(d_A, d_B, d_C, DSIZE);
    //cudaCheckErrors("kernel launch failure");

    // wait for the kernel to finish execution
    cudaDeviceSynchronize();
    //cudaCheckErrors("kernel execution failure");

    cudaMemcpy(h_C, d_C, DSIZE*sizeof(float), cudaMemcpyDeviceToHost);
    //cudaCheckErrors("cudaMemcpy D2H failure");

    printf("A[0] = %f\n", h_A[0]);
    printf("B[0] = %f\n", h_B[0]);
    printf("C[0] = %f\n", h_C[0]);
    return 0;
}
This is result ...
A[0] = 0.840188
B[0] = 0.394383
C[0] = 0.000000

Saturday, January 6, 2024

Python 3.10.12 : LaTeX on colab notebook - part 043.

Today I tested with a simple LaTeX examples in the Colab notebook.
If you open my notebook colab then you will see some issues and how can be fixed.
You can find this on my notebook - catafest_056.ipynb on colab repo.

Sunday, December 10, 2023

Python 3.10.12 : Simple examples with some Python modules - part 042.

I added some simple examples in my repo on GitHub where I have notebooks from Google Colab.
Python usage is limited to this type of interface with Python version stability rules.
You can see the Python version that Colab is using with this command in the code area:
!python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
These are the Python packages I used: cartopy, matplotlib, numpy, geemap, earthengine-api, rasterio.
Of these examples, some require some Google configuration, others require knowledge of topography ... or are very simple to use with a dedicated module as we have visualized more special types of image files.
See there an example with the cartopy python package :
import matplotlib.pyplot as plt

import cartopy.crs as ccrs
from cartopy.io import shapereader
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

import cartopy.io.img_tiles as cimgt

extent = [15, 25, 55, 35]

request = cimgt.OSM()

fig = plt.figure(figsize=(9, 13))
ax = plt.axes(projection=request.crs)
gl = ax.gridlines(draw_labels=True, alpha=0.2)
gl.top_labels = gl.right_labels = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER

ax.set_extent(extent)

ax.add_image(request, 11)

plt.show()

Wednesday, December 6, 2023

Python 3.10.12 : Simple example with SymPy - part 041.

SymPy is a Python library for symbolic mathematics. If you are new to SymPy, start with the introductory tutorial.
You can find a simple example with a partial differentiation on my GitHub colab repo.

Monday, November 27, 2023

Python 3.10.12 : My colab get images from imdb.com by the name - part 040.

Transformers provides thousands of pretrained models to perform tasks on different modalities such as text, vision, and audio.
You find more on my Colab Github repo.

Python 3.10.12 : My colab get images from imdb.com by the name - part 039.

You can use gspread and google-auth to get data from a spreadsheet from google drive and use it.
The colab notebook can be found on my colab repo on Github.
from google.colab import auth
auth.authenticate_user()

import gspread
from google.auth import default
creds, _ = default()

gc = gspread.authorize(creds)

# Open the spreadsheet by name
spreadsheet = gc.open('fiecare_saptamana')

# Choose a worksheet from the spreadsheet
worksheet = spreadsheet.get_worksheet(0)  # 0 represents the index of the first worksheet

# Get and print the data
data = worksheet.get_all_values()

#uncoment this line to print data sau add more source code to parse data
#print(data)

#the result will be 
#[['noreply@projects.blender.org', '437', 'notifications@github.com',  ...

Monday, October 23, 2023

Python 3.10.12 : My colab get images from imdb.com by the name - part 038.

This colab notebook named catafest_050.ipynb will let you to get images from imdb.com by the name of the actor/actress.
You can find this notebook on my GitHub project.

Sunday, August 13, 2023

Python 3.10.12 : My colab test with Gated recurrent unit mechanism - part 037.

This is a simple example for Gated recurrent unit mechanism known as GRUs.
You can find this in my GitHub colab project.
import numpy as np
import tensorflow as tf
import keras
from keras import layers
units = 64
tf.keras.layers.GRU(
    units,
    activation="tanh",
    recurrent_activation="sigmoid",
    use_bias=True,
    kernel_initializer="glorot_uniform",
    recurrent_initializer="orthogonal",
    bias_initializer="zeros",
    kernel_regularizer=None,
    recurrent_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    recurrent_constraint=None,
    bias_constraint=None,
    dropout=0.0,
    recurrent_dropout=0.0,
    return_sequences=False,
    return_state=False,
    go_backwards=False,
    stateful=False,
    unroll=False,
    time_major=False,
    reset_after=True,
)
inputs = tf.random.normal([32, 10, 8])
gru = tf.keras.layers.GRU(4)
output = gru(inputs)
print(output.shape)

gru = tf.keras.layers.GRU(4, return_sequences=True, return_state=True)
whole_sequence_output, final_state = gru(inputs)
print(whole_sequence_output.shape)
print(final_state.shape)

Saturday, August 12, 2023

Python 3.10.12 : My colab tutorials and news from colab - part 036.

Today I recapitulated a bit the artificial intelligence part and a simple example for google drive.
I created two notebooks in collaboration and added them to my github repo.
The most interesting is the one with textgenrnn.
textgenrnn is an modern neural network architecture which utilizes new techniques as attention-weighting and skip-embedding to accelerate training
The last notebook colab is catafest_045.

News : Colab behavior through runtime .

I would like Google to emphasize more on the development side some elements that work like robots by interfacing with the development side.
Today I worked a little on artificial intelligence and I realized that it doesn't create textgenrnn_weights.hdf5 file for training created with the Python textgenrnn mode.
A solution is to reset the runtime with Ctrl+M and resume running.
They specify RESTART RUNTIME when using Python modules, see:
WARNING: The following packages were previously imported in this runtime:
   [numpy]
You must restart the runtime in order to use newly installed versions.
In this case, with the creation of textgenrnn_weights.hdf5 file, it is more difficult to understand and cannot be seen easily.

Thursday, August 10, 2023

Python 3.10.12 : My colab tutorials and news from colab - part 035.

In this notebook I will show you how to use python to run a program written in the programming language for CUDA.
This allows you to use NVIDIA CUDA Compiler Driver NVCC, see this official webpage.
NVCC Plugin for Jupyter Notebook by https://github.com/andreinechaev/nvcc4jupyter.
The example I tested is simple:
# This is formatted as CUDA code
__global__ void cuda_hello(){
    printf("Hello World from GPU!\n");
}

int main() {
    cuda_hello<<<1,1>>>();
    return 0;
}

Tuesday, July 11, 2023

Python 3.8.10 : My colab tutorials and news from colab - part 034.

I add a new colab notebook with a simple source code to list all running VM processes from the colab notebook
You can see more examples on my GitHub colab google repo.
This is the source code:
%%sh
echo "List all running VM processes."
ps -ef
echo "Done"

Friday, May 5, 2023

Python 3.8.10 : My colab tutorials and news from colab - part 033.

Colab comes with new changes:
Starting today paid users can select their preferred NVIDIA GPU. Visit Runtime > Change runtime type and choose between T4, V100, and A100. We'll do our best to assign your choice based on GPU availability.
Today I tested a new python package called News API with colab to search for news.
News API is a simple, easy-to-use REST API that returns JSON search results for current and historic news articles published by over 80,000 worldwide sources.
You can find my sample code in my collaboration area of the GitHub repository.

Saturday, April 29, 2023

Python 3.8.10 : My colab tutorials - part 032.

I haven't written for the python community in a long time, here is another example that I created using a tool from google called colab.
catafest_038.ipynb - simple example with StableDiffusionPipeline and DiffusionPipeline to generate images based a text ...
This and the other examples can be found in my repository named colab_google on my GitHub account.

Saturday, February 25, 2023

Python 3.8.10 : My colab tutorials - part 031.

I update my source code for the collaboratory google tool. This colab notebook comes with source code for an update of IPython, PyTikTokAPI python module using the cookies browser, and a simple OpenCV test to create an image and show with google.colab.patches.
The cookies for the TikTok package module can be found in any browser by pressing the F12 key.
I don't find a good python package that parse the tiktok content.
The application for development with TikTok looks like Instagram and Facebook ... you can test it at https://developers.tiktok.com/app/.

Sunday, February 19, 2023

Tuesday, February 14, 2023

News : Colab changes ...

Python 2 is no longer supported in Colab and cells runs iron python.
All information you need on migrating your code from Python 2 to Python 3 can be found on Porting Python 2 Code to Python 3.
You can see these changes on the GitHub project.

Tuesday, January 24, 2023

Python 3.8.10 : My colab tutorials - part 029.

Colab from google is a great tool when you want to write equations. I show a simple example with LaTeX formula with this colab notebook named catafest_034.