import tensorflow as tf
import numpy as np
#定义训练次数
training_steps = 3000
#定义输入的数据和对应标签在for循环中进行填充
data = []
label = []
for i in range(200):
x1 = np.random.uniform(-1, 1)
x2 = np.random.uniform(0, 2)
data.append([np.random.normal(x1, 0.1), np.random.normal(x2, 0.1)])
if x1**2 + x2**2 <= 1:
label.append(0)
else:
label.append(1)
data = np.hstack(data).reshape(-1,2)
label = np.hstack(label).reshape(-1,1)
#定义前向传播的隐层
def hidden_layer(input_tensor, weight1, bias1, weight2, bias2, weight3, bias3):
layer1 = tf.nn.relu(tf.matmul(input_tensor, weight1)+bias1)
layer2 = tf.nn.relu(tf.matmul(layer1, weight2)+bias2)
output = tf.matmul(layer2, weight3)+bias3
return output
x = tf.placeholder(tf.float32, shape=(None, 2), name = 'x-input')
y_=tf.placeholder(tf.float32,shape=(None, 1), name = 'y-output')
#定义权重和偏置项
weight1 = tf.Variable(tf.truncated_normal([2,10], stddev = 0.1))
bias1 = tf.Variable(tf.constant(0.1, shape=[10]))
weight2 = tf.Variable(tf.truncated_normal([10,10], stddev = 0.1))
bias2 = tf.Variable(tf.constant(0.1, shape=[10]))
weight3 = tf.Variable(tf.truncated_normal([10, 1], stddev = 0.1))
bias3 = tf.Variable(tf.constant(0.1, shape = [1]))
#用len()函数计算data数据长度
sample_size = len(data)
#计算前向传播结果
y = hidden_layer(x,weight1, bias1, weight2, bias2, weight3, bias3)
# 计算均方误差损失
error_loss = tf.reduce_sum(tf.pow(y_-y,2))/sample_size
# 将均方误差加入losses集合
tf.add_to_collection('losses', error_loss)
# 实现权重的L2正则化
regularizer = tf.contrib.layers.l2_regularizer(0.01)
regularization = regularizer(weight1) + regularizer(weight2) + regularizer(weight3)
tf.add_to_collection('losses', regularization)
loss = tf.add_n(tf.get_collection('losses'))
train_op = tf.train.AdamOptimizer(0.01).minimize(loss)
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(training_steps):
sess.run(train_op, feed_dict={x:data, y_:label})
if i%20 == 0:
loss_value = sess.run(loss, feed_dict={x:data, y_:label})
print("after %d steps, mse_loss:%f" %(i, loss_value))