python - 将 Tensorflow 模型批量维度重塑为时间序列
问题描述
我正在尝试沿批处理维度重塑 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
我明白错误告诉我什么,但我不确定修复它的正确方法。
解决方案
也许这对你有用:
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
。
推荐阅读
- python - 在机器人框架中使用 MQTT 订阅多个主题
- mongodb-realm - 领域聚合的奇怪回报
- node.js - 阻止 node.js 和电子的音频应用程序
- azure - Grafana 作为 Azure 流分析输出
- apache-spark - 无法从 spark scala 应用程序中的 kafka 流中读取 AVRO 数据
- telegram - 在我点击 bot telethon 电报中的按钮后发送 x 照片
- python - 如何修复 jupyter notebook 中的内核错误?
- octave - cell2mat 产生错误的输出大小
- css - 如何使用 webgpu 上下文禁用画布上的过滤
- spring-boot - 在基于 Axon 的 Spring Boot 微服务中未触发使用 @EventHandler 注释的方法