This code provides a basic example of training a binary classification model with TensorFlow in JupyterLab. You can adapt this structure for more complex tasks by modifying the model architecture, data preparation steps, and training parameters.

### Library Installation

The code starts by installing TensorFlow, a popular library for machine learning tasks.

!pip install tensorflow

### Version Check

It then verifies the installed TensorFlow version using

import tensorflow as tf

print(tf.__version__)

### GPU Availability Check (Optional)

The !nvidia-smi command (assuming you have an Nvidia GPU) checks if a GPU is available and its details.

!nvidia-smi

### Data Preparation

It generates 1000 samples with 10 features each using *np.random.rand* for the training data. Random binary labels (0 or 1) are assigned to each sample using *np.random.randint.* A similar approach is used to generate test data.

### Model Definition

A sequential model is defined using *tf.keras.models.Sequential*. The model consists of three layers:

- The first layer has 64 units with a ReLU activation function and takes the input data with a shape of (10,) (10 features).
- The second layer also has 64 units with a ReLU activation.
- The output layer has 1 unit with a sigmoid activation suitable for binary classification tasks.

### Model Compilation

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.

### Model Training

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 (the number of samples processed together at each step).

### Model Evaluation

After training, the model's performance on unseen data is evaluated using *model.evaluate*. The test accuracy is then printed using the test_acc value.

import numpy as np

# Generate random training data

train_data = np.random.rand(1000, 10) # 1000 samples, 10 features

train_labels = np.random.randint(0, 2, size=(1000,)) # Binary labels

# Define the model

model = tf.keras.models.Sequential([

tf.keras.layers.Dense(64, activation='relu', input_shape=(10,)),

tf.keras.layers.Dense(64, activation='relu'),

tf.keras.layers.Dense(1, activation='sigmoid')

])

# Compile the model

model.compile(optimizer='adam',

loss='binary_crossentropy',

metrics=['accuracy'])

# Train the model

model.fit(train_data, train_labels, epochs=10, batch_size=32)

# Generate random test data

test_data = np.random.rand(100, 10) # 100 samples, 10 features

test_labels = np.random.randint(0, 2, size=(100,)) # Binary labels

# Evaluate the model

test_loss, test_acc = model.evaluate(test_data, test_labels)

print('Test accuracy:', test_acc)

The best way to understand and learn how to perform this function is through hands-on experience. Follow the steps below to create the sample notebook in your Syntasa environment:

- Download the sample notebook .ipynb file from this article.
- Create a new notebook in your Syntasa environment using the import notebook option.