我是深度学习的初学者,并试图理解算法是如何工作的,使用JavaScript编写它们。现在我正在研究像Tensorflow那样的conv2d的JavaScript实现,并且误解了如何处理不同数量的过滤器,我已经成功完成了一个输出过滤器和多个输出,但我很困惑如何使用多个过滤器输入生成操作,例如: 32 - > 64
以下是使用ndarray的代码示例 :
const outCount = 32 // count of inputs filters
const inCount = 1 // count of output features
const filterSize = 3
const stride = 1
const inShape = [1, 10, 10, outCount]
const outShape = [
1,
Math.ceil((inShape[1] - filterSize + 1) / stride),
Math.ceil((inShape[2] - filterSize + 1) / stride),
outCount
];
const filters = ndarray([], [filterSize, filterSize, inCount, outCount])
const conv2d = (input) => {
const result = ndarray(outShape)
// for each output feature
for (let fo = 0; fo < outCount; fo += 1) {
for (let x = 0; x < outShape[1]; x += 1) {
for (let y = 0; y < outShape[2]; y += 1) {
const fragment = ndarray([], [filterSize, filterSize]);
const filter = ndarray([], [filterSize, filterSize]);
// agregate fragment of image and filter
for (let fx = 0; fx < filterSize; fx += 1) {
for (let fy = 0; fy < filterSize; fy += 1) {
const dx = (x * stride) + fx;
const dy = (y * stride) + fy;
fragment.data.push(input.get(0, dx, dy, 0));
filter.data.push(filters.get(fx, fy, 0, fo));
}
}
// calc dot product of filter and image fragment
result.set(0, x, y, fo, dot(filter, fragment));
}
}
}
return result
}
对于测试我使用Tenforflow作为true的来源,它的算法工作正确但1 -> N
。但我的问题是如何在输入值中添加多个过滤器的支持,例如N -> M
。
有人可以解释如何修改此算法,使其与Tensorflow tf.nn.conv2d
更兼容
非常感谢。
答案 0 :(得分:0)
您需要添加另一个for循环。你没有指定所有的输入形状和尺寸,所以它实际上很难写出来但它看起来很像。
// agregate fragment of image and filter
for (let fx = 0; fx < filterSize; fx += 1) {
for (let fy = 0; fy < filterSize; fy += 1) {
//addition
for (let ch = 0; ch < input.get_channels) {
const dx = (x * stride) + fx;
const dy = (y * stride) + fy;
fragment.data.push(input.get(0, dx, dy, ch));
filter.data.push(filters.get(fx, fy, ch, fo));
}
}
}