【发布时间】: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