【问题标题】:Tensorflow always predict the same outputTensorFlow 总是预测相同的输出
【发布时间】:2017-11-07 05:20:13
【问题描述】:

所以,我正在尝试学习 tensorflow,为此,我尝试为我认为并不难的东西创建分类器。 我想预测一个数字是奇数还是偶数。 问题是 Tensorflow 总是预测相同的输出,我在最后几天搜索了答案,但没有任何帮助...... 我看到了以下答案:-Tensorflow predicts always the same result

-TensorFlow always converging to same output for all items after training

-TensorFlow always return same result

这是我的代码:

在:

df
    nb  y1
0   1   0
1   2   1
2   3   0
3   4   1
4   5   0
...
19  20  1

inputX = df.loc[:, ['nb']].as_matrix()
inputY = df.loc[:, ['y1']].as_matrix()
print(inputX.shape)
print(inputY.shape)

出来:

(20, 1) (20, 1)

在:

# Parameters
learning_rate = 0.00000001
training_epochs = 2000
display_step = 50
n_samples = inputY.size


x = tf.placeholder(tf.float32, [None, 1])   
W = tf.Variable(tf.zeros([1, 1]))           
b = tf.Variable(tf.zeros([1]))            
y_values = tf.add(tf.matmul(x, W), b)      
y = tf.nn.relu(y_values)                 
y_ = tf.placeholder(tf.float32, [None,1])  

# Cost function: Mean squared error
cost = tf.reduce_sum(tf.pow(y_ - y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initialize variabls and tensorflow session
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for i in range(training_epochs):  
    sess.run(optimizer, feed_dict={x: inputX, y_: inputY}) # Take a gradient descent step using our inputs and labels

    # Display logs per epoch step
    if (i) % display_step == 0:
        cc = sess.run(cost, feed_dict={x: inputX, y_:inputY})
        print("Training step:", '%04d' % (i), "cost=", "{:.9f}".format(cc)) #, \"W=", sess.run(W), "b=", sess.run(b)

print ("Optimization Finished!")
training_cost = sess.run(cost, feed_dict={x: inputX, y_: inputY})
print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

出来:

Training step: 0000 cost= 0.250000000
Training step: 0050 cost= 0.250000000
Training step: 0100 cost= 0.250000000
...
Training step: 1800 cost= 0.250000000
Training step: 1850 cost= 0.250000000
Training step: 1900 cost= 0.250000000
Training step: 1950 cost= 0.250000000
Optimization Finished!
Training cost= 0.25 W= [[ 0.]] b= [ 0.]

在:

sess.run(y, feed_dict={x: inputX })

出来:

array([[ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.],
       [ 0.]], dtype=float32)

我尝试使用我的 Hyper 参数,例如学习率或训练周期数。 我将激活函数从 softmax 更改为 relu。 我更改了我的数据框以获取更多示例,但什么也没发生。 我还尝试为我的权重添加随机数,但没有任何改变,成本刚刚开始上升。

【问题讨论】:

    标签: python machine-learning tensorflow neural-network deep-learning


    【解决方案1】:

    快速浏览一下代码,对我来说它看起来不错(可能是将权重初始化为零的部分,通常您需要一个不同于零的小数字以避免微不足道的解决方案),但我不认为你可以用线性回归来拟合整数奇偶校验的问题。

    关键是你正在努力适应

    x % 2
    

    有形式的预测

    activation(x * w + b)
    

    而且没有办法找到好的w和b来解决这个问题。

    另一种理解这一点的方法是绘制数据:x 奇偶校验的散点图是两条线,用一条线拟合它们的唯一方法是使用一条平线(这将有反正成本很高)。

    我认为最好先更改数据,但是如果要解决此问题,则应该使用正弦或余弦作为激活函数来获得一些结果。

    【讨论】:

    • 坦克!我会尝试使用其他数据,并使用随机的起始重量。
    • 非常老的问题/答案,但为什么初始化零权重会导致问题?我在这里发布了一个类似的问题:datascience.stackexchange.com/questions/64520/…如果你有兴趣看看。
    【解决方案2】:

    我看到的主要问题是你在 W 矩阵中用 0 初始化你的权重。您在线性层中的操作基本上是 Wx + b。因此,关于 x 的梯度是 W。如果你现在从 W 的零开始,那么梯度也是 0,你什么也学不到。尝试使用 tensorflow.org 上所述的随机初始值

    # Create two variables.
    weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),
                          name="weights")
    biases = tf.Variable(tf.zeros([200]), name="biases")
    

    【讨论】:

      【解决方案3】:

      首先我必须承认我从未使用过 tensorflow。但我认为你在这里有一个建模问题。

      您正在使用最简单的网络架构(一维perceptron)。您有两个要学习的变量(w 和 b),输出的决策规则如下所示

      如果你减去 b 并除以 w 你得到 ​​p>

      所以你基本上是在寻找一个阈值来区分奇数和偶数。无论您如何选择 w 和 b,您总是会错误分类一半的数字。

      虽然判断一个数字是奇数还是偶数对我们人类来说似乎是一项非常微不足道的任务,但对于单个感知器来说却不是。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-04
        • 2017-04-25
        • 1970-01-01
        相关资源
        最近更新 更多