Tensorflow RNN如何创建具有各种批处理大小的零状态? [英] Tensorflow RNN how to create zero state with various batch size?

查看:63
本文介绍了Tensorflow RNN如何创建具有各种批处理大小的零状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题中如何做当state_is_tuple = True?时,我设置了TensorFlow RNN状态:

In this question How do I set TensorFlow RNN state when state_is_tuple=True?: the accepted answer initialize the initial state like this:

state_placeholder = tf.placeholder(tf.float32, [num_layers, 2, batch_size, state_size])

我认为这需要特定的批次大小,而我现在拥有的是:

I assume this requires a specific batch size, while what I have now is:

inputSeq = tf.placeholder(tf.float32, [None, seqLength, observationDim], name='input_seq')
outputs, final_state = tf.nn.dynamic_rnn(cell, inputSeq, initial_state=initialState)

并且我希望此 initialState 为零状态,并且可以配置,因为 inputSeq 的批处理大小可能会有所不同.但是, cell.zero_state 不接受无"作为批处理大小.有什么解决方法吗?

And I want this initialState to be a zero state, and can be configurable as the batch size of inputSeq could vary. However, cell.zero_state does not accept None as batch size. Is there any workaround?

推荐答案

cell.zero_state 接受标量张量.

通过 tf.shape 获取占位符的批处理大小,然后完成:B = tf.shape(state_placeholder)[0]#批量大小的标量张量initial_state = cell.zero_state(B)

Get the batch size of the place holder via tf.shape, then it is done: B = tf.shape(state_placeholder)[0] # the batch size scalar tensor initial_state = cell.zero_state(B)

这篇关于Tensorflow RNN如何创建具有各种批处理大小的零状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆