【问题标题】:How to reshape a tensor with None dimension tensorflow如何使用无维度张量流重塑张量
【发布时间】:2020-11-16 09:19:12
【问题描述】:

我有一个形状为 (3, None, 80, 10) 的张量,我想将其重塑为 (3*None, 80, 10)。我尝试使用以下代码行,但没有得到所需的形状 (3*None, 80, 10),我得到了一个形状为 (None, None, None) 的张量:

shape = [tf.shape(node_embed_tmp)[k] for k in range(4)]
Y = tf.reshape(node_embed_tmp, [shape[0]*shape[1], shape[2], shape[3]])

任何建议,请!

编辑:

我也试过下面的代码:

shape = [tf.shape(node_embed_tmp)[k] for k in range(4)]
Y=tf.reshape(node_embed_tmp, [-1, shape[2], shape[3]])

但我得到了(None, None, None) 的形状和以下错误:ValueError: Input size (depth of inputs) must be accessible via shape inference, but saw value None.

【问题讨论】:

  • 你在看 model.summary() 吗?尝试用实际数据调用模型
  • 3*None 没有任何意义。你为什么要那个?你的意思是(3,80,10)
  • 我是 Tensorflow 的新手,我正在寻找通过以下代码行在上述张量上使用 LSTM lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(embedding_u_size) # init_state = cell.zero_state(None, dtype=tf.float32) node_type_embed, _ = tf.nn.dynamic_rnn(lstm_cell, Y, time_major= True, dtype=tf.float32),我认为在这种情况下我不能使用 model.summary() 它是不是 keras 层。
  • @Ragnar 3维表示某个类别的数据,None维表示每个类别的batch_size
  • 我想你想要的是(None, 80, 10, 3)

标签: python tensorflow tensor


【解决方案1】:

重塑可变维度张量具有挑战性, 你可以使用 keras.Input 库

from tensorflow import keras
tensor_shape = (3, None, 80, 10)
input = keras.Input(shape=((None,) + tensor_shape[1:]), dtype = 'int32')
input.shape

输出

TensorShape([None, None, None, 80, 10])

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 2017-09-05
    • 2020-10-13
    • 1970-01-01
    • 2023-01-19
    • 2018-04-12
    • 2022-07-26
    • 1970-01-01
    • 2016-02-15
    相关资源
    最近更新 更多