CHAPTER 4 - RUNNING A NEURAL NETWORK

FOR LINUX



AD

Your First Machine Learning Program with Linux

The goal of this section is to copy and paste a machine learning program into the command line and run the code file. We will discuss how the program works later.

The first step is to download the data for the program. Download pima-indians-diabetes.csv from networkrepository.com/pima-indians-diabetes.php . Open the file and remove the tab, comma, and semicolon checkmarks. Click OK. You should see comma delimited data in the LibreOffice Calc sheet. Save pima-indians-diabetes.csv to your working directory. For me it is /home/steve/

4.1 Open the terminal. Copy and paste the code below into the Nano file called test.py. Be sure to modify the path for pima-indians-diabetes.csv to your home directory, where it resides. Be sure to use sudo with the nano command.

steve@steve:~$    sudo nano test.py

4.1.1 Open the command line terminal on your computer with Ctrl-Alt-t . Be sure you are in the conda base by typing conda activate. From the base environment prompt type conda activate env2. You are now in the Anaconda Navigator env2 environment. This environment contains python, tensorflow, and keras. (See Chapter 2.)

In Chapter 2 we discussed Method 1 and Method 2 for placing a conda environment at the command line. Use which ever Method is appropriate. The prompt should look like the following:

(env2) steve@steve:~$

4.1.2 At the command prompt type python test.py to run the program. Make sure you are in the home directory of your prompt. The file test.py and pima-indians-diabetes.csv must be in the home directory also.

# first neural network with keras tutorial
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
# load the dataset
dataset = loadtxt('/home/steve/pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (y) variables
# 0-8 are dependent variables- 8th column not included
X = dataset[:,0:8]
# 8 column is independent variable
y = dataset[:,8]
# define the keras model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit the keras model on the dataset
model.fit(X, y, epochs=150, batch_size=16)
# evaluate the keras model
# Use _, to get last item from iteration
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f'% (accuracy*100))

4.1.3 You should see several hundred lines of output, with the Accuracy listed on the last line. If you do not see the output lines, there is an error. Try googling the error message if you do not understand the error message. Double check you paths,and installation.



Neural program output.

4.1.4 To exit the env2 and and return the prompt to base.

(env2) steve@steve:~$   conda deactivate

Output

(base) steve@steve:~$

4.1.5 To exit the base and return the prompt to normal.

(base) steve@steve:~$   conda deactivate

Output

steve@steve:~$





theSurfDragon.com


Conda Navigation

Table of Contents
Ch1-Installation
Ch2-Conda Navigator
Ch3-Nano Editor
Ch4-Run a Program
Ch5-Program Explained