【问题标题】:How to use optimizer.minimize with maxPooling layers in TensorFlow.js如何在 TensorFlow.js 中将 optimize.minimize 与最大池化层一起使用
【发布时间】:2019-02-02 13:04:31
【问题描述】:

在 TensorFlow.js 中使用 optimizer.minimize()model.predict() 训练具有损失函数的模型时,我遇到了一个问题。这只发生在我在卷积神经网络中使用maxPooling2D 层时,代码类似于下面的代码。它会产生此错误:Cannot read property 'backend' of undefined。我不确定是什么原因造成的或如何解决它。使用没有任何池化层的卷积层 (tf.layers.conv2d()) 时不会发生该错误。我正在使用 TensorFlow.js 版本 0.14.2 和 Google Chrome 版本 71.0.3578.98。可以使用以下代码重现此错误:

loss = (pred, label) => pred.sub(label).square().mean();
optimizer = tf.train.sgd(0.001);

const input = tf.input({shape: [100, 100, 4]});
const conv = tf.layers.conv2d({
    kernelSize: 5,
    filters: 8,
    strides: 1,
    activation: 'relu',
    kernelInitializer: 'VarianceScaling'
});
const pool = tf.layers.maxPooling2d({
    poolSize: [2, 2],
    strides: [2, 2]
});
const flat = tf.layers.flatten();
const dense = tf.layers.dense({units: 10});
const output = dense.apply(flat.apply(pool.apply(conv.apply(input))));
const model = tf.model({inputs: input, outputs: output});

for (var i = 0; i < 10; i++) {
    optimizer.minimize(() =>
        loss(model.predict([tf.ones([1, 100, 100, 4])]), tf.ones([1, 10]))
    );
}

编辑:已解决。详情请见scai's answer

编辑 2:这似乎不是错误,而是使用 model.predict() 对反向传播的更改。 More information

【问题讨论】:

    标签: javascript machine-learning conv-neural-network tensorflow.js


    【解决方案1】:

    在 TensorFlow.js 0.14+ 中,有一个更改禁用了 Model.predict() 方法中的反向传播支持。您可以使用带有 {training: true} 标志的 Model.apply() 方法来修复您的代码。

    即改变

        optimizer.minimize(() =>
        loss(model.predict([tf.ones([1, 100, 100, 4])]), tf.ones([1, 10]))
    );
    

       optimizer.minimize(() =>
        loss(model.apply([tf.ones([1, 100, 100, 4])], {training: true}), tf.ones([1, 10]))
    );
    

    【讨论】:

      猜你喜欢
      • 2019-11-10
      • 2018-07-26
      • 2018-08-04
      • 2022-01-05
      • 2019-12-10
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多