如何将张量列表乘以 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是一个Tensor包含n_timesteps元素的列表,因此pred = tf.matmul (outputs, weights['out']) + biases['out']抛出错误

ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shape: [100,128,16], [16,1]。

. 我怎样才能做这个乘法?

佩佩

解决方案是将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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Tensorflow如何将一维张量转换为矢量

来自分类Dev

Tensorflow:成本的张量列表

来自分类Dev

将标量乘以具有tensorflow keras后端的张量

来自分类Dev

如何在Tensorflow中乘以多维张量的维度之一?

来自分类Dev

TensorFlow如何命名张量?

来自分类Dev

如何将Tensorflow.js中的3D张量重塑为4D张量?

来自分类Dev

TensorFlow:如何对张量进行排名?

来自分类Dev

如何安装TensorFlow的张量板?

来自分类Dev

如何将tensorflow.js模型权重转换为pytorch张量并返回?

来自分类Dev

Tensorflow convert_to_tensor TypeError:预期为单个Tensor时的张量列表

来自分类Dev

Tensorflow convert_to_tensor TypeError:预期为单个Tensor时的张量列表

来自分类Dev

tensorflow组或将获取操作或张量收集到单个获取中

来自分类Dev

如何将不同大小的张量列表转换为单个张量?

来自分类Dev

张量的Tensorflow“映射操作”?

来自分类Dev

TensorFlow 打印输入张量?

来自分类Dev

Tensorflow中图形中的张量名称列表

来自分类Dev

根据TensorFlow中的python列表过滤张量

来自分类Dev

Tensorflow + LSF。LSF集群上的分布式张量流

来自分类Dev

Tensorflow 中张量和天花板上的 Argmax

来自分类Dev

如何将张量列表转换为torch :: Tensor?

来自分类Dev

如何将pytorch张量列表分离为数组

来自分类Dev

MNIST Tensorflow:如何将形式[i]的张量转换为形式[... 0,0,0,1,0,0 ...]的张量,其中1在第i个位置?

来自分类Dev

将TensorFlow张量转换为Numpy数组

来自分类Dev

TensorFlow:将图像导入张量流模型

来自分类Dev

如何将张量推到TensorFlow队列并将其从另一个进程中拉出?

来自分类Dev

如何从Tensorflow中的张量获取特定行?

来自分类Dev

Tensorflow:如何在张量中修改值

来自分类Dev

Tensorflow:如何通过名称获取张量?

来自分类Dev

如何在Tensorflow中保持堆叠张量

Related 相关文章

  1. 1

    Tensorflow如何将一维张量转换为矢量

  2. 2

    Tensorflow:成本的张量列表

  3. 3

    将标量乘以具有tensorflow keras后端的张量

  4. 4

    如何在Tensorflow中乘以多维张量的维度之一?

  5. 5

    TensorFlow如何命名张量?

  6. 6

    如何将Tensorflow.js中的3D张量重塑为4D张量?

  7. 7

    TensorFlow:如何对张量进行排名?

  8. 8

    如何安装TensorFlow的张量板?

  9. 9

    如何将tensorflow.js模型权重转换为pytorch张量并返回?

  10. 10

    Tensorflow convert_to_tensor TypeError:预期为单个Tensor时的张量列表

  11. 11

    Tensorflow convert_to_tensor TypeError:预期为单个Tensor时的张量列表

  12. 12

    tensorflow组或将获取操作或张量收集到单个获取中

  13. 13

    如何将不同大小的张量列表转换为单个张量?

  14. 14

    张量的Tensorflow“映射操作”?

  15. 15

    TensorFlow 打印输入张量?

  16. 16

    Tensorflow中图形中的张量名称列表

  17. 17

    根据TensorFlow中的python列表过滤张量

  18. 18

    Tensorflow + LSF。LSF集群上的分布式张量流

  19. 19

    Tensorflow 中张量和天花板上的 Argmax

  20. 20

    如何将张量列表转换为torch :: Tensor?

  21. 21

    如何将pytorch张量列表分离为数组

  22. 22

    MNIST Tensorflow:如何将形式[i]的张量转换为形式[... 0,0,0,1,0,0 ...]的张量,其中1在第i个位置?

  23. 23

    将TensorFlow张量转换为Numpy数组

  24. 24

    TensorFlow:将图像导入张量流模型

  25. 25

    如何将张量推到TensorFlow队列并将其从另一个进程中拉出?

  26. 26

    如何从Tensorflow中的张量获取特定行?

  27. 27

    Tensorflow:如何在张量中修改值

  28. 28

    Tensorflow:如何通过名称获取张量?

  29. 29

    如何在Tensorflow中保持堆叠张量

热门标签

归档