tensorflow 动态获取 BatchSzie 的大小实例
作者:jinmingz 时间:2023-03-05 16:56:48
我就废话不多说了,大家还是直接看代码吧~
import tensorflow as tf
import sys
with tf.variable_scope('ha'):
a1 = tf.get_variable('a', shape=[], dtype=tf.int32)
with tf.variable_scope('haha'):
a2 = tf.get_variable('a', shape=[], dtype=tf.int32)
with tf.variable_scope('hahaha'):
a3 = tf.get_variable('a', shape=[], dtype=tf.int32)
with tf.variable_scope('ha', reuse=True):
# 不会创建新的变量
a4 = tf.get_variable('a', shape=[], dtype=tf.int32)
sum = a1 + a2 + a3 + a4
fts_s = tf.placeholder(tf.float32, shape=(None, 100), name='fts_s')
b = tf.zeros(shape=(tf.shape(fts_s)[0], tf.shape(fts_s)[1]))
concat = tf.concat(axis=1, values=[fts_s, b])
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
for var in tf.global_variables():
print var.name
import numpy as np
ft_sample = np.ones((10, 100))
con_value = sess.run([concat], feed_dict={fts_s: ft_sample})
print con_value[0].shape
results:
ha/a:0
ha/haha/a:0
ha/haha/hahaha/a:0
(10, 200)
小总结:
1: 对于未知的shape, 最常用的就是batch-size 通常是 None 代替, 那么在代码中需要用到实际数据的batch size的时候应该怎么做呢?
可以传一个tensor类型, tf.shape(Name) 返回一个tensor 类型的数据, 然后取batchsize 所在的维度即可. 这样就能根据具体的数据去获取batch size的大小
2: 对于变量命名, 要善于用 variable_scope 来规范化命名, 以及 reuse 参数可以控制共享变量
补充知识:tensorflow RNN 使用动态的batch_size
在使用tensorflow实现RNN模型时,需要初始化隐藏状态 如下:
lstm_cell_1 = [tf.nn.rnn_cell.DropoutWrapper(tf.nn.rnn_cell.BasicLSTMCell(HIDDEN_SIZE),output_keep_prob=dropout_keep_prob) for _ in range(NUM_LAYERS)]
cell_1 = tf.nn.rnn_cell.MultiRNNCell(lstm_cell_1)
self.init_state_1 = cell_1.zero_state(self.batch_size,tf.float32)
如果我们直接使用超参数batch_size初始化 在使用模型预测的结果时会很麻烦。我们可以使用动态的batch_size,就是将batch_size作为一个placeholder,在运行时,将batch_size作为输入输入就可以实现根据数据量的大小使用不同的batch_size。
代码实现如下:
self.batch_size = tf.placeholder(tf.int32,[],name='batch_size')
self.state = cell.zero_state(self.batch_size,tf.float32)
来源:https://blog.csdn.net/zjm750617105/article/details/82959175
标签:tensorflow,BatchSzie
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教程:mysql申明变量以及赋值
2009-10-26 10:31:00
js自定义鼠标右键的实现原理及源码
2024-04-28 09:51:39
Django使用Celery加redis执行异步任务的实例内容
2022-08-25 18:09:53
![](https://img.aspxhome.com/file/2023/2/97762_0s.png)
sqlserver 无法验证产品密匙的完美解决方案[测试通过]
2024-01-23 11:53:23
asp如何创建目录?
2009-11-14 20:51:00
网页切片算法的若干问题
2008-04-17 13:10:00
你可能不知道的Python 技巧小结
2022-12-18 07:08:10
sysbench-0.4.12编译安装和CPU测试例子分享
2024-01-24 10:06:21
切换路径在Jupyter里调用本地文件的操作
2022-05-24 15:03:54
![](https://img.aspxhome.com/file/2023/0/66350_0s.jpg)
python 爬取英雄联盟皮肤并下载的示例
2023-07-22 09:57:45
![](https://img.aspxhome.com/file/2023/0/60970_0s.png)
对numpy中shape的深入理解
2023-12-09 03:43:41
sql server 2005用户权限设置深入分析
2024-01-19 12:08:21
pycharm永久激活方法
2021-12-22 01:17:34
![](https://img.aspxhome.com/file/2023/6/89216_0s.png)
Python yield 使用方法浅析
2023-03-30 11:15:18
解决SQL Server日志文件损坏严重的问题
2009-02-05 15:55:00
Vue + Node.js + MongoDB图片上传组件实现图片预览和删除功能详解
2024-05-10 14:14:56
![](https://img.aspxhome.com/file/2023/9/126539_0s.jpg)
客户端和服务端的编码“陷阱”
2007-12-21 19:19:00
python实现八大排序算法(1)
2022-05-20 08:17:47
![](https://img.aspxhome.com/file/2023/6/130996_0s.jpg)
asp如何远程读取数据库页面?
2010-06-16 09:57:00
什么样的图标更具有可用性
2007-10-16 17:47:00
![](https://img.aspxhome.com/file/UploadPic/200710/16/20071016174919899s.jpg)