python - 你如何在 Tensorflow 中进行预测

标签 python machine-learning neural-network tensorflow

我是 tensorflow 的菜鸟。所以我在研究 Xor 问题,我的问题是你如何在 tensorflow 中进行预测。所以当我输入 [1,0] 时,我希望它给我 1 或 0。同样在不同的情况下,如果它是意味着具有多个值(回归量)的模型,例如股票。我该怎么做 谢谢。 到目前为止,我做到了这一点:

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 })

最佳答案

由于您的分类只是 0 当且仅当 Output<0.5 您可以添加新的预测节点:

prediction_op = tf.round(Output)

然后调用它

print(sess.run(prediction_op, feed_dict={X: np.array([[1., 0.]])}))

关于python - 你如何在 Tensorflow 中进行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40273426/

相关文章:

python - django.db.utils.IntegrityError : NOT NULL constraint failed: products_product. 图像错误与图像字段

python - 检测图像中的多个矩形

python - Python 中存在 MemoryError,但 IPython 中没有

machine-learning - 咖啡 | solver.prototxt值设置策略

python - 创建神经网络

neural-network - 如何选择每个卷积层的过滤器数量?

python - 通过添加重复的数字来更改序列的名称

python - 如何从 dlib.simple_object_detector 获得置信度

machine-learning - 用于 Web 应用程序的 SVM

python - 如何使用 Python 绘制成本曲线