Monday, 3 August 2020

Tensorflow and antibiotics

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.

Algorithm ref: Tensorflow for dummies by Mathew Scarpino, page 114:

This model computes loss with cross entropy.


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
from math import log
from numpy import mean
# 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))

loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(_sentinel = None, labels = y, logits = tf.nn.sigmoid(tf.add(tf.multiply(x, m),c)), name = None))


# calculate cross entropy
def cross_entropy(x_train, ytrain):
    return -sum([x_train[i]*log(y_train[i]) for i in range(len(x_train))])


results = list()
for i in range(len(x_train)):
    # create the distribution for each event {0, 1}
    expected = [1.0 - x_train[i], x_train[i]]
    predicted = [1.0 - y_train[i], y_train[i]]
    # calculate cross entropy for the two events
    ce = cross_entropy(expected, predicted)
    print('>[y=%.1f, yhat=%.1f] ce: %.3f nats' % (x_train[i], y_train[i], ce))
    results.append(ce)
 
# calculate the average cross entropy
mean_ce = mean(results)
print('Average Cross Entropy: %.3f nats' % mean_ce)


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()


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})
                

#final values of m and c
print('')
print('m =', session.run(m))
print('c =', session.run(c))
print('Final loss: ', lossval)

Here is the output:


(virtualenvironment.) david@debian:~/pythonvirenv$ python3 sigmodelsigloss.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)])
>[y=0.1, yhat=0.9] ce: 0.088 nats
>[y=0.1, yhat=0.9] ce: 0.088 nats
>[y=0.1, yhat=0.9] ce: 0.088 nats
>[y=0.1, yhat=0.9] ce: 0.088 nats
>[y=0.1, yhat=0.9] ce: 0.087 nats
>[y=0.1, yhat=0.9] ce: 0.088 nats
>[y=0.1, yhat=0.9] ce: 0.087 nats
>[y=0.2, yhat=0.9] ce: 0.087 nats
Average Cross Entropy: 0.088 nats
2020-08-06 22:15:04.888524: 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-06 22:15:04.888610: 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-06 22:15:04.888639: 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.

m = 0.017963253
c = 0.13417429
Final loss:  [0.49679244]
(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 = 0.017963253*x + 0.13417429
-0.13417429 =  0.017963253*x
x = -0.13417429 / 0.017963253


x = −−7.469375953




To scale up after normalising at the start we multiply by 1000
x = -7469 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