代码:
import tensorflow as tf
import numpy as np
### 定义添加神经网络层函数 START ### def add_layer(inputs,in_size,out_size,activation_function=None):
"""描述: 添加神经网络层函数.
:param inputs: 输入神经层
:param in_size: 输入神经层的神经元个数
:param out_size: 输出神经层的神经元个数
:param activation_function: 激励函数
""" # 定义一个"in_size行,out_size列"的随机矩阵变量
Weights=tf.Variable(tf.random_normal([in_size,out_size])) # 定义一个"1行,out_size列"的0值矩阵基准变量
biases=tf.Variable(tf.zeros([1,out_size])+0.1) # 定义一个矩阵乘法函数公式
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
### 定义添加神经网络层函数 END ### ### 定义变量结构 START### # 定义起始输入:在指定的-1到1的间隔内返回300个均匀间隔的1行300列的数组,再将数组转化为1列300行的矩阵
# 例如:
# x1 = np.array([1, 2, 3, 4, 5])
# # the shape of x1 is (5,)
# x1_new = x1[:, np.newaxis]
# # now, the shape of x1_new is (5, 1)
# array([[1],
# [2],
# [3],
# [4],
# [5]])
# x1_new = x1[np.newaxis,:]
# # now, the shape of x1_new is (1, 5)
# array([[1, 2, 3, 4, 5]]) x_data=np.linspace(-1,1,300)[:,np.newaxis] # 定义噪点 :使用高斯分布的概率密度函数定义一个均值为0,标准差为0.05的高斯随机数,个数为x_data的矩阵元素数
noise =np.random.normal(0,0.05,x_data.shape) # 定义起始输出:x_data的平方减去0.5,再加上噪点
y_data=np.square(x_data)-0.5+noise # 定义运行时参数变量
xs=tf.placeholder(tf.float32,[None,1])
ys=tf.placeholder(tf.float32,[None,1]) ### 定义神经网络结构 START### # 定义隐藏层神经网络层layer01
layer01=add_layer(xs,1,10,activation_function=tf.nn.relu)
# 定义隐藏层神经网络层layer02
layer02=add_layer(layer01,10,10,activation_function=tf.nn.sigmoid)
# 定义预测输出层 prediction
prediction =add_layer(layer02,10,1,activation_function=None)
# 计算损失
# 1.计算起始输出与预测输出的偏差的平方
loss_square=tf.square(y_data - prediction)
# 2.计算一个张量的各个维度上元素的总和.
reduce_sum_square=tf.reduce_sum(loss_square,reduction_indices=[1])
# 3.计算损失:张量的各个维度上的元素的平均值
loss=tf.reduce_mean(reduce_sum_square) #使用梯度下降算法训练所有样本
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# 定义初始化变量
init=tf.initialize_all_variables()
# 创建会话
sess=tf.Session()
# 运行初始化变量指针
sess.run(init) ### 定义神经网络结构 END### ###定义变量结构 END### for i in range(2000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if i%50==0:
print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
输出结果:
> Executing task: python d:\Work\002_WorkSpace\VSCode\Tensorflow\cnn.py <

WARNING:tensorflow:From C:\Program Files\Python\Python37\lib\site-packages\tensorflow\python\framework
\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From C:\Program Files\Python\Python37\lib\site-packages\tensorflow\python\ops
\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
WARNING:tensorflow:From C:\Program Files\Python\Python37\lib\site-packages\tensorflow\python\util
\tf_should_use.py:193: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
2019-06-16 18:23:25.445771: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports
instructions that this TensorFlow binary was not compiled to use: AVX2
0.9150444
0.018474927
0.012227052
0.008430008
0.006330067
0.005174632
0.0045147026
0.004099458
0.0037936615
0.0035521714
0.0033668855
0.003235288
0.0031796119
0.003798308
0.011472862
0.011122204
0.0038715526
0.0029777498
0.00284954
0.0028072707
0.0027813027
0.0027617016
0.0027467846
0.0027342557
0.0027231644
0.0027126905
0.0027037202
0.0026956936
0.0026887206
0.0026827992
0.0026773391
0.0026706234
0.0026643125
0.0026575066
0.0026512532
0.00264405
0.0026368005
0.0026302505
0.0026243015
0.0026188325 Terminal will be reused by tasks, press any key to close it.

[Tensorflow]激励函数tf.nn.relu样例的更多相关文章

  1. tf.nn.relu

    tf.nn.relu(features, name = None) 这个函数的作用是计算激活函数 relu,即 max(features, 0).即将矩阵中每行的非最大值置0. import tens ...

  2. 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法

    在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化 ...

  3. 【TensorFlow】tf.nn.max_pool实现池化操作

    max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考[TensorFlow]tf.nn.conv2d是怎样实现卷积的? tf.nn.max_pool(va ...

  4. tensorflow 之tf.nn.depthwise_conv2d and separable_conv2d实现及原理

    Depthwise Separable Convolution 1.简介 Depthwise Separable Convolution 是谷歌公司于2017年的CVPR中在论文”Xception: ...

  5. 【Tensorflow】tf.nn.depthwise_conv2d如何实现深度卷积?

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/mao_xiao_feng/article/ ...

  6. 【Tensorflow】tf.nn.atrous_conv2d如何实现空洞卷积?膨胀卷积

    介绍关于空洞卷积的理论可以查看以下链接,这里我们不详细讲理论: 1.Long J, Shelhamer E, Darrell T, et al. Fully convolutional network ...

  7. tf.nn.relu 激活函数

    tf.nn.relu(features, name = None) 计算校正线性:max(features, 0) 参数: features:一个Tensor.必须是下列类型之一:float32,fl ...

  8. TensorFlow学习---tf.nn.dropout防止过拟合

    一. Dropout原理简述: tf.nn.dropout是TensorFlow里面为了防止或减轻过拟合而使用的函数,它一般用在全连接层. Dropout就是在不同的训练过程中随机扔掉一部分神经元.也 ...

  9. TensorFlow:tf.nn.max_pool实现池化操作

    tf.nn.max_pool(value, ksize, strides, padding, name=None) 参数是四个,和卷积很类似: 第一个参数value:需要池化的输入,一般池化层接在卷积 ...

随机推荐

  1. Linux中 mkdir 创建文件夹命令

    语法 mkdir (选项)(参数) 选项 -Z:设置安全上下文,当使用SELinux时有效: -m<目标属性>或--mode<目标属性>建立目录的同时设置目录的权限: -p或- ...

  2. 请解释一下 JavaScript 的同源策略

    概念: 同源策略是客户端脚本(尤其是Netscape Navigator2.0,其目的是防止某个文档或脚本从多个不同源装载. 这里的同源策略指的是:协议,域名,端口相同,同源策略是一种安全协议. 指一 ...

  3. jar启动脚本shell

    #!/bin/bash#这里可替换为你自己的执行程序,其他代码无需更改 APP_NAME=/opt/server/msp/health-api/health-2.0.2.jar#使用说明,用来提示输入 ...

  4. 算法习题---4.3救济金发放(UVa133)

    一:题目 (n< )个人站成一圈,逆时针编号为1~n.有两个官员,A从1开始逆时针数,B从n开始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个官员停在同一个人上 ...

  5. 简介Python中用于处理字符串的center()方法

    简介Python中用于处理字符串的center()方法 这篇文章主要介绍了简介Python中用于处理字符串的center()方法,是Python入门中的基础知识,需要的朋友可以参考下 center() ...

  6. 简单的django登录项目---带views视图函数(脚本文件)---用Bootstrap

    简单的django登录项目 1.首先建立工程,建立工程请参照:https://www.cnblogs.com/effortsing/p/10394511.html 2.在Firstdjango工程项目 ...

  7. es6 关于map和for of的区别有哪些?

    1.es6  关于map和for of的区别有哪些? ——主要想了解一下性能方面的

  8. 基于Visual Studio Code搭建Vue开发环境

    安装node.js最新版 这里安装的是8.11.4版   image.png 更新npm至最新版 安装node.js后, npm默认版本为: 6.1.0   image.png 使用npm insta ...

  9. Flutter Resolving dependencies...很慢解决办法

    昨天打开AS,看到提示更新,就手贱的点了,结果更新到3.5版本后flutter的一些插件用不了了... 没办法,只能卸载,重新装回以前的版本,什么SDK.AVD的都重新装了 全都装好后,重新运行项目的 ...

  10. 域名购买、SSL证书申请使用和本地服务映射外网

    万网购买域名 1.在如下网址购买,价格不是很贵,几块钱一年的都有:https://wanwang.aliyun.com/domain/?spm=5176.100251.111252.24.4ddd4f ...