AI-Dog

爱生活,乐分享,大家好才是真的好!

导航

神经网络解决二值分类问题的完整程序

import tensorflow as tf
# NumPy是一个科学计算工具包,这里通过numpy工具包生成模拟数据集
from numpy.random import RandomState


# 定义一批训练数据batch大小
batch_size = 8
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

# 在shape的一个维度上使用None可以方便使用不同batch的大小。
# 在训练时将数据分成比较小的batch,但在测试时,可以一次性使用所有数据。
# 当数据比较小时这样比较方便测试数据,但当数据量大时,大的batch会导致内存溢出
x = tf.placeholder(tf.float32, shape=(None, 2), name="x-input")
y_ = tf.placeholder(tf.float32, shape=(None, 1), name="y-input")

# 定义前项传播的过程
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)

# 定义损失函数和和反向传播算法
y = tf.sigmoid(y)
# 定义损失函数,刻画预测值与真实值之间的差距
cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)) +
                                (1 - y_) * tf.log(tf.clip_by_value(1-y, 1e-10, 1.0)))

train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)

# 通过随机数生成一个模拟数据集
rdm = RandomState(1)
dataset_size = 128
X = rdm.rand(dataset_size, 2)

# 定义规则来给出样本标签,在这里x1 + x2 < 1被认为正样本
# 而其他为负样本。和TensorFlow游乐场不同,这里使用0代表负样本,1代表正
# 大部分解决分类问题的神经网络都会采用0,1表示
Y = [[int(x1 + x2 < 1)] for (x1, x2) in X]

# 定义会话
with tf.Session() as sess:
    # 定义初始化变量
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    print(sess.run(w1))
    print(sess.run(w2))

    # 设定训练轮数
    STEPS = 10000
    for i in range(STEPS):
        # 每次选取batch_size个样本进行训练
        start = (i * batch_size) % dataset_size
        end = min(start + batch_size, dataset_size)

        # 通过选取的样本训练神经网络并更新参数
        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
        if i % 1000 == 0:
            # 每隔一段时间计算所有数据上的交叉熵并输出
            total_cross_entropy = sess.run(cross_entropy, feed_dict={x: X, y_: Y})
            print("After %d training step(s), cross entropy on all data is %g" % (i, total_cross_entropy))
    print(sess.run(w1))
    print(sess.run(w2))



"""
输出:
[[-0.8113182   1.4845988   0.06532937]
 [-2.4427042   0.0992484   0.5912243 ]]
[[-0.8113182 ]
 [ 1.4845988 ]
 [ 0.06532937]]
After 0 training step(s), cross entropy on all data is 1.89805
After 1000 training step(s), cross entropy on all data is 0.655075
After 2000 training step(s), cross entropy on all data is 0.626172
After 3000 training step(s), cross entropy on all data is 0.615096
After 4000 training step(s), cross entropy on all data is 0.610309
After 5000 training step(s), cross entropy on all data is 0.608679
After 6000 training step(s), cross entropy on all data is 0.608231
After 7000 training step(s), cross entropy on all data is 0.608114
After 8000 training step(s), cross entropy on all data is 0.608088
After 9000 training step(s), cross entropy on all data is 0.608081
[[ 0.08782727  0.51795506  1.7529843 ]
 [-2.2372198  -0.20525953  1.0744455 ]]
[[-0.49522772]
 [ 0.40552336]
 [-1.0061253 ]]

"""

 

posted on 2019-03-11 20:27  AI-Dog  阅读(1921)  评论(0编辑  收藏  举报