# TensorFlow | 自己动手写深度学习模型之全连接神经网络

1. 让大家了解Tensorflow 的基本使用方法；
2. 使用 tensorboard 可视化你的神经网络结构和变量的更新情况；
3. 断点保存模型，可以在训练意外中断之后再次运行时接着中断之前的状态继续训练；
4. 展示全连接神经网络是不是真的可以拟合任意函数，拟合效果怎样。

## 加载数据

def load_data(noise=0.1):
from sklearn.datasets import make_moons
m = 2000
X_moons, y_moons = make_moons(m, noise=noise, random_state=42)
return X_moons, y_moons

## 随机 batch

def random_batch(X_train, y_train, batch_size):
rnd_indices = np.random.randint(0, len(X_train), batch_size)
X_batch = X_train[rnd_indices]
y_batch = y_train[rnd_indices]
return X_batch, y_batch

## 划分训练集和测试集

test_ratio = 0.2
test_size = int(len(data) * test_ratio)
X_train = data[:-test_size]
X_test = data[-test_size:]
y_train = label[:-test_size]
y_test = label[-test_size:]

## 建立全连接神经网络层

# regularizer = tf.contrib.layers.l2_regularizer(lambd)def fc_layers(input_tensor,regularizer):
HINDENN1 = 6
HINDENN2 = 4
with tf.name_scope("full-connect-layer"):
fc1 = tf.layers.dense(input_tensor, HINDENN1, activation=tf.nn.elu,\
kernel_regularizer=regularizer, name="fc1")
fc2 = tf.layers.dense(fc1, HINDENN2, activation=tf.nn.elu,\
kernel_regularizer=regularizer, name="fc2")
return fc2

## 搭积木一样搭建整个模型的结构

n_inputs = X_train.shape[1]
n_outputs = len(set(y_train))
with tf.name_scope("input"):
X = tf.placeholder(tf.float32, shape=(None, n_inputs), name="X")
y = tf.placeholder(tf.int32, shape=(None), name="y")

regularizer = tf.contrib.layers.l2_regularizer(lambd)
fc2 = fc_layers(X,regularizer)
with tf.name_scope("output"):
logits = tf.layers.dense(fc2, n_outputs, kernel_regularizer=regularizer,name="output")

with tf.name_scope('loss'):
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y, logits= logits)
loss = tf.reduce_mean(xentropy, name = 'loss')
loss_summary = tf.summary.scalar('loss', loss)

global_step = tf.Variable(0, trainable = False)
with tf.name_scope('train'):

with tf.name_scope('eval'):
predictions = tf.argmax(logits, 1)
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
acc_summary = tf.summary.scalar('acc', accuracy)

summary_op = tf.summary.merge([loss_summary, acc_summary])

## 定义模型和变量的保存地址

checkpoint_path = "./chickpoints/model.ckpt"
checkpoint_epoch_path = checkpoint_path + ".epoch"
final_model_path = "./chickpoints/model"

now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
logdir = './logs/'+ now
file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())
saver = tf.train.Saver()

## 训练和验证

n_epochs = n_epochsbatch_size = batch_sizen_batches = int(np.ceil(len(data) / batch_size))with tf.Session() as sess:
init = tf.global_variables_initializer()

if os.path.isfile(checkpoint_epoch_path):
# if the checkpoint file exists, restore the model and load the epoch number
with open(checkpoint_epoch_path, "rb") as f:
print("Training was interrupted. Continuing at epoch", start_epoch)
saver.restore(sess, checkpoint_path)
else:
start_epoch = 0
sess.run(init)

for epoch in range(start_epoch, n_epochs):
for batch_index in range(n_batches):
X_batch, y_batch = random_batch(X_train, y_train, batch_size)
sess.run(train_op, feed_dict={X: X_batch, y: y_batch})
loss_val, summary_str,test_pred, test_acc = sess.run(
[loss, summary_op,predictions, accuracy],\
feed_dict={X: X_test, y: y_test})

if epoch % 50 == 0:
print("Epoch:", epoch, "\tLoss:", loss_val,"\tAcc:",test_acc)
saver.save(sess, checkpoint_path)
with open(checkpoint_epoch_path, "wb") as f:
f.write(b"%d" % (epoch + 1))

saver.save(sess, final_model_path)
y_pred = predictions.eval(feed_dict={X: X_test, y: y_test})
print('precision_score',precision_score(y_test, y_pred))
print('recall_score',recall_score(y_test, y_pred))

sess.close()

if os.path.isfile(checkpoint_epoch_path) 语句检查是否存在上次运行程序所保存的模型，如果存在，直接加载，然后从中断的位置继续跑程序rugosa不存在，则从头开始跑。sess.run(train_op, feed_dict={X: X_batch, y: y_batch}) 语句表示训练过程，所有训练过程包括前向过程、反向传播过程/梯度下降过程、计算损失等都包括在前文的 train_op 中了；这里代码没有打印训练时的实时 损失和正确率情况，如果你跑大数据集，你可以在这一段加一些语句，实时打印模型的情况。每一个 epoch 我们将训练的模型在测试集上进行验证准确率等，loss_val, summary_str,test_pred, test_acc = sess.run(

[loss, summary_op,predictions, accuracy],\ feed_dict={X: X_test, y: y_test})，并且将要保存的日志信息写入文件 file_writer.add_summary(summary_str, epoch)。每 50 epoch 打印测试集上的损失和正确率 print("Epoch:", epoch, "\tLoss:", loss_val,"\tAcc:",test_acc) ，并且保存临时模型。在模型跑完之后，我们对测试集进行预测 y_pred = predictions.eval(feed_dict={X: X_test, y: y_test})，得到预测结果之后计算测试集的精确率和找回率，打印输出。

noise=0.2 Training was interrupted. Continuing at epoch 4951 precision_score 0.9681818181818181 recall_score 0.9770642201834863 noise=0.3 Training was interrupted. Continuing at epoch 4951 precision_score 0.9339622641509434 recall_score 0.908256880733945

Training was interrupted. Continuing at epoch 4951 precision_score 1.0 recall_score 1.0 noise=0.3 Training was interrupted. Continuing at epoch 4951 precision_score 0.9308755760368663 recall_score 0.926605504587156

https://github.com/wangle1218/Depp-learning-TensorFlow-tutorials/blob/master/fc_clf.pygithub.com

## 参考资料

1. Aurélien Géron，《 Hands-On Machine Learning with Scikit-Learn and TensoFlow》

0 条评论

## 相关文章

83320

83470

430110

18620

35740

25080

### 详解 BP 神经网络基本原理及 C 语言实现

BP（Back Propagation）即反向传播，指的是一种按照误差反向传播来训练神经网络的方法。而 BP 神经网络即为一种按照误差反向传播的方法训练的神经网...

56040

### 支持向量机原理(四)SMO算法原理

在SVM的前三篇里，我们优化的目标函数最终都是一个关于$\alpha$向量的函数。而怎么极小化这个函数，求出对应的$\alpha$向量，进而求出分离超平面我...

11920

61090

21020