import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings(‘ignore’)
tf.set_random_seed(777)
xdata = [[0, 0], [0, 1], [1, 0], [1, 1]]
ydata = [[0], [1], [1], [0]]
占位符
x = tf.placeholder(tf.float32, [None, 2])
y = tf.placeholder(tf.float32, [None, 1])
前向传播
w1 = tf.Variable(tf.random_normal([2, 5]), name=‘w1’)
b1 = tf.Variable(tf.random_normal([5]), name=‘b1’)
w2 = tf.Variable(tf.random_normal([5, 1]), name=‘w2’)
b2 = tf.Variable(tf.random_normal([1]), name=‘b2’)
z1 = tf.matmul(x, w1)+b1
a1 = tf.tanh(z1)
z2 = tf.matmul(a1, w2)+b2
a2 = tf.sigmoid(z2)
代价函数
cost = -tf.reduce_mean(y*tf.log(a2)+(1-y)*tf.log(1-a2))
反向传播
dz2 = a2-y