{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "60b5bfb3-dcec-4ae7-a3d7-c92371f611a6",
   "metadata": {},
   "source": [
    "\n",
    "Getting started with TesnorFlow.  </br>\n",
    "This code provides a basic example of training a binary classification model with TensorFlow in JupyterLab. </br>\n",
    "You can adapt this structure for more complex tasks by modifying the model architecture, data preparation steps, and training parameters.\n",
    "\n",
    "Library Installation: The code starts by installing TensorFlow, a popular library for machine learning tasks.\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aad3297d-067a-4cb2-a620-5c6aa9c24daf",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install tensorflow"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fed8b4f3-bc16-4865-adb9-826f72e19aca",
   "metadata": {},
   "source": [
    "Version Check: It then verifies the installed TensorFlow version using"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5052ef81-6f02-4f14-9421-89637372e35a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "print(tf.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05b679a0-c469-4797-ac18-92f08be69dcb",
   "metadata": {},
   "source": [
    "GPU Availability Check (Optional): The !nvidia-smi command (assuming you have an Nvidia GPU) checks if a GPU is available and its details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a91bc9f6-ab47-4838-a9db-f7f0614cc914",
   "metadata": {},
   "outputs": [],
   "source": [
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8313184c-1ebf-4d40-8a13-7db44668a2bd",
   "metadata": {},
   "source": [
    "Data Preparation:<br>\n",
    "It generates 1000 samples with 10 features each using np.random.rand for the training data.</br>\n",
    "Random binary labels (0 or 1) are assigned to each sample using np.random.randint. A similar approach is used to generate test data.\n",
    "\n",
    "Model Definition: </br>\n",
    "A sequential model is defined using tf.keras.models.Sequential. The model consists of three layers:</br>\n",
    "The first layer has 64 units with a ReLU activation function and takes the input data with a shape of (10,) (10 features).</br>\n",
    "The second layer also has 64 units with a ReLU activation.</br>\n",
    "The output layer has 1 unit with a sigmoid activation suitable for binary classification tasks.</br>\n",
    "\n",
    "Model Compilation: </br>\n",
    "The model is compiled using model.compile. Here, the optimizer is set to 'adam' (a popular optimization algorithm), the loss function is set to 'binary_crossentropy' (suitable for binary classification), and the 'accuracy' metric is chosen to monitor model performance during training.</br>\n",
    "\n",
    "Model Training: <br>\n",
    "The model is trained on the prepared data using model.fit. It trains for 10 epochs (iterations over the data) with a batch size of 32 (number of samples processed together at each step).</br>\n",
    "Model Evaluation: After training, the model's performance on unseen data is evaluated using model.evaluate. Test accuracy is then printed using the test_acc value.</br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "650ce370-198c-474a-a66e-be56e54430cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Generate random training data\n",
    "train_data = np.random.rand(1000, 10)  # 1000 samples, 10 features\n",
    "train_labels = np.random.randint(0, 2, size=(1000,))  # Binary labels\n",
    "\n",
    "# Define the model\n",
    "model = tf.keras.models.Sequential([\n",
    "    tf.keras.layers.Dense(64, activation='relu', input_shape=(10,)),\n",
    "    tf.keras.layers.Dense(64, activation='relu'),\n",
    "    tf.keras.layers.Dense(1, activation='sigmoid')\n",
    "])\n",
    "\n",
    "# Compile the model\n",
    "model.compile(optimizer='adam',\n",
    "              loss='binary_crossentropy',\n",
    "              metrics=['accuracy'])\n",
    "\n",
    "# Train the model\n",
    "model.fit(train_data, train_labels, epochs=10, batch_size=32)\n",
    "\n",
    "# Generate random test data\n",
    "test_data = np.random.rand(100, 10)  # 100 samples, 10 features\n",
    "test_labels = np.random.randint(0, 2, size=(100,))  # Binary labels\n",
    "\n",
    "# Evaluate the model\n",
    "test_loss, test_acc = model.evaluate(test_data, test_labels)\n",
    "print('Test accuracy:', test_acc)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python(GPU)",
   "language": "python",
   "name": "syntasa_kernel_python_gpu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
