0

I'm very new to tensorflow, and I want to define my own cost function using numpy for my project described here

This is my code I copied:

# Backward propagation
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=yhat))
updates = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

# Run SGD
sess = tf.Session()
init = tf.global_variables_initializer()

sess.run(updates, feed_dict={X: train_X, y: train_y})

I want to change this cost function to another function I already have defined using numpy. Is there a way to convert the y tensor to a numpy array and then back to a tensor.

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=yhat))

I haven't had success using transpose() and eval() because it was necessary to be run during a tf.session.

Here is the loss function in numpy:

def loss_perunage(ones_array,votearray):
    results = countresults(ones_array,votearray)
    return 1 - results[0]/int(len(a))

The function uses another dataset to compute how good the results are. I want this perunage be the cost.

Hope you guys can give me tips on this, feel free to ask anything if something is unclear.

Edit: Sorry for my English, I'm not native

Luke DeLuccia
  • 541
  • 6
  • 16
DJosh
  • 15
  • 1
  • 5
  • Hello @DJosh, this question may be a duplicate of, https://stackoverflow.com/questions/33684441/writing-a-custom-cost-function-in-tensorflow, which is worth a read! Basically, you should be defining your own cost functions using `tensorflow` operations because NumPy isn't designed to handle `tensorflow` objects. – Dascienz Jan 08 '19 at 21:19
  • Possible duplicate of [writing a custom cost function in tensorflow](https://stackoverflow.com/questions/33684441/writing-a-custom-cost-function-in-tensorflow) – Dascienz Jan 08 '19 at 21:20

0 Answers0