Python 为什么简单回归的TensorFlow结果远离其输入?

Python 为什么简单回归的TensorFlow结果远离其输入?,python,numpy,tensorflow,machine-learning,neural-network,Python,Numpy,Tensorflow,Machine Learning,Neural Network,我有一个简单的回归训练数据如下。我想用TensorFlow对网络进行训练,然后再次将[1 0 1]输入到网络中(与示例3相同),这将使我得到接近1的值(比如0.99) 下面是我的TensorFlow代码(在Python3中)。我用了一个线性图层,然后是一个S形图层。我用的是均方损失。请注意,在最后几行中,我输入了[1 0 1]来测试模型的预测能力。我只得到了0.5015,这与我的期望相差甚远(即0.99) 第1版:TensorFlow代码: import tensorflow as tf im

我有一个简单的回归训练数据如下。我想用TensorFlow对网络进行训练,然后再次将
[1 0 1]
输入到网络中(与示例3相同),这将使我得到接近1的值(比如0.99)

下面是我的TensorFlow代码(在Python3中)。我用了一个线性图层,然后是一个S形图层。我用的是均方损失。请注意,在最后几行中,我输入了
[1 0 1]
来测试模型的预测能力。我只得到了
0.5015
,这与我的期望相差甚远(即
0.99

第1版:TensorFlow代码:

import tensorflow as tf
import numpy as np

batch_xs=np.array([[0,0,1],[1,1,1],[1,0,1],[0,1,1]])
batch_ys=np.array([[0],[1],[1],[0]])

x = tf.placeholder(tf.float32, [None, 3])
W = tf.Variable(tf.zeros([3, 1]))
b = tf.Variable(tf.zeros([1]))

y = tf.nn.sigmoid(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 1])

mean_square_loss = tf.reduce_mean(tf.square(y_ - y)) 

learning_rate = 0.05

train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(mean_square_loss)
sess = tf.Session()
tf.global_variables_initializer().run(session=sess)

sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

x0=np.array([[1.,0.,1.]])
x0=np.float32(x0)
y0=tf.nn.sigmoid(tf.matmul(x0,W) + b)

print('%.15f' % sess.run(y0))
from numpy import exp, array, random, dot
training_set_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
training_set_outputs = array([[0, 1, 1, 0]]).T
random.seed(1)
synaptic_weights = 2 * random.random((3, 1)) - 1
for iteration in range(10000):
output = 1 / (1 + exp(-(dot(training_set_inputs, synaptic_weights))))
synaptic_weights += dot(training_set_inputs.T, (training_set_outputs - output) * output * (1 - output))
print(1 / (1 + exp(-(dot(array([1, 0, 1]), synaptic_weights)))))
为什么结果与预期值相差甚远?如果我只是使用Numpy而不是TensorFlow,下面的9行代码可以实现
0.9936
的输出

第2版:Numpy代码:

import tensorflow as tf
import numpy as np

batch_xs=np.array([[0,0,1],[1,1,1],[1,0,1],[0,1,1]])
batch_ys=np.array([[0],[1],[1],[0]])

x = tf.placeholder(tf.float32, [None, 3])
W = tf.Variable(tf.zeros([3, 1]))
b = tf.Variable(tf.zeros([1]))

y = tf.nn.sigmoid(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 1])

mean_square_loss = tf.reduce_mean(tf.square(y_ - y)) 

learning_rate = 0.05

train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(mean_square_loss)
sess = tf.Session()
tf.global_variables_initializer().run(session=sess)

sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

x0=np.array([[1.,0.,1.]])
x0=np.float32(x0)
y0=tf.nn.sigmoid(tf.matmul(x0,W) + b)

print('%.15f' % sess.run(y0))
from numpy import exp, array, random, dot
training_set_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
training_set_outputs = array([[0, 1, 1, 0]]).T
random.seed(1)
synaptic_weights = 2 * random.random((3, 1)) - 1
for iteration in range(10000):
output = 1 / (1 + exp(-(dot(training_set_inputs, synaptic_weights))))
synaptic_weights += dot(training_set_inputs.T, (training_set_outputs - output) * output * (1 - output))
print(1 / (1 + exp(-(dot(array([1, 0, 1]), synaptic_weights)))))

如何修复版本1中的TensorFlow代码,使结果接近0.99?非常感谢

在你的tensorflow代码中
sess.run(训练步,进给量={x:batch\uxs,y:batch\uys})
更新/训练你的重量。请注意,此操作仅运行一次,学习率为0.05。但在numpy代码中,您运行10000次迭代,这相当于

for i in range(10000):
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

结果应该是0.95左右。如果你将学习率增加到1,就像在numpy代码上那样,你应该会得到预期的行为(0.99)。

在tensorflow代码中
sess.run(训练步,进给dict={x:batch\uxs,y:batch\uys})
更新/训练你的权重。请注意,此操作仅运行一次,学习率为0.05。但在numpy代码中,您运行10000次迭代,这相当于

for i in range(10000):
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
结果应该是0.95左右。如果您将学习率增加到1,就像在numpy代码上所做的那样,您应该会得到预期的行为(0.99)