输入是类似于:

[[ 0.  0.  0.  0.  0.  0.  0. ],
 [ 0.  0.  0.  0.  0.  0.  0. ],
 ......
 [ 0.  0.  0.  0.  0.  0.  0. ]]

的数组,设数组变量名为 X 则有:

  • len(X)为单组训练数据中得数据个数
  • len(X[index])为每个训练数据的特征数量,即输入节点个数

输出是类似于:

[[ 0 ], [ 0 ], [ 0 ], [ 0 ].........[ 0 ]]

即只有一个输出节点,值类型为float

 

随机初始化用的函数:

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)


def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

输入输出及基本参数设置:

learning_rate = 1.5
training_epochs = 1000000
display_step = 5000

X = tf.placeholder('float', shape=[None, 7])
Y = tf.placeholder('float', shape=[None, 1])

构建网络

BP神经网络包括输入层,隐层和输出层,使用反向传播梯度下降法来训练各个节点上得权重,得到目标输出

w_H_F = weight_variable([7, 16])
b_H_F = bias_variable([16])

t_X = tf.reshape(X, [-1, 7])

h_fc1 = tf.nn.relu(tf.matmul(t_X, w_H_F) + b_H_F)

w_out = weight_variable([16, 1])
b_out = bias_variable([1])

y_out = tf.nn.softmax(tf.matmul(h_fc1, w_out) + b_out)

t_X将输入转化为向量        h_fc1 即隐层 1        y_out即输出层

  • 隐节点输出模型:Oj=f(∑Wij×Xi-qj)
  • 输出节点输出模型:Yk=f(∑Tjk×Oj-qk)

训练目标是使输出偏差尽可能最小(Cost函数值得再次考虑)

cost = tf.reduce_sum(tf.abs(Y - y_out))
nn = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

初始化并运行网络

init = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(training_epochs):
        train_x, train_y = get_train_data()
        sess.run(nn, feed_dict={X: train_x, Y: train_y})
        # print('Now At : ' + str(epoch))

        if epoch % display_step == 0:
            test_x, test_y = get_train_data()
            print("Epoch {epoch} : cost={cost}".format(epoch=epoch,
                                                       cost=str(sess.run(cost, feed_dict={X: test_x, Y: test_y}))))

还没有出过结果。。。因为数据的预处理还没做完

目前1K个数据的结果是无法收敛= =