title: Converting Tensor to Numpy Array - Custom Loss function In keras
body: <p>I am trying to build a custom loss function in keras. Unfortunately i have little knowledge with tensor flow. Is there a way i can convert the incoming tensors into a numpy array so i can compute my loss function?</p>
  
  <p>Here is my function:</p>
  
  <pre><code>def getBalance(x_true, x_pred):
  
      x_true = np.round(x_true)
      x_pred = np.round(x_pred)
  
      NumberOfBars = len(x_true)
      NumberOfHours = NumberOfBars/60
  
      TradeIndex = np.where( x_pred[:,1] == 0 )[0]
  
      ##remove predictions that are not tradable
      x_true = np.delete(x_true[:,0], TradeIndex)
      x_pred = np.delete(x_pred[:,0], TradeIndex)
  
      CM = confusion_matrix(x_true, x_pred)
  
      correctPredictions = CM[0,0]+CM[1,1]
      wrongPredictions = CM[1,0]+CM[0,1]
      TotalTrades = correctPredictions+wrongPredictions
      Accuracy = (correctPredictions/TotalTrades)*100
  
      return Accuracy 
  </code></pre>
  
  <p>If its not possible to use numpy array's what is the best way to compute that function with tensorflow? Any direction would be greatly appreciated, thank you!</p>
  
  <p><strong>Edit 1:</strong>
  Here are some details of my model. I am using a LSTM network with heavy drop out. The inputs are a multi-variable multi-time step.
  The outputs are a 2d array of binary digits (20000,2)</p>
  
  <pre><code>model = Sequential()
  
  model.add(Dropout(0.4, input_shape=(train_input_data_NN.shape[1], train_input_data_NN.shape[2])))
  
  model.add(LSTM(30, dropout=0.4, recurrent_dropout=0.4))
  
  model.add(Dense(2))
  
  model.compile(loss='getBalance', optimizer='adam')
  
  history = model.fit(train_input_data_NN, outputs_NN, epochs=50,  batch_size=64, verbose=1, validation_data=(test_input_data_NN, outputs_NN_test))
  </code></pre>