tensorflow学习:建立一个最简单的神经网络
2017-07-14 15:05
751 查看
程序功能:使用y= x^2 - 0.5加噪声生成1000个<x, y>样本点,然后搭建一个最简单的神经网络学习
import tensorflow as tf
import numpy as np
def add_layer(input_data, input_size, output_size, activation_function = None):
'''
input_data:输入数据
input_size:输入的神经元个数
output_size:输出的神经元个数
activation_function:激励函数
'''
#产生input_size行output_size列的随机数
Weights = tf.Variable(tf.random_normal([input_size, output_size]))
#产生一行output_size列全为0.1的数
biases = tf.Variable(tf.zeros([1, output_size]) + 0.1)
#input_data * weights + biases
Wx_plus_b = tf.matmul(input_data, Weights) + biases
if activation_function is None:
output = Wx_plus_b
else:
output = activation_function(Wx_plus_b)
return output
#在-1到1之间均匀产生1000个数的list,并将其转成列向量
x_data = np.linspace(-1, 1, 1000)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
#隐藏层输出10个神经单元,现在还不知道该怎么设置这个神经单元数比较好
layer1 = add_layer(xs, 1, 10, tf.nn.relu)
prediction = add_layer(layer1, 10, 1, None)
#add_layer函数不需要自己实现,直接调下面函数也能达到相同的效果
# layer1_new = tf.layers.dense(xs, 10, tf.nn.relu)
# prediction_new = tf.layers.dense(layer1_new, 1)
#这两个loss到底有什么区别
loss = tf.reduce_mean(tf.square(ys - prediction))
# loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for step in range(1001):
#这个地方是用所有的数据进行梯度下降,这样做不合适
sess.run(train_step, feed_dict = {xs: x_data, ys: y_data})
if step % 50 == 0:
print(step, sess.run
958b
(loss, feed_dict = {xs: x_data, ys:y_data}))
import tensorflow as tf
import numpy as np
def add_layer(input_data, input_size, output_size, activation_function = None):
'''
input_data:输入数据
input_size:输入的神经元个数
output_size:输出的神经元个数
activation_function:激励函数
'''
#产生input_size行output_size列的随机数
Weights = tf.Variable(tf.random_normal([input_size, output_size]))
#产生一行output_size列全为0.1的数
biases = tf.Variable(tf.zeros([1, output_size]) + 0.1)
#input_data * weights + biases
Wx_plus_b = tf.matmul(input_data, Weights) + biases
if activation_function is None:
output = Wx_plus_b
else:
output = activation_function(Wx_plus_b)
return output
#在-1到1之间均匀产生1000个数的list,并将其转成列向量
x_data = np.linspace(-1, 1, 1000)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
#隐藏层输出10个神经单元,现在还不知道该怎么设置这个神经单元数比较好
layer1 = add_layer(xs, 1, 10, tf.nn.relu)
prediction = add_layer(layer1, 10, 1, None)
#add_layer函数不需要自己实现,直接调下面函数也能达到相同的效果
# layer1_new = tf.layers.dense(xs, 10, tf.nn.relu)
# prediction_new = tf.layers.dense(layer1_new, 1)
#这两个loss到底有什么区别
loss = tf.reduce_mean(tf.square(ys - prediction))
# loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for step in range(1001):
#这个地方是用所有的数据进行梯度下降,这样做不合适
sess.run(train_step, feed_dict = {xs: x_data, ys: y_data})
if step % 50 == 0:
print(step, sess.run
958b
(loss, feed_dict = {xs: x_data, ys:y_data}))
相关文章推荐
- tensorflow学习笔记二——建立一个简单的神经网络拟合二次函数
- Tensorflow 入门学习3 Hello TensorFlow 建立一个隐藏层的神经网络实现回归分析
- tensorflow建立一个简单的神经网络的方法
- tensorflow建立一个简单的神经网络
- TensorFlow:一个简单的神经网络
- Tensorflow构建一个简单的神经网络
- TensorFlow:实战Google深度学习框架(二)实现简单神经网络
- 神经网络与深度学习 1.3 神经网络的架构 1.4 一个简单的分类手写数字的网络架构
- Tensorflow学习:单层神经网络的建立
- tensorflow笔记(二)之构造一个简单的神经网络
- 用tensorflow搭建一个简单的神经网络
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试
- tensorflow 学习笔记(四) - mnist实例--用简单的神经网络来训练和测试
- tensorflow学习笔记之简单的神经网络训练和测试
- 学习笔记TF055:TensorFlow神经网络简单实现一元二次函数
- (尤其是训练集验证集的生成)深度学习 tensorflow 实战(2) 实现简单神经网络以及随机梯度下降算法S.G.D
- Tensorflow 构造一个简单的神经网络
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试
- 神经网络梯度消失的解释 发表于2016/10/6 11:08:30 10609人阅读 分类: 深度学习 转载自哈工大SCIR(公众号) 为了弄清楚为何会出现消失的梯度,来看看一个极简单的深度
- tensorflow 学习笔记11 最简单的循环神经网络(RNN)