当前位置:网站首页>【Tensorflow】共享机制
【Tensorflow】共享机制
2022-04-23 06:11:00 【^_^ 晅菲】
由于需要写一些TF的代码,忘记共享机制实现,复习并写了一下。
1. 共享机制
TF主要提供 Variable Scope 这种独特的机制来共享变量
> #创建或返回给定名称的变量
> tf.get_variable(<name>, <shape>, <initializer>)
>
> #管理传给get_variable()的变量名称的作用域
> tf.variable_scope(<scope_name>)
2. 实例
Step 1:创建变量
def conv_relu(input, kernel_shape, bias_shape):
# Create variable named "weights".
weights = tf.get_variable("weights", kernel_shape,
initializer=tf.random_normal_initializer())
# Create variable named "biases".
biases = tf.get_variable("biases", bias_shape,
initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(input, weights,
strides=[1, 1, 1, 1], padding='SAME')
return tf.nn.relu(conv + biases)
Step 2:定义作用域:这个部分是可以复用的模块,可以是卷积等操作。
def my_image_filter(input_images):
with tf.variable_scope("conv1"):
# Variables created here will be named "conv1/weights", "conv1/biases".
relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
with tf.variable_scope("conv2"):
# Variables created here will be named "conv2/weights", "conv2/biases".
return conv_relu(relu1, [5, 5, 32, 32], [32])
Step3:复用
这里一定注意,复用某个卷积块的时候一定记得使用:scope.reuse_variables()
with tf.variable_scope("image_filters") as scope:
result1 = my_image_filter(image1)
scope.reuse_variables() # 最关键的一句
result2 = my_image_filter(image2) # 这样的话image2与image1就可以使用相同的模块了
3. 参考:
版权声明
本文为[^_^ 晅菲]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43882112/article/details/122124054
边栏推荐
- ProcessBuilder工具类
- 【动态规划】不同的二叉搜索树
- 【2021年新书推荐】Red Hat RHCSA 8 Cert Guide: EX200
- 【点云系列】PnP-3D: A Plug-and-Play for 3D Point Clouds
- Android interview Online Economic encyclopedia [constantly updating...]
- Machine learning II: logistic regression classification based on Iris data set
- MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
- HandlerThread原理和实际应用
- 第2章 Pytorch基础1
- 【动态规划】杨辉三角
猜你喜欢

winform滚动条美化
![[recommendation of new books in 2021] enterprise application development with C 9 and NET 5](/img/1d/cc673ca857fff3c5c48a51883d96c4.png)
[recommendation of new books in 2021] enterprise application development with C 9 and NET 5

PaddleOCR 图片文字提取

Summary of image classification white box anti attack technology
树莓派:双色LED灯实验

ArcGIS license server administrator cannot start the workaround

c语言编写一个猜数字游戏编写

【2021年新书推荐】Professional Azure SQL Managed Database Administration
![[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide](/img/36/1c484aec5efbac8ae49851844b7946.png)
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide

MySQL的安装与配置——详细教程
随机推荐
MySQL notes 1_ database
ArcGIS license server administrator cannot start the workaround
Computer shutdown program
素数求解的n种境界
【動態規劃】不同路徑2
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide
launcher隐藏不需要显示的app icon
[多屏互动] 实现双多屏异显二:startActivity方式
【點雲系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
Gephi教程【1】安装
【动态规划】三角形最小路径和
GEE配置本地开发环境
JNI中使用open打开文件是返回-1问题
Pytorch模型保存与加载(示例)
.net加载字体时遇到 Failed to decode downloaded font:
PyTorch最佳实践和代码编写风格指南
HandlerThread原理和实际应用
ProcessBuilder工具类
Handler进阶之sendMessage原理探索
微信小程序 使用wxml2canvas插件生成图片部分问题记录