Tensorflow学习笔记03-使用神经网络做线性回归
import tensorflow as tf
import numpy as np
#input就是输入数据,输入矩阵,in_size就是输入矩阵的列数(数据属性数量),out_size输出矩阵列数
#activation_function就是激活函数
def add_layer(input,in_size,out_size,activation_function):
Weight=tf.Variable(tf.random_normal([in_size,out_size]))
biases=tf.Variable(tf.zeros([1,out_size]))+0.1
Wx_plus_b=tf.matmul(input,Weight)+biases
if activation_function is None:
outputs=Wx_plus_b
else:
outputs=activation_function(Wx_plus_b)
return outputs #####create data#####
#x_data 300x1,np.newaxis 增加维度
x_data=np.linspace(-1,1,300)[:,np.newaxis]
#noise是均值为0,方差为0.05的高斯随机数
noise=np.random.normal(0,0.05,x_data.shape)
#noise就是数据中的干扰成分,噪点,数据是非线性变化的
y_data=np.square(x_data)-0.5+noise xs=tf.placeholder(tf.float32,[None,1])
ys=tf.placeholder(tf.float32,[None,1]) #隐藏层有十个神经元
l1=add_layer(xs,1,10,activation_function=tf.nn.relu)
predition=add_layer(l1,10,1,activation_function=None)
#计算代价函数,reduction_indices是指在哪一维度上进行求解
loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-predition),
reduction_indices=[1]))
#使用梯度下降的方法使loss达到最小
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss) init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init) for _ in range(1000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if _%50==0:
print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
#在练习神经网络时,对loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-predition),reduction_indices=[1]))
#不知道是如何得到的,便实现以下练习,清楚了计算细节
x=[[1],
[2],
[3]]
sess=tf.Session()
print(sess.run(tf.square(x)))
print(sess.run(tf.reduce_sum(tf.square(x), 1)))
print(sess.run(tf.reduce_mean(tf.reduce_sum(tf.square(x), 1))))
print(sess.run(tf.reduce_mean(x,0)))
输出结果:

说明0代表是对列进行求均值,求和操作,1代表对行进行求均值,求和操作
reduction_indices也是相同的作用,是为了和旧版本的兼容,最好不使用
Tensorflow学习笔记03-使用神经网络做线性回归的更多相关文章
- tensorflow学习笔记七----------卷积神经网络
卷积神经网络比神经网络稍微复杂一些,因为其多了一个卷积层(convolutional layer)和池化层(pooling layer). 使用mnist数据集,n个数据,每个数据的像素为28*28* ...
- Tensorflow学习笔记2019.01.03
tensorflow学习笔记: 3.2 Tensorflow中定义数据流图 张量知识矩阵的一个超集. 超集:如果一个集合S2中的每一个元素都在集合S1中,且集合S1中可能包含S2中没有的元素,则集合S ...
- TensorFlow学习笔记——LeNet-5(训练自己的数据集)
在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)
续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...
- 深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识
深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识 在tf第一个例子的时候需要很多预备知识. tf基本知识 香农熵 交叉熵代价函数cross-entropy 卷积神经网络 s ...
- tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)
tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...
- tensorflow学习笔记——自编码器及多层感知器
1,自编码器简介 传统机器学习任务很大程度上依赖于好的特征工程,比如对数值型,日期时间型,种类型等特征的提取.特征工程往往是非常耗时耗力的,在图像,语音和视频中提取到有效的特征就更难了,工程师必须在这 ...
- tensorflow学习笔记——VGGNet
2014年,牛津大学计算机视觉组(Visual Geometry Group)和 Google DeepMind 公司的研究员一起研发了新的深度卷积神经网络:VGGNet ,并取得了ILSVRC201 ...
- TensorFlow学习笔记10-卷积网络
卷积网络 卷积神经网络(Convolutional Neural Network,CNN)专门处理具有类似网格结构的数据的神经网络.如: 时间序列数据(在时间轴上有规律地采样形成的一维网格): 图像数 ...
随机推荐
- keycloak
keycloak报错, 少了配置项 keycloak.enabled=ture 找不到 publicKey, 1 ping不通 认证中心,2 网络不好
- 2019.03.24 Ajax
也还是会用到jquery中的Ajax请i求 所以第一还是引入jQuery包 可以去网上搜腾讯网静态资源库 然后引入包 Ajax还是异步处理 处理堵塞问题 import time 就可以了 ...
- 使用Emmet 快速生成HTML代码
在前端开发的过程中,一个最繁琐的工作就是写 HTML.CSS 代码.数量繁多的标签.属性.尖括号.标签闭合等,让前端们甚是苦恼.于是,我向大家推荐 Emmet,它提供了一套非常简单的语法规则,书写起来 ...
- [LeetCode] 704. Binary Search_Easy tag: Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- [LeetCode] 596. Classes More Than 5 Students_Easy tag:SQL
There is a table courses with columns: student and class Please list out all classes which have more ...
- Stacking调参总结
1. 回归 训练了两个回归器,GBDT和Xgboost,用这两个回归器做stacking 使用之前已经调好参的训练器 gbdt_nxf = GradientBoostingRegressor(lear ...
- jenkins 关联 钉钉机器人
注意:Jenkins URL配置中需要在最后添加"/",要不然会导致拼接的Url出错,这里填写有问题会导致无法从钉钉中跳转到Jenkins任务
- npm 安装React Devtools调试工具
有时候没有***工具时,怎么安装React DevTool, 其一直接搜索到Chrome的插件安装即可. 其二, 可以通过下载github上的react-devtools, 然后打包,最后导入chro ...
- OpenGL and Vulkan resources
OpenGL https://www.zhihu.com/question/22005157https://open.gl/https://github.com/cybercser/OpenGL_3_ ...
- 准备dbcp2-2.1.1和pool2-2.4.2 、commons-dbcp-1.4jar包
下载地址:https://pan.baidu.com/s/1gtcW36Lz6Yt-j9WlTu31Pw