title: How to improve my neural network accuracy function such that keras lazy evaluation is possible
body: <p>I currently have my neural network accuracy function (and neural network) as follows:</p>
  <pre><code>import tensorflow as tf
  from tensorflow import keras
  from tensorflow.keras import layers
  from tensorflow.keras import models
  from tensorflow.keras.models import Sequential
  from tensorflow.keras.layers import Dense
  from tensorflow.keras.layers import LSTM
  import numpy as np
  
  def predoneacc(y_true, y_pred):
      y_nptrue = y_true.numpy()
      y_nppredinit = y_pred.numpy()
  
      y_nppred = np.round(y_nppredinit)
      return arr1eq(y_nptrue,y_nppred)
  
  def arr1eq(y_true, y_pred):
      k = 0.0
      count1 = 0.0
      for i in range(len(y_true)):
          for j in range(len(y_true[0])):
              if y_pred[i][j] == 1.0:
                  count1 = count1+1.0
                  if y_true[i][j] == 1.0:
                      k = k+1.0
      if(count1==0.0): return 0
      else: return k/count1
  
  tap=5
  
  model = keras.Sequential([
      keras.layers.LSTM(300, input_shape=(tap,45)),
      keras.layers.Dense(45, activation='sigmoid')
  ])
  model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=[predoneacc], run_eagerly=True)
  model.fit(x,y,epochs=150) #x,y data properly given input/output shape required by the network
  </code></pre>
  <p>But this accuracy function cannot be used by TensorFlow keras <a href="https://keras.io/api/models/model_training_apis/" rel="nofollow noreferrer"><code>tensorflow.keras.models.compile()</code></a> unless <code>run_eagerly=True</code> is set because <code>.numpy()</code> is used. The code runs up to <code>model.fit()</code>, which complains that I have to set eager evaluation on. So I would like to re-code my accuracy function so that <code>.compile()</code> can accept the accuracy function without setting <code>run_eagerly=True</code> in <code>.compile()</code>.</p>
  <p>What would be the way out? my accuracy function currently simply returns number of accurate 1 predictions divided by total number of 1 predictions in binary classification problems, with output layer activation function being sigmoid.</p>
  <p>I have tensorflow 2.0.</p>