检查输入时出错:预期embedding_Embedding1_input具有形状[,1103],但得到形状为[1103,1]

时间:2020-01-09 17:04:53

标签: tensorflow lstm tensorflow.js

要了解用于将来项目的Tensorflow.js,我尝试使用this tutorial,同时还将示例从Python转换为NodeJS。它的一部分是通过创建带有预测变量输入和标签输出的训练集来生成训练数据。我看不到我的训练集对这些图层的形状是否错误。

模型创建如下:

model = tf.sequential();
model.add( tf.layers.embedding( {
    inputDim: total_words + 1,
    outputDim: 10,
    inputLength: max_length,
} ) );
model.add( tf.layers.lstm( {
    units: 100,
    kernelRegularizer: tf.regularizers.l2( {
        l2: 0.0001
    } )
} ) );
model.add( tf.layers.dropout( {
    rate: 0.1
} ) );
model.add( tf.layers.dense( { units: total_words, activation: 'softmax' } ) );
const optimizer = tf.train.rmsprop( learningRate );
model.compile( { optimizer: optimizer, loss: 'categoricalCrossentropy' } );

训练循环如下:

for ( let i = 0; i < sets.length; ++i ) {
    let predictor = tf.tensor1d( sets[ i ].predictor );
    let label = tf.scalar( sets[ i ].label );
    await model.fit( predictor, label, {
        epochs: epochs,
        callbacks
    } );
    predictor.dispose();
    label.dispose();
}

万一有人问我如何生成训练集:

async function GenerateTrainingSets( paddedInputSequence, maxInput ) {
    let sets = [];
    for ( let i = 0; i < maxInput - 1; i++ ) {
        let predArray = [];
        for ( let preds = 0; preds < sets.length + 1; preds++ ) {
            predArray.push( paddedInputSequence[ preds ] );
        }
        let sizeDifference = maxInput - predArray.length;
        for ( let pads = 0; pads < sizeDifference; pads++ ) {
            predArray.push( 0 );
        }
        sets.push( {
            predictor: predArray,
            label: paddedInputSequence[ i + 1 ]
        } )
    }
    return sets;
}

预测变量是具有相同长度(1103)的一维数组,标号是标量(int)。

1 个答案:

答案 0 :(得分:1)

模型期望输入形状为[null, total_words + 1](对于批处理大小,为空)。输入至少应为2d张量。如果不是(1d张量),它将在轴-1上扩展。

如果要根据单个矢量进行预测,则可以在轴0上扩展输入张量。


let predictor = tf.tensor2d( sets[ i ].predictor, [1, total_words + 1] ); // total_words + 1 should be 1103