Tensorflow入门练习(简单神经网络的训练过程)
2018-03-27 15:41
645 查看
import tensorflow as tf import os import numpy as np os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' BATCH_SIZE = 8 SEED = 23455 # 用种子产生随机数 rdm = np.random.RandomState(SEED) # 取出其中 32 * 2 的数据作为输入 X = rdm.rand(32,2) # Y的值为用以下逻辑生成 Y_ = [[int(x0 + x1< 1)] for (x0,x1) in X] print("X:\n",X) print("Y_:\n",Y_) x = tf.placeholder(tf.float32, shape=(None, 2)) y_ = tf.placeholder(tf.float32, shape=(None, 1)) w1 = tf.Variable(tf.random_normal([2,3], stddev=1, seed=1)) # 2 * 3 w2 = tf.Variable(tf.random_normal([3,1], stddev=1, seed=1)) # 3 * 1 a = tf.matmul(x, w1) y = tf.matmul(a, w2) # 定义损失函数及反向传播函数 loss_mse = tf.reduce_mean(tf.square(y-y_)) train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss_mse) # 开始训练 with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) # 输出目前(未经训练)的参数取值。 print("w1:\n", sess.run(w1)) print("w2:\n", sess.run(w2)) print("\n") # 训练模型。 STEPS = 3000 for i in range(STEPS): start = (i * BATCH_SIZE) % 32 # 总共有32组数据 end = start + BATCH_SIZE #每次用8个训练 sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]}) # 优化目标函数 if i % 500 == 0: # 每500次输出一次计算结果,输出经过500次迭代,目标函数优化到多少 total_loss = sess.run(loss_mse, feed_dict={x: X, y_: Y_}) print("After %d training step(s), loss_mse on all data is %g" % (i, total_loss)) # 输出训练后的参数取值。 print("\n") print("w1:\n", sess.run(w1)) print("w2:\n", sess.run(w2))
相关文章推荐
- tensorflow入门之训练简单的神经网络方法
- tensorflow入门之训练简单的神经网络
- tensorflow 学习笔记(四) - mnist实例--用简单的神经网络来训练和测试
- 训练神经网络的简单例子(TensorFlow平台下Python实现)
- [置顶] TensorFlow入门之简单实现神经网络的结构
- 机器学习算法练习之(二):Python和Tensorflow分别实现简单的神经网络
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试(MNIST For ML Beginners)
- TensorFlow神经网络的训练过程
- TensorFlow入门,基本介绍,基本概念,计算图,pip安装,helloworld示例,实现简单的神经网络
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试
- 利用TensorFlow训练简单的二分类神经网络模型的方法
- Tensorflow入门-简单神经网络进行MNIST数据集识别
- TensorFlow 入门之第一个神经网络训练 MNIST
- tensorflow学习笔记之简单的神经网络训练和测试
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试
- 利用TensorFlow训练简单的二分类神经网络模型
- (尤其是训练集验证集的生成)深度学习 tensorflow 实战(2) 实现简单神经网络以及随机梯度下降算法S.G.D
- TensorFlow入门深度学习--02.构建神经网络训练算法的步骤
- 神经网络基本原理-4.8神经网络之训练过程的性能监督
- 简单入门循环神经网络RNN:时间序列数据的首选神经网络