【问题标题】:Reshape Tensorflow model batch dimension into time series将 Tensorflow 模型批量维度重塑为时间序列
【发布时间】:2020-08-07 20:15:08
【问题描述】:

我正在尝试沿批处理维度重塑 Tensorflow 模型的输入。我想将一些批次样本组合成一个时间序列,以便将其输入 LSTM 层。

具体来说,我有 1024 个样本,我想将它们分成 64 个时间步长的组,结果是 16 批 64 个时间步长,每个时间步长都有原始的 24 个特征。

 #input tensor is (1024, 24)
 inputLayer = Input(shape=(24,))

 #I want it to be (16, 64, 24)
 reshapedLayer = layers.Reshape([64, 24])(inputLayer)
 lstmLayer = layers.LSTM(128, activation='relu')(reshapedLayer)

这会编译但会引发运行时错误

tensorflow.python.framework.errors_impl.InvalidArgumentError:  
Input to reshape is a tensor with 24576 values, but the requested shape has 1572864

我明白错误告诉我什么,但我不确定修复它的正确方法。

【问题讨论】:

    标签: python tensorflow lstm reshape tensor


    【解决方案1】:

    也许这对你有用:

    import tensorflow as tf
    
    inputs = tf.keras.layers.Input(shape=(24,))
    
    x = tf.reshape(inputs, (16, 64, 24))
    x = tf.keras.layers.LSTM(128, activation='relu')(x)
    
    model = tf.keras.Model(inputs=inputs, outputs=x)
    
    # dummy data
    inputs = tf.random.uniform(shape=(1024, 24))
    
    outputs = model(inputs)
    

    tf.reshape替换重塑层。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 2018-10-26
      相关资源
      最近更新 更多