在tensorflow训练时,重量和成本不变

时间:2017-08-02 21:31:24

标签: tensorflow neural-network regression

我是一个新秀,并尝试使用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))

1 个答案:

答案 0 :(得分:0)

我认为您需要将费用函数更改为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))
相关问题