TensorFlow实现简易的全连接网络模型

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))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值