0

我是深度學習的初學者,並試圖理解算法是如何工作的,並使用JavaScript編寫它們。現在我正在研究像Tensorflow那樣的conv2d的JavaScript實現,並且誤解了如何處理不同數量的過濾器,我已經成功地實現了一個輸出過濾器和多個輸出,但是我很困惑如何使用多個過濾器輸入操作來生成操作。 32 - > 64Tensorflow,conv2d和過濾器

下面是代碼例如使用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爲真,並將其算法的一個源工作正確的,但與1 -> N。但我的問題是如何在輸入值中添加多個過濾器的支持,如N -> M

有人可以解釋一下如何修改這個算法,使它更加兼容Tensorflow tf.nn.conv2d 非常感謝。

回答

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)); 
     } 
    } 
    } 
+0

看起來你是絕對正確的,非常感謝你! – XMANX