This document discusses getting started with Keras for binary classification. It covers installing Keras with different backends like TensorFlow, Theano, or CNTK. It also covers configuring Keras by editing the keras.json file to select a backend and set the image data format. The document then demonstrates building a basic binary classification model on the Breast Cancer dataset using Keras sequential API to add layers, compile, train, and predict with the model.
1. Keras: Hello World:
Installation, and
Binary Classification
by H. Shah-Hosseini
www.linkedin.com/in/dr-hamed-shah-hosseini
Keras: Hello World, by H. Shah-Hosseini 1
Prerequisite: being Familiar with Python and Scikit-learn
2. Keras: Preliminaries
you should have installed one of theses as backend of keras :
tensorflow
cntk
theano
The default backend is specified in file keras.json
----------------------------------------------
If you have run Keras at least once, you will find the Keras configuration file at:
$HOME/.keras/keras.json
----------------------------------------------------
If keras,json is not there, you can create it.
NOTE for Windows Users: Please replace $HOME with %USERPROFILE%.
Keras: Hello World, by H. Shah-Hosseini 2
3. Keras: Preliminaries (2)
The default configuration file ‘keras.json’ looks like this:
{
"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
--------------------------------------------------------------
You can change the field backend to "theano", "tensorflow", or "cntk“
Then, Keras will use the new configuration, the next time you run any Keras code.
Keras: Hello World, by H. Shah-Hosseini 3
4. Keras: image_data_format
image_data_format:
it is String, either "channels_last" or "channels_first".
It specifies which data format convention Keras will follow.
keras.backend.image_data_format() returns the data format it uses now.
------------------------------------------------------------
For 2D data (e.g. image):
"channels_last" assumes (rows, cols, channels)
"channels_first" assumes (channels, rows, cols)
--------------------------------------------------------------------
For 3D data:
"channels_last" assumes (conv_dim1, conv_dim2, conv_dim3, channels)
"channels_first" assumes (channels, conv_dim1, conv_dim2, conv_dim3).
-------------------------------------------------------------------
Keras: Hello World, by H. Shah-Hosseini 4
5. Keras: image_data_format:
how to change in code?
How to change defaults in program:
>>> from keras import backend as K
>>> K.image_data_format()
'channels_first'
>>> K.set_image_data_format('channels_last')
>>> K.image_data_format()
'channels_last'
-------------------------
In code above, image_data_format was ‘channels_first’, but we
changed it to ‘channels_last’
and we checked it that it has been done correctly.
Keras: Hello World, by H. Shah-Hosseini 5
6. Keras: Installation
First, you should install anaconda or Miniconda, to program in python.
Then, if you want Keras on Theano, use at Cmd:
conda install -c conda-forge keras
------------------------------
If you want Keras on CNTK, use at Cmd:
First, install cntk; then, install keras, like this:
pip install <url-of-cntk>
Pip install keras
---------------------
If you want Keras on TensorFlow, use at Cmd:
pip install tensorflow-gpu
pip install keras
Keras: Hello World, by H. Shah-Hosseini 6
https://www.tensorflow.org/install/install_windows
https://keras.io/
https://docs.microsoft.com/en-us/cognitive-
toolkit/Setup-Windows-Python?tabs=cntkpy22
References:
Cmd=Command prompt
7. Keras: Installation
Now, try the following python command in your python editor,
and if running it doesn’t not give any error, you are ok:
Keras: Hello World, by H. Shah-Hosseini 7
8. Keras: Binary classification
Use BreastCancer dataset which is a
binary classification:
----------------------
Model creation:
We begin with Sequential()
and then, we add dense (fully
connected) layers with Dense.
Keras: Hello World, by H. Shah-Hosseini 8
9. Keras: Binary classification (2)
We then use compile to specify the optimization algorithm
options such the optimization method, the loss function, and
metrics:
---------------------------------------
Next, we use fit to train the model:
Keras: Hello World, by H. Shah-Hosseini 9
10. Keras: Binary classification (3)
To predict with the trained model, we use predict:
-----------------------------
Since this is classification, we must convert the vector to nearest
integer. Here, zero or one.
Keras: Hello World, by H. Shah-Hosseini 10