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