当前位置:网站首页>[tensorflow] sharing mechanism
[tensorflow] sharing mechanism
2022-04-23 13:18:00 【^_^ Min Fei】
List of articles
Because I need to write some TF Code for , Forget about sharing mechanism implementation , Reviewed and wrote .
1. Sharing mechanism
TF Mainly provide Variable Scope This unique mechanism to share variables
> # Create or return a variable with a given name
> tf.get_variable(<name>, <shape>, <initializer>)
>
> # Transfer management to get_variable() The scope of the variable name
> tf.variable_scope(<scope_name>)
2. example
Step 1: Create variables
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: Define scope : This part is a reusable module , It can be convolution and other operations .
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: Reuse
Pay attention here , When reusing a convolution block, be sure to use :scope.reuse_variables()
with tf.variable_scope("image_filters") as scope:
result1 = my_image_filter(image1)
scope.reuse_variables() # The most important sentence
result2 = my_image_filter(image2) # In this case image2 And image1 You can use the same module
3. Reference resources :
- Official website guidelines :variable_scope
- https://www.jianshu.com/p/ab0d38725f88
版权声明
本文为[^_^ Min Fei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230611136673.html
边栏推荐
- mui picker和下拉刷新冲突问题
- XML
- 超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- Brief introduction of asynchronous encapsulation interface request based on uniapp
- 【快排】215. 数组中的第K个最大元素
- The use of dcast and melt in R language is simple and easy to understand
- X509 parsing
- Complete project data of UAV apriltag dynamic tracking landing based on openmv (LabVIEW + openmv + apriltag + punctual atom four axes)
- Nodejs + websocket cycle small case
猜你喜欢
GIS practical tips (III) - how to add legend in CASS?
100 GIS practical application cases (51) - a method for calculating the hourly spatial average of NC files according to the specified range in ArcGIS
[official announcement] Changsha software talent training base was established!
Solve the problem that Oracle needs to set IP every time in the virtual machine
LeetCode_ DFS_ Medium_ 695. Maximum area of the island
EMMC / SD learning notes
“湘见”技术沙龙 | 程序员&CSDN的进阶之路
Imx6ull QEMU bare metal tutorial 1: GPIO, iomux, I2C
9419 page analysis of the latest first-line Internet Android interview questions
Lpddr4 notes
随机推荐
【微信小程序】flex布局使用记录
Feature Engineering of interview summary
"Xiangjian" Technology Salon | programmer & CSDN's advanced road
Uninstall MySQL database
CMSIS cm3 source code annotation
Vscode tips
5道刁钻的Activity生命周期面试题,学完去吊打面试官!
Proteus 8.10 installation problem (personal test is stable and does not flash back!)
web三大组件之Servlet
@优秀的你!CSDN高校俱乐部主席招募!
100 GIS practical application cases (34) - splicing 2020globeland30
nodeJs + websocket 循环小案例
How to build a line of code with M4 qprotex
LeetCode_ DFS_ Medium_ 695. Maximum area of the island
vscode小技巧
async void 导致程序崩溃
100000 college students have become ape powder. What are you waiting for?
“湘见”技术沙龙 | 程序员&CSDN的进阶之路
[untitled] make a 0-99 counter, P1 7 connected to key, P2 connected to nixie tube section, common anode nixie tube, P3 0,P3. 1. Connect the nixie tube bit code. Each time you press the key, the nixie
hbuilderx + uniapp 打包ipa提交App store踩坑记