我是Tensorflow的新手。我正在尝试解决Xor问题,我的疑问是如何在Tensorflow中进行预测。例如,当我输入[1,0]时,我希望它能输出1或0。此外,在另一种情境下,如果模型需要处理多个值(如回归模型)例如股票数据,该如何操作?感谢您。目前我的进展如下:
import tensorflow as tfimport numpy as npX = tf.placeholder(tf.float32, shape=([4,2]), name = "Input")y = tf.placeholder(tf.float32, shape=([4,1]), name = "Output")#weightsW = 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 modelNode1 = tf.sigmoid(tf.matmul(X, W)+ Biases1)Output = tf.sigmoid(tf.matmul(Node1, w2)+ Biases2)#Setting up the Cost functioncost = tf.reduce_mean(((y* tf.log(Output))+ ((1-y)* tf.log(1.0 - Output)))* -1)#Now to training and optimizingtrain_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 sessioninitial = 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 })
回答:
由于您的分类规则是当Output小于0.5时输出0,您可以添加一个新的预测节点:
prediction_op = tf.round(Output)
然后调用它
print(sess.run(prediction_op, feed_dict={X: np.array([[1., 0.]])}))