Tensorflow实战1:最简单神经网络实现
2018-01-06 00:00
633 查看
摘要: 代码地址:https://github.com/juxiangwu/deeplearning-notes
# -*- coding: utf-8 -*- ''' 拟合f(x) = x^2 -0.5 ''' import numpy as np import tensorflow as tf # 构造函数数据 x_data = np.linspace(-1,1,300)[:,np.newaxis] # 噪声数据 nose_data = np.random.normal(0,0.05,x_data.shape) # 构造函数 y_data = np.square(x_data) - 0.5 + nose_data # 定义神经网络数据 xs = tf.placeholder(tf.float32,[None,1]) ys = tf.placeholder(tf.float32,[None,1]) # 构建神经网络 # 每一层都经过向量化处理 y = weight * x + bias def add_layer(inputs,in_size,out_size,activation_func=None): # 构建权重weights = in_size * out_size 矩阵 weights = tf.Variable(tf.random_normal([in_size,out_size])) # 构建 biases = 1*out_size矩阵 biases = tf.Variable(tf.random_normal([1,out_size]) + 0.1) # 矩阵相乘 Wx_plus_b = tf.matmul(inputs,weights) + biases if activation_func == None: return Wx_plus_b else: outputs = activation_func(Wx_plus_b) return outputs # 构建隐藏层 h1 = add_layer(xs,1,20,activation_func=tf.nn.relu) # 构建输出层 pred = add_layer(h1,20,1,activation_func=None) # 构建损失函数 loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - pred),reduction_indices=[1])) # 构建优化器 optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss) # 训练神经网络 init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) for i in range(1000): sess.run(optimizer,feed_dict={xs:x_data,ys:y_data}) if i % 10 == 0: output = sess.run(loss,feed_dict={xs:x_data,ys:y_data}) print(output) print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
相关文章推荐
- TensorFlow:实战Google深度学习框架(二)实现简单神经网络
- [action] deep learning 深度学习 tensorflow 实战(2) 实现简单神经网络以及随机梯度下降算法S.G.D
- (尤其是训练集验证集的生成)深度学习 tensorflow 实战(2) 实现简单神经网络以及随机梯度下降算法S.G.D
- 利用tensorflow 一步一步实现一个简单神经网络,线性回归
- 训练神经网络的简单例子(TensorFlow平台下Python实现)
- 利用tensorflow实现简单的神经网络
- 学习笔记TF055:TensorFlow神经网络简单实现一元二次函数
- TensorFlow实战——实现神经网络
- 简单神经网络TensorFlow实现
- [置顶] TensorFlow入门之简单实现神经网络的结构
- tensorflow56 《TensorFlow技术解析与实战》06 神经网络的发展及其Tensorflow实现
- Tensorflow实战学习(二十八)【实现简单卷积网络】
- 使用循环神经网络实现语言模型——源自《TensorFlow:实战Goole深度学习框架》
- TensorFlow入门,基本介绍,基本概念,计算图,pip安装,helloworld示例,实现简单的神经网络
- Tensorflow简单神经网络实现
- 使用tensorflow实现全连接神经网络的简单示例,含源码
- TensorFlow技术解析与实战 6 神经网络的发展及其 TensorFlow 实现
- Tensorflow实战Google-第三章-简单神经网络
- 学习笔记TF055:TensorFlow神经网络简单实现一元二次函数
- 机器学习算法练习之(二):Python和Tensorflow分别实现简单的神经网络