【问题标题】:Weights and costs unchanged when training in tensorflow在 tensorflow 中训练时权重和成本不变
【发布时间】:2026-01-25 16:05:01
【问题描述】:

我是个菜鸟,尝试使用 tensorflow 解决多输入多输出问题。但是,在训练过程中,网络的权重和成本是不变的。这是一些主要代码,任何建议将不胜感激!

learning_rate = 0.01
training_epoch = 2000
batch_size = 100
display_step = 1

# place holder for graph input
x = tf.placeholder("float64", [None, 14])
y = tf.placeholder("float64", [None, 8])

# model weights
w_1 = tf.Variable(tf.zeros([14, 11], dtype = tf.float64))
w_2 = tf.Variable(tf.zeros([11, 8], dtype = tf.float64))

# construct a model
h_in = tf.matmul(x, w_1)
h_out = tf.nn.relu(h_in)
o_in = tf.matmul(h_out, w_2)
o_out = tf.nn.relu(o_in)

# cost: mean square error
cost = tf.reduce_sum(tf.pow((o_out - y), 2))

# optimizer
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# initializer
init = tf.global_variables_initializer()

# launch the graph
with tf.Session() as sess:
    sess.run(init)

    for epoch in range(training_epoch):
        pos = 0;
        # loop over all batches
        if pos < train_input_array.shape[0]:
            # get the next batch
            batch_i = []
            batch_o = []
            for i in range(pos, pos + batch_size):
                batch_i.append(train_input_array[i].tolist())
                batch_o.append(train_output_array[i].tolist())
            np.array(batch_i)
            np.array(batch_o)
            pos += batch_size;
        sess.run(optimizer, feed_dict = {x: batch_i, y: batch_o})
        print sess.run(w_2[0])

        if (epoch + 1) % display_step == 0:
            c = sess.run(cost, feed_dict = {x: batch_i, y: batch_o})
            print("Epoch: ", "%04d" % (epoch + 1), "cost: ", "{:.9f}".format(c))

【问题讨论】:

    标签: tensorflow neural-network regression


    【解决方案1】:

    我认为你需要改变你的成本函数,以 reduce_mean

    # reduce sum doesn't work
    cost = tf.reduce_sum(tf.pow((o_out - y), 2))
    # you need to use mean 
    cost = tf.reduce_mean(tf.pow((o_out - y), 2))
    

    【讨论】:

    • 我改成reduce_mean,但是loss还是没变。
    • 你可以尝试使用momentm优化器和更多参数
    • 感谢您的建议,我将 weights 变量初始化器从 tf.zero 更改为 tf.random_normal,loss 终于开始下降了。