Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you predict in Tensorflow

I'm a noob to tensorflow. So I was playing around with the Xor problem my question is how do you predict in tensorflow. So when i enter [1,0], i want it to give me either 1 or 0. Also in a different scenario if it is model which is meant to have more than multiple values(regressor)eg stocks. How would i do that Thank you. So far i got this far:

import tensorflow as tf
import numpy as np
X = tf.placeholder(tf.float32, shape=([4,2]), name = "Input")
y = tf.placeholder(tf.float32, shape=([4,1]), name = "Output")
#weights
W = tf.Variable(tf.random_uniform([2,2], -1,1), name = "weights1")
w2 = tf.Variable(tf.random_uniform([2,1], -1,1), name = "weights2")
Biases1 = tf.Variable(tf.zeros([2]), name = "Biases1")
Biases2 = tf.Variable(tf.zeros([1]), name = "Biases2")
#Setting up the model
Node1 = tf.sigmoid(tf.matmul(X, W)+ Biases1)
Output = tf.sigmoid(tf.matmul(Node1, w2)+ Biases2)
#Setting up the Cost function
cost = tf.reduce_mean(((y* tf.log(Output))+ 
                       ((1-y)* tf.log(1.0 - Output)))* -1)
#Now to training and optimizing
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
xorX = np.array([[0,0], [0,1], [1,0], [1,1]])
xorY = np.array([[0], [1], [1], [0]])
#Now to creating the session
initial = tf.initialize_all_variables()
sess = tf.Session()
sess.run(initial)
for i in range(100000):
    sess.run(train_step, feed_dict={X: xorX, y: xorY })
like image 540
sam202252012 Avatar asked Jan 28 '26 13:01

sam202252012


1 Answers

Since your classification is simply 0 iff Output<0.5 you can add new, prediction node:

prediction_op = tf.round(Output)

and call it afterwards

print(sess.run(prediction_op, feed_dict={X: np.array([[1., 0.]])}))
like image 200
lejlot Avatar answered Jan 30 '26 02:01

lejlot