[TensorFlow学习手记] 5 - 建造神经网络
2017-11-24 11:54
429 查看
Code
import tensorflow as tf import matplotlib.pyplot as plt import numpy as np ''' Add layer 定义添加神经层的函数def add_layer(),它有四个参数:输入值、输入的大小、输出的大小和激励函数, 我们设定默认的激励函数是None ''' def add_layer(inputs, in_size, out_size, activation_function=None): Weights = tf.Variable(tf.random_normal([in_size,out_size])) # Normal distribution 生成随机变量矩阵 biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) # biases初始值不推荐为0 Wx_plus_b = tf.matmul(inputs,Weights) + biases if activation_function is None: outputs = Wx_plus_b else: outputs = activation_function(Wx_plus_b) return outputs x_data = np.linspace(-1,1,300)[:, np.newaxis] ''' np.newaxis的功能是插入新维度,可以看出np.newaxis分别是在行或列上增加维度, 原来是(10,)的数组,在行上增加维度变成(1,10)的二维数组,在列上增加维度变为(10,1)的二维数组 看下面的例子: In [1]: np.linspace(1, 10, 10) Out[1]: array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]) In [2]: np.linspace(1, 10, 10)[np.newaxis,:] Out[2]: array([[ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]]) In [3]: np.linspace(1, 10, 10)[:,np.newaxis] Out[3]: array([[ 1.], [ 2.], [ 3.], [ 4.], [ 5.], [ 6.], [ 7.], [ 8.], [ 9.], [ 10.]]) In [4]: np.linspace(1, 10, 10).shape Out[4]: (10,) In [5]: np.linspace(1, 10, 10)[np.newaxis,:].shape Out[5]: (1, 10) In [6]: np.linspace(1, 10, 10)[:,np.newaxis].shape Out[6]: (10, 1) ''' noise = np.random.normal(0,0.05,x_data.shape) # 增加噪声点 y_data = np.square(x_data) - 0.5 + noise # x^2 + 0.5 +noise # define placeholder for inputs to network xs = tf.placeholder(tf.float32, [None,1]) ys = tf.placeholder(tf.float32, [None,1]) ''' 利用占位符定义我们所需的神经网络的输入。 tf.placeholder()就是代表占位符, 这里的None代表无论输入有多少都可以,因为输入只有一个特征,所以这里是1。 ''' l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu) # 添加隐藏层 predition = add_layer(l1, 10, 1, activation_function=None) # 添加输出层 loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - predition), reduction_indices=[1])) ''' tf.reduce_*( input_tensor, 输入 axis=None, # 取哪一维度 keep_dims=False, # 保持维度 name=None, reduction_indices=None # 兼容 ) ''' train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) for i in range(1000): # training sess.run(train_step, feed_dict={xs: x_data, ys: y_data}) if i % 50 == 0: # to see the step improvement print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
Result
1.09957 0.00971825 0.00745628 0.00676008 0.00616613 0.00563508 0.00525119 0.004969 0.00473265 0.00450678 0.00431545 0.00412944 0.00399298 0.00388178 0.00376811 0.00367137 0.00359963 0.00353908 0.00348795 0.00344684
相关文章推荐
- Tensorflow之nn 简单神经网络学习
- Tensorflow学习教程------普通神经网络对mnist数据集分类
- TensorFlow 深度学习框架 (1)-- 神经网络与前向传播
- 基于Tensorflow学习神经网络- NN(神经网络)发展-分类
- TensorFlow 深度学习框架 (2)-- 反向传播优化神经网络
- TensorFlow简易学习[3]:实现神经网络
- TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络
- tensorflow学习笔记二——建立一个简单的神经网络拟合二次函数
- 深度学习——残差神经网络ResNet在分别在Keras和tensorflow框架下的应用案例
- 深度学习-传统神经网络使用TensorFlow框架实现MNIST手写数字识别
- 学习笔记TF052:卷积网络,神经网络发展,AlexNet的TensorFlow实现
- TensorFlow 深度学习框架 (1)-- 神经网络与前向传播
- 【深度学习_2.3_1】神经网络之TensorFlow初步应用
- 基于Tensorflow学习神经网络-CNN
- TensorFlow 深度学习框架 (2)-- 反向传播优化神经网络
- TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络
- tensorflow 学习笔记7 普通神经网络实现mnist手写识别
- TensorFlow 深度学习框架 (1)-- 神经网络与前向传播
- 基于Tensorflow学习神经网络-FCN
- TensorFlow 深度学习框架 (2)-- 反向传播优化神经网络