TensorFlow ---XOR异或神经网络

异或问题:相同为0 不同为1

import tensorflow as tf
import matplotlib.pyplot as plt

# 定义数据集
x_data = [[0, 0],
          [0, 1],
          [1, 0],
          [1, 1]]
y_data = [[0],
          [1],
          [1],
          [0]]

tf.set_random_seed(1)
learning_rate = 0.1

# 占位符
X, Y = tf.placeholder('float', shape=[None, 2]), tf.placeholder('float', [None, 1])

# 初始化权重和偏置  FP
W1, b1 = tf.Variable(tf.random_normal([2, 3]), dtype=tf.float32), tf.Variable(tf.random_normal([3]), dtype=tf.float32)
W2, b2 = tf.Variable(tf.random_normal([3, 1]), dtype=tf.float32), tf.Variable(tf.random_normal([1]), dtype=tf.float32)
a1 = tf.sigmoid(tf.matmul(X, W1) + b1)
a2 = tf.sigmoid(tf.matmul(a1, W2) + b2)

# 代价函数  交叉熵
cost = -tf.reduce_mean(Y * tf.log(a2) + (1 - Y) * tf.log(1 - a2))
J = []

# BP
m = tf.cast(tf.shape(X)[0], dtype=tf.float32)
dz2 = a2 - Y
dw2 = 1 / m * tf.matmul(tf.transpose(a1), dz2)
db2 = tf.reduce_mean(dz2, axis=0)

da1 = tf.matmul(dz2, tf.transpose(W2))
dz1 = da1 * a1 * (1 - a1)
dw1 = 1 / m * tf.matmul(tf.transpose(X), dz1)
db1 = tf.reduce_mean(dz1, axis=0)

# 参数更新
BP_update = [
    tf.assign(W2, W2 - learning_rate * dw2),
    tf.assign(b2, b2 - learning_rate * db2),
    tf.assign(W1, W1 - learning_rate * dw1),
    tf.assign(b1, b1 - learning_rate * db1)
]

# # 梯度下降优化器
# train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)

# 准确率计算
predicted = tf.cast(a2 > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for step in range(4001):
        _, cost_, accuracy_ = sess.run([BP_update, cost, accuracy], feed_dict={X: x_data, Y: y_data})
        # _, cost_, accuracy_ = sess.run([train, cost, accuracy], feed_dict={X: x_data, Y: y_data})
        if step % 100 == 0:
            print(step, 'Cost: ', cost_, 'accuracy: ', accuracy_)
            J.append(cost_)

    h, p, a = sess.run([a2, predicted, accuracy], feed_dict={X: x_data, Y: y_data})
    print(f'h_pre:{h}\nh:{p}\nacc:{a}')

    plt.plot(J)
    plt.show()

你可能感兴趣的:(深度学习,Tensorflow)