【问题标题】:What's the input_size in a BasicRNNCell in tensorflow?张量流中 BasicRNNCell 中的 input_size 是多少?
【发布时间】:2018-04-12 13:23:45
【问题描述】:

根据BasicRNNCell的文档:

__call__(
    inputs,
    state,
    scope=None)

Args:
inputs: 2-D tensor with shape [batch_size x input_size].

似乎input_size 在不同的运行中可以不同?据我对RNN的了解,input_size确定了形状为(input_size, hidden_state_size)的内部权重矩阵W_x,应该是一致的。如果我用input_size=3input_size=4 交替运行这个单元格会怎样?

【问题讨论】:

标签: machine-learning tensorflow neural-network deep-learning recurrent-neural-network


【解决方案1】:

inputs 是一个二维张量:[batch_size x input_size]

你是对的,input_size必须对应RNN单元的num_units。但batch_size 可以变化,只需对应调用的另一个参数state

试试这个代码:

import tensorflow as tf
from tensorflow.contrib.rnn import BasicRNNCell

dim = 10
x = tf.placeholder(tf.float32, shape=[None, dim])
y = tf.placeholder(tf.float32, shape=[4, dim])
z = tf.placeholder(tf.float32, shape=[None, dim + 1])
print('x, y, z:', x.shape, y.shape, z.shape)

cell = BasicRNNCell(dim)
state1 = cell.zero_state(batch_size=4, dtype=tf.float32)
state2 = cell.zero_state(batch_size=8, dtype=tf.float32)

out1, out2 = cell(x, state1)
print(out1.shape, out2.shape)

out1, out2 = cell(x, state2)
print(out1.shape, out2.shape)

out1, out2 = cell(y, state1)
print(out1.shape, out2.shape)

这是输出:

x, y, z: (?, 10) (4, 10) (?, 11)
(4, 10) (4, 10)
(8, 10) (8, 10)
(4, 10) (4, 10)

此单元格接受具有两种状态的x,具有state1y,并且不接受具有任何状态的z。以下两个调用都会导致错误:

out1, out2 = cell(y, state2)     # ERROR: dimensions mismatch
print(out1.shape, out2.shape)

out1, out2 = cell(z, state1)     # ERROR: dimensions mismatch
print(out1.shape, out2.shape)

【讨论】:

    猜你喜欢
    • 2017-07-11
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    相关资源
    最近更新 更多