使用TensorFlow的简单前馈神经网络将无法学习

Giz*_*zmo 6 python neural-network tensorflow

我正在尝试使用TensorFlow构建一个简单的神经网络.目标是在32像素x 32像素图像中找到矩形的中心.矩形由五个向量描述.第一个矢量是位置矢量,另外四个是方向矢量,构成矩形边.一个向量具有两个值(x和y).

在此输入图像描述

该图像的相应输入将是(2,5)(0,4)(6,0)(0,-4)( - 6,0).中心(因此所需的输出)位于(5,7).

我想出的代码如下所示:

    import tensorflow as tf 
    import numpy as np
    import Rectangle_Records

    def init_weights(shape):
        """ Weight initialization """
        weights = tf.random_normal(shape, stddev=0.1)
        return tf.Variable(weights)

    def forwardprop(x, w_1, w_2):
        """ Forward-propagation """
        h = tf.nn.sigmoid(tf.matmul(x, w_1))
        y_predict = tf.matmul(h, w_2)
        return y_predict

    def main():
        x_size = 10
        y_size = 2
        h_1_size = 256

        # Prepare input data
        input_data = Rectangle_Records.DataSet()

        x = tf.placeholder(tf.float32, shape = [None, x_size])
        y_label = tf.placeholder(tf.float32, shape = [None, y_size])

        # Weight initializations
        w_1 = init_weights((x_size, h_1_size))
        w_2 = init_weights((h_1_size, y_size))

        # Forward propagation
        y_predict = forwardprop(x, w_1, w_2)

        # Backward propagation
        cost = tf.reduce_mean(tf.square(y_predict - y_label))

        updates = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

        # Run
        sess = tf.Session()
        init = tf.global_variables_initializer()
        sess.run(init)

        for i in range(200):
            batch = input_data.next_batch(10)
            sess.run(updates, feed_dict = {x: batch[0], y_label: batch[1]})

        sess.close()

    if __name__ == "__main__":
        main()

可悲的是,网络无法正常学习.结果太过分了.例如,[[3.74561882,3.70766664]]应该是[[3.,7.]].我究竟做错了什么?

vij*_*y m 14

主要问题是你的整个训练只是针对那些one epoch训练不够.请尝试以下更改:

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for j in range(30):
    input_data = Rectangle_Records.DataSet()
    for i in range(200):
        batch = input_data.next_batch(10)
        loss, _ = sess.run([cost,updates], feed_dict = {x: batch[0], y_label: batch[1]})

    pred = sess.run(y_predict, feed_dict={x: batch[0]})
    print('Cost:', loss  )
    print('pred:', pred)
    print('actual:', batch[1])
sess.close()
Run Code Online (Sandbox Code Playgroud)

将优化器更改为动量优化器以加快收敛速度​​: tf.train.AdamOptimizer(0.01).minimize(cost)