batch_size = n # 首先选取一小部分数据作为batch,此处定义batch的大小为n;
# 每次读取一小部分数据作为当前的训练数据来执行反向传播算法。
x = tf.placeholder(tf.float32, shape=(batch_size, 2), name='x-input')
y_ = tf.placeholder(tf.float32, shape=(batch_size, 1), name='y-input')
# 定义神经网络结构和优化算法。
loss = ……
train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
训练神经网络。
with tf.Session() as sess:
# 参数初始化。
……
#迭代的更新参数。
for i in range(STEPS):
# 准备batch_size个训练数据。一般将所有训练数据随机打乱后再选取可以得到更好的优化效果。
current_X, current_Y = ……
sess.run(train_step, feed_dict={x: current_X, y_: current_Y})
本文介绍如何利用TensorFlow构建神经网络,并通过小批量梯度下降法进行训练。主要内容包括:定义占位符以接收输入数据,设置神经网络结构及损失函数,采用Adam优化器最小化损失,以及在训练过程中不断更新参数。
1万+

被折叠的 条评论
为什么被折叠?



