python - 如何用张量索引 LSTMStateTuple 列表?
问题描述
我有一个 pythonLSTMStateTuple
对象列表,我必须使用张量作为索引来检索它们。例如:
index = tf.constant(0)
lstm = tf.nn.rnn_cell.LSTMCell(128)
states = [lstm.zero_state(10, tf.float32), lstm.zero_state(10, tf.float32)]
如果我尝试state = states[index]
我得到一个错误并state = tf.gather(states, index)
转换states
为张量并返回一个张量 shape [10, 2, 128]
。
我怎样才能得到 aLSTMStateTuple
而不是张量?当我将状态传递给 lstm 时,我想避免从列表LSTMStateTuple
到张量以及从张量到的转换。LSTMStateTuple
解决方案
您创建两个状态并将它们放在一个LSTMStateTuple
.
cell_state = tf.placeholder(tf.float32, [batch_size, state_size])
hidden_state = tf.placeholder(tf.float32, [batch_size, state_size])
init_state = tf.nn.rnn_cell.LSTMStateTuple(cell_state, hidden_state)
推荐阅读
- python - 系列不匹配(在“不匹配”系列中查找值)
- git - JetBrains Upsource 在连接 GCP 云源存储库 URL 时抛出未知主机错误 | SSH 连接
- javascript - 是否可以使用 JS 获取代码正在执行的函数的名称?
- javascript - 使用 React 我正在尝试生成代表三个集合的三个按钮,单击时会将您带到该卡片集合
- python-3.x - 我可以获取“外部”类属性并在“内部”类中使用它们吗?
- css - 如何在css中使用渐变的半圆边框
- java - android获取会话ID
- r - 如何创建带有 dbf 扩展名的文件列表?
- angular - 生产构建中的错误:索引 html 生成失败
- html - 如何让我的网站用户更改字体颜色而不让他们访问代码?