Friday, 14 August 2020

Tensorflow, antibiotics and MSE

The following network was trained with micrograms /ml of total antibiotic as input , and % success rate as output.
The antibiotic dose x was normalised by dividing by 1000 so that the values fell between 0 and 1. Similarly, y the success rate was divided by 100.


Figures for total dose and % success rate were obtained from table 2 from the web page

https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5124968/

The link to table 2 is

https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5124968/table/t2/?report=objectonly

Table 2 is contained in the chapter entitled Genetic algorithm with the deterministic model.
The function y= mx + c = 0 was used to measure the gradient of the data.

REF: Algorithm for calculating mean squared error (MSE) from 

Pro Deep Learning with Tensorflow  by Santanu Pattanayak, page 144:


This model computes loss using MSE.

A previous post uses cross entropy to calculate loss. The link is

http://pythonprediction.blogspot.com/2020/08/tensorflow-and-antibiotics.html


The code from this program can be saved as filename.py

The network can be run from a terminal by typing:
python3 filename.py

Here is the code:

import tensorflow as tf


# values of x = [0.118, 0.128, 0.122, 0.128, 0.141, 0.132, 0.143, 0.156]
# values of y = [0.912, 0.943, 0.923, 0.932, 0.944, 0.925, 0.940, 0.950]
x_train = [0.118, 0.128, 0.122, 0.128, 0.141, 0.132, 0.143, 0.156]
y_train = [0.912, 0.943, 0.923, 0.932, 0.944, 0.925, 0.940, 0.950]


m = tf.Variable(0.)
c = tf.Variable(0.)

x = tf.placeholder(dtype = tf.float32)
y = tf.placeholder(dtype=tf.float32)



# using sigmoid function y = mx + c
model = tf.nn.sigmoid(tf.add(tf.multiply(x, m),c))

pred = tf.add(tf.multiply(x, m),c)
error = pred - y
loss = tf.reduce_mean(tf.square(error))

learn_rate = 0.005
num_epochs = 350

#using Gradient Descent with learning rate 0.005
train = tf.train.GradientDescentOptimizer(learn_rate).minimize(loss)
session = tf.Session()
init = tf.global_variables_initializer()

loss_trace = []


session.run(init)

#training model for 350 iterations
for epoch in range(num_epochs):
    session.run([train], {x:x_train, y:y_train})
#   lossval = session.run([loss], {x:x_train, y:y_train})
    loss_trace.append(session.run([loss], {x:x_train, y:y_train}))
    pred = tf.add(tf.multiply(x, m),c)
    error = pred - y
    error = session.run([error], {x:x_train, y:y_train})
    prediction = session.run([pred],{x:x_train, y:y_train})
    print('Iter: ', epoch, 'MSE in training: ', loss_trace[-1])
 
#final values of m and c
print('')
print('m =', session.run(m))
print('c =', session.run(c))
print('Final loss: ', loss_trace[-1])
print('Prediction :' , prediction)
print('Error: ', error)

import matplotlib.pyplot as plt
plt.xlabel('Number of epochs --------->')
plt.ylabel('Error (MSE) --------->')
plt.plot(loss_trace)
plt.show()


Here is the output:


(virtualenvironment.) david@debian:~/pythonvirenv$ python3 antibioticmsev.py
/home/david/pythonvirenv/virtualenvironment./lib/python3.5/site-packages/tensorflow/python/framework/dtypes.py:455: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/david/pythonvirenv/virtualenvironment./lib/python3.5/site-packages/tensorflow/python/framework/dtypes.py:456: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/david/pythonvirenv/virtualenvironment./lib/python3.5/site-packages/tensorflow/python/framework/dtypes.py:457: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/david/pythonvirenv/virtualenvironment./lib/python3.5/site-packages/tensorflow/python/framework/dtypes.py:458: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/david/pythonvirenv/virtualenvironment./lib/python3.5/site-packages/tensorflow/python/framework/dtypes.py:459: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/david/pythonvirenv/virtualenvironment./lib/python3.5/site-packages/tensorflow/python/framework/dtypes.py:462: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
2020-08-14 18:47:47.336227: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2020-08-14 18:47:47.336326: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2020-08-14 18:47:47.336355: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
Iter:  0 MSE in training:  [0.85414684]
Iter:  1 MSE in training:  [0.8368502]
Iter:  2 MSE in training:  [0.819904]
Iter:  3 MSE in training:  [0.803301]
Iter:  4 MSE in training:  [0.7870342]
.
.
.
Iter:  347 MSE in training:  [0.0008233281]
Iter:  348 MSE in training:  [0.0008090532]
Iter:  349 MSE in training:  [0.00079507055]

m = 0.11940679
c = 0.89168817
Final loss:  [0.00079507055]
Prediction : [array([0.90577817, 0.9069722 , 0.9062558 , 0.9069722 , 0.9085245 ,
       0.90744984, 0.90876335, 0.91031563], dtype=float32)]
Error:  [array([-0.00622183, -0.03602779, -0.0167442 , -0.02502775, -0.03547549,
       -0.01755017, -0.03123665, -0.03968436], dtype=float32)]
(virtualenvironment.) david@debian:~/pythonvirenv$






Results
-------------

For y= m*x + c 
as we have a value for m and a value for c,  we can calculate the value of X when 0 = mx + c for the gradient at which the neural network starts to learn.
These are the steps:
y = m*x + c
0 = m*x + c
 
−0.89168817 =  0.11940679x
x = -0.89168817 / 0.11940679
x = -7.467650458



To scale up after normalising at the start we multiply by 1000
x = -7467 micrograms or -7.5 milligrams.
 

This is the dose at the gradient of the neural net starting to learn, therefore the therapeutic value of the antibiotic starts at the dose -7.5 milligrams /ml.

Conclusion
--------------------
The therapeutic starting value of the dose is negative which leads me to assume that the therapeutic value depends on the cumulative effect of antibiotics already in the environment or previous dosage.



No comments:

Post a Comment