简体   繁体   English

Tensorflow,conv2d和过滤器

[英]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.

相关问题 使用 tf.conv2d 添加偏差 - Tensorflow.js - Add bias with tf.conv2d - Tensorflow.js tf.cast 未更改 dtype 原始问题:tensorflowjs 错误:传递给“conv2d”的参数“x”必须是 float32 张量,但得到了 int32 张量 - tf.cast not changing the dtype ORIGINAL ISSUE:tensorflowjs Error: Argument 'x' passed to 'conv2d' must be float32 tensor, but got int32 tensor tensorflow js:未捕获的错误:检查时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 [28,28,1] 的数组 - tensorflow js: Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [28,28,1] 有没有办法将预期的 conv2d_Conv2D1_input 从 4 维减少到 3? - Is there a way to reduce expected conv2d_Conv2D1_input from 4 dimensions to 3? 错误:输入 0 与层 conv2d_Conv2D1 不兼容:预期 ndim=4,发现 ndim=5 - Error: Input 0 is incompatible with layer conv2d_Conv2D1: expected ndim=4, found ndim=5 CSS 过滤器破坏 3D 变换 - CSS Filters break 3D transforms 使用filters和d3.js在svg中部分更改阴影不透明度 - Change a shadow opacity partially in svg using filters and d3.js 在d3.js中的同一数据集上实现多个过滤器 - Implementing multiple filters on same dataset in d3.js 当页面AJAX进入时,jQuery过滤掉内联AJAX请求 - jQuery filters out inline AJAX request when page AJAX'd in 将 tensorflow.js lstm 的 1d 数组重塑为 3d - reshape 1d array to 3d for tensorflow.js lstm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM