繁体   English   中英

如何在TensorFlow上将张量列表乘以单个张量?

[英]How to multiply list of tensors by single tensor on TensorFlow?

我正在实现RNN,并且与我发现的示例相反,这些示例仅将最后一步中的输出成本最小化

x = tf.placeholder ("float", [features_dimension, None, n_timesteps])
y = tf.placeholder ("float", [labels_dimension, None, n_timesteps])

# Define weights
weights = {'out': tf.Variable (tf.random_normal ([N_HIDDEN, labels_dimension]))}
biases = {'out': tf.Variable (tf.random_normal ([labels_dimension]))}

def RNN (x, weights, biases):
    # Prepare data shape to match `rnn` function requirements
    # Current data input shape: (features_dimension, BATCH_SIZE, n_timesteps)
    # Required shape: `n_timesteps` tensors list of shape (BATCH_SIZE, features_dimension)
    # We make a division of the data to split it in individual vectors that
    # will be fed for each timestep

    # Permuting features_dimension and n_timesteps
    # Shape will be (n_timesteps, BATCH_SIZE, features_dimension)
    x = tf.transpose (x, [2, 1, 0])
    # Reshaping to (BATCH_SIZE*n_timesteps, features_dimension) (we are removing the depth dimension with this)
    x = tf.reshape(x, [BATCH_SIZE*n_timesteps, features_dimension])
    # Split the previous 2D tensor to get a list of `n_timesteps` tensors of
    # shape (batch_size, features_dimension).
    x = tf.split (x, n_timesteps, 0)

    # Define a lstm cell with tensorflow
    lstm_cell = rnn.BasicLSTMCell (N_HIDDEN, forget_bias=1.0)
    # Get lstm cell output
    outputs, states = rnn.static_rnn (lstm_cell, x, dtype=tf.float32)
    # Linear activation; outputs contains the array of outputs for all the
    # timesteps
    pred = tf.matmul (outputs, weights['out']) + biases['out']

但是,对象outputs是具有n_timesteps元素的Tensor列表,因此pred = tf.matmul (outputs, weights['out']) + biases['out']引发错误

ValueError:形状必须为2级,但对于输入形状为[100,128,16],[16,1]的'MatMul'(op:'MatMul'),其级别为3。

我该如何进行乘法?

解决方案是将张量列表tf.stack到3d张量中,然后使用tf.map_fn在沿维度0的每个2d张量上应用乘法运算:

    # Transform the list into a 3D tensor with dimensions (n_timesteps, batch_size, N_HIDDEN)
    outputs = tf.stack(outputs)

    def pred_fn(current_output):
        return tf.matmul(current_output, weights['out']) + biases['out']
    # Use tf.map_fn to apply pred_fn to each tensor in outputs, along dimension 0 (timestep dimension)
    pred = tf.map_fn(pred_fn, outputs)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM