0% found this document useful (0 votes)
44 views5 pages

CTRL

The document provides code snippets for loading and preprocessing data, building sequential neural network models in Keras, and evaluating model performance on classification tasks. Key steps include: 1. Importing libraries and loading/preprocessing data for modeling. 2. Splitting data into training and test sets using train_test_split. 3. Defining a sequential model with Flatten, Dense, and activation layers to process input data. 4. Compiling the model by specifying loss, optimizer, and evaluation metrics. 5. Additional code shows techniques like merging multiple sequential models and controlling randomization.

Uploaded by

jaffar bikat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

CTRL

The document provides code snippets for loading and preprocessing data, building sequential neural network models in Keras, and evaluating model performance on classification tasks. Key steps include: 1. Importing libraries and loading/preprocessing data for modeling. 2. Splitting data into training and test sets using train_test_split. 3. Defining a sequential model with Flatten, Dense, and activation layers to process input data. 4. Compiling the model by specifying loss, optimizer, and evaluation metrics. 5. Additional code shows techniques like merging multiple sequential models and controlling randomization.

Uploaded by

jaffar bikat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ctrl + M for making heading

# import the neccassry libraries and mount the drive first

import pandas as pd

import numpy as np

import tensorflow as tf

from tensorflow import keras

import matplotlib.pyplot as plt

from keras.models import Sequential

from tensorflow.keras import layers

from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_squared_error

from sklearn.preprocessing import StandardScaler

from sklearn.model_selection import train_test_split

from google.colab import drive

from sklearn import preprocessing

drive.mount('/content/drive')

Shift + Enter

for cell execution

data.head()
Viewing the first 5 lines/ rows

data.info()
provides a concise summary of a DataFrame. It displays information about the DataFrame, such as the
number of rows and columns, the data types of each column, the number of non-null values, and the
memory usage
data.target.value_counts()
For counting number of unique values in the target column.

The resulting object will be in descending order so that the first element is the most frequently-
occurring element. Excludes NA values by default.

i.e. 1 1000

0 500

X.info()

The info() method prints information about the DataFrame.

The information contains the number of columns, column labels, column data types, memory
usage, range index, and the number of cells in each column (non-null values).

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,


random_state=42)

Here,

X_train: is Input for Training Set (Total Data of Training Set)

X_test: is input for Test Set

y_train: is Label for Training Set

y_test: is Label for Test Set

print ("X_train: ", X_train)


print ("y_train: ", y_train)
print("X_test: ", X_test)
print ("y_test: ", y_test)

# Add a Flatten layer to flatten the input data


model.add(layers.Flatten(input_shape=(13, 1)))
Flatten layer make these 13 dimensions () into Column Vector

# Add one or more Dense layers


model.add(layers.Dense(64, activation='relu'))
Dense means every member of the previous layer (Every member of column
Vector is connected with 64 neurons.

# You can add more hidden layers if needed


model.add(layers.Dense(32, activation='relu'))
Above 64 neurons are connected to every member of the 32 neurons.

# Add the output layer with a single neuron and sigmoid activation for
binary classification
model.add(layers.Dense(1, activation='sigmoid'))
Above 32 neurons are connected to only one neuron.

import numpy as np

import matplotlib.pyplot as plt

from tensorflow.keras.layers import Conv1D, Activation, SpatialDropout1D

from tensorflow.keras.layers import LayerNormalization, concatenate, Flatten

from tensorflow.keras.optimizers import Adam

from tensorflow.keras import Input, Model

from tensorflow.keras.layers import Dense

from tensorflow.keras.metrics import Precision, Recall, TruePositives, TrueNegatives, FalsePositives,


FalseNegatives, AUC

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix,


roc_auc_score, roc_curve

from sklearn.model_selection import train_test_split

import tensorflow as tf

from tensorflow import keras

from tensorflow.keras import layers

# Define the ANN model

model = keras.Sequential()
# Add a Flatten layer to flatten the input data

model.add(layers.Flatten(input_shape=(13, 1)))

# Add one or more Dense layers

model.add(layers.Dense(64, activation='relu'))

# You can add more hidden layers if needed

model.add(layers.Dense(32, activation='relu'))

# Add the output layer with a single neuron and sigmoid activation for binary classification

model.add(layers.Dense(1, activation='sigmoid'))

# Define additional performance metrics

metrics = [

'accuracy',

Precision(name='precision'),

Recall(name='recall'),

AUC(name='auc')

# Compile the model with the specified metrics

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=metrics)

In Keras, compiling the model means configuring the learning process. It's where you define the
optimizer, loss function, and metrics that you want to use. The optimizer is the algorithm that adjusts
the weights of the network to minimize the loss function.

random_state = 1 Controls the shuffling applied to the data before applying the split.
The Merge layer
Multiple Sequential instances can be merged into a single output via a Merge layer. The output
is a layer that can be added as first layer in a new Sequential model. For instance, here's a
model with two separate input branches getting merged:

from keras.layers import Merge

left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))

right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))

merged = Merge([left_branch, right_branch], mode='concat')

final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(10, activation='softmax'))

https://faroit.com/keras-docs/1.0.6/getting-started/sequential-model-guide/

You might also like