tensorflow 实现自定义layer并添加到计算图中


Posted in Python onFebruary 04, 2020

目的

将用户自定义的layer结合tensorflow自带的layer组成多层layer的计算图。

实现功能

对2D图像进行滑动窗口平均,并通过自定义的操作layer返回结果。

import tensorflow as tf
import numpy as np
sess = tf.Session()

#将size设为[1, 4, 4, 1]是因为tf中图像函数是处理四维图片的。
#这四维依次是: 图片数量,高度, 宽度, 颜色通道
x_shape = [1,4,4,1]
x_val = np.random.uniform(size = x_shape)


#tf.nn.conv2d中name表明该layer命名为“Moving_Avg_Window”
#该卷积核为[[0.25,0.25],[0.25,0.25]],所以是一个求平均操作
x_data = tf.placeholder(tf.float32, shape = x_shape)
my_filter = tf.constant(0.25, shape = [2,2,1,1])
my_strides = [1,2,2,1]
mov_avg_layer = tf.nn.conv2d(x_data, my_filter, my_strides, padding = 'SAME', name = 'Moving_Avg_Window')


#自定义layer,对卷积操作之后的输出做操作
def custom_layer(input_matrix):
  input_matrix_sqeeze = tf.squeeze(input_matrix)
  A = tf.constant([1.,2.],[-1.,3.])
  b = tf.constant(1., shape = [2,2])
  temp1 = tf.matmul(A, input_matrix_sqeeze)
  temp2 = tf.add(temp1, b)
  return(tf.sigmod(temp2))
#把刚刚自定义的layer加入到计算图中,并给予自定义的命名(利用tf.name_scope())
with tf.name_scope('Custom_Layer') as scope:
  custom_layer1 = custom_layer(mov_avg_layer)


#为占位符传入4*4图片,并执行计算图
print(sess.run(custom_layer, feed_dict= {x_data: x_val}))

以上这篇tensorflow 实现自定义layer并添加到计算图中就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python异常处理总结
Aug 15 Python
线程和进程的区别及Python代码实例
Feb 04 Python
Python实现excel转sqlite的方法
Jul 17 Python
Python读写及备份oracle数据库操作示例
May 17 Python
Python 中Django验证码功能的实现代码
Jun 20 Python
利用pyshp包给shapefile文件添加字段的实例
Dec 06 Python
Python常用编译器原理及特点解析
Mar 23 Python
Python Socketserver实现FTP文件上传下载代码实例
Mar 27 Python
python将音频进行变速的操作方法
Apr 08 Python
如何使用python切换hosts文件
Apr 29 Python
Windows 下更改 jupyterlab 默认启动位置的教程详解
May 18 Python
简单且有用的Python数据分析和机器学习代码
Jul 02 Python
TensorFlow实现自定义Op方式
Feb 04 #Python
tensorflow使用指定gpu的方法
Feb 04 #Python
TensorFlow梯度求解tf.gradients实例
Feb 04 #Python
基于TensorFlow中自定义梯度的2种方式
Feb 04 #Python
tensorflow 查看梯度方式
Feb 04 #Python
opencv python图像梯度实例详解
Feb 04 #Python
TensorFlow设置日志级别的几种方式小结
Feb 04 #Python
You might like
php后退一页表单内容保存实现方法
2012/06/17 PHP
php mysql_real_escape_string函数用法与实例教程
2013/09/30 PHP
PHP页面实现定时跳转的方法
2014/10/31 PHP
PHP中array_keys和array_unique函数源码的分析
2016/02/26 PHP
php 修改上传文件大小限制实例详解
2016/10/23 PHP
[原创]PHP global全局变量经典应用与注意事项分析【附$GLOBALS用法对比】
2019/07/12 PHP
laravel框架语言包拓展实现方法分析
2019/11/22 PHP
Javascript 获取LI里的内容
2008/12/17 Javascript
Jquery进度条插件 Progress Bar小问题解决
2011/07/12 Javascript
验证手机号码的JS方法分享
2013/09/10 Javascript
Textarea根据内容自适应高度
2013/10/28 Javascript
jqgrid 表格数据导出实例
2013/11/21 Javascript
Nodejs从有门道无门菜鸟起飞必看教程
2016/07/20 NodeJs
Angular表单验证实例详解
2016/10/20 Javascript
解决VUEX兼容IE上的报错问题
2018/03/01 Javascript
Python中的exec、eval使用实例
2014/09/23 Python
Python 操作文件的基本方法总结
2017/08/10 Python
对Python3.6 IDLE常用快捷键介绍
2018/07/16 Python
在python中pandas读文件,有中文字符的方法
2018/12/12 Python
Python opencv实现人眼/人脸识别以及实时打码处理
2019/04/29 Python
Django生成PDF文档显示网页上以及PDF中文显示乱码的解决方法
2019/12/17 Python
Python编程快速上手——strip()函数的正则表达式实现方法分析
2020/02/29 Python
Java Spring项目国际化(i18n)详细方法与实例
2020/03/20 Python
python入门教程之基本算术运算符
2020/11/13 Python
资深财务管理人员自我评价
2013/09/22 职场文书
领导视察欢迎词
2014/01/15 职场文书
旅游项目开发策划书
2014/01/18 职场文书
冰淇淋店的创业计划书
2014/02/07 职场文书
教师求职自荐书
2014/06/14 职场文书
教师职位说明书
2014/07/29 职场文书
2015学校六五普法工作总结
2015/04/22 职场文书
2015迎新晚会开场白
2015/05/29 职场文书
电影地道战观后感
2015/06/04 职场文书
焦点访谈观后感
2015/06/11 职场文书
《活见鬼》教学反思
2016/02/24 职场文书
小程序实现筛子抽奖
2021/05/26 Javascript