Accessing Variable in Keras Callback
<p>So I've a CNN implemented. I have made custom callbacks that are confirmed working but I have an issue.</p>

<p>This is a sample output.
Example of iteration 5 (batch-size of 10,000 for simplicity)</p>

<pre><code>50000/60000 [========================&gt;.....] - ETA: 10s ('new lr:', 0.01)

('accuracy:', 0.70)
</code></pre>

<p>I have 2 callbacks (tested to work as shown in the output):
(1) Changes the learning rate at each iteration. (2) Prints the accuracy at each iteration.</p>

<p>I have an external script that determines the learning rate by taking in the accuracy. </p>

<p><strong>Question:</strong>
How to make the accuracy at each iteration available so that an external script can access it? In essence an accessible variable at each iteration. I'm able to access it only once the process is over with <code>AccuracyCallback.accuracy</code></p>

<p><strong>Problem</strong>
I can pass a changing learning rate. But how do I get the accuracy once this has been passed in a form of an accessible variable at each iteration?</p>

<p><strong>Example</strong>
My external script determines the learning rate at iteration 1: 0.01. How do I get the accuracy as an accessible variable in my external script at iteration 1 instead of a print statement?</p>

<p>You can <a href="https://keras.io/callbacks/#create-a-callback" rel="nofollow">create your own callback</a></p>

<pre><code>class AccCallback(keras.callbacks.Callback):

    def on_batch_end(self, batch, logs={}):
        accuracy = logs.get('acc')
        # pass accuracy to your 'external' script and set new lr here
</code></pre>

<p>In order for <code>logs.get('acc')</code> to work, you have to tell Keras to monitor it:</p>

<pre><code>model.compile(optimizer='...', loss='...', metrics=['accuracy'])
</code></pre>

<p>Lastly, note that the type of <code>accuracy</code> is <code>ndarray</code> here. Should it cause you any issue, I suggest wrapping it: <code>float(accuracy)</code>.</p>

python keras