[英]Tensorflow, conv2d and filters
I'm beginner in deep learning, and trying to understand how algorithms works, writing them using JavaScript. 我是深度学习的初学者,并且试图了解算法的工作原理,并使用JavaScript编写它们。 Now I'm working on JavaScript implementation of conv2d like Tensorflow does, and misunderstand how to handle different count of filters, I have succeeded for one output filter and multiple output, but I'm confused how to produce operations with multiple filters input eg 32 -> 64
现在我像Tensorflow一样致力于conv2d的JavaScript实现,并且误解了如何处理不同数量的过滤器,我已经成功实现了一个输出过滤器和多个输出,但是我很困惑如何使用多个过滤器输入(例如32)进行运算-> 64
Here is example of code using ndarray : 这是使用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
}
For test I'm using a Tenforflow as a source of true and it algorithm works correct but with 1 -> N
. 为了进行测试,我使用Tenforflow作为true的来源,它的算法工作正确,但使用
1 -> N
But my question how to add a support of multiple filters in input value like N -> M
. 但是我的问题是如何在输入值(例如
N -> M
添加对多个过滤器的支持。
Could someone explain how to modify this algorithm to make it more compatible with Tensorflow tf.nn.conv2d
A lot of thanks. 有人可以解释如何修改此算法,使其与Tensorflow
tf.nn.conv2d
更加兼容。非常感谢。
You would need to add another for loop. 您将需要添加另一个for循环。 You didn't specify all of your input shapes and dimensions so it's actually kind of hard to write it exactly but it would look like this.
您并未指定所有输入形状和尺寸,因此实际上很难准确地编写它,但是看起来像这样。
// 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));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.