tensorflow源碼閱讀

注:在利用 tensorflow 做實際項目時,對其中的某些模塊使用不明白或實現好奇。因此想到寫一篇 trace code 的博客用來記錄官方的實現方法。剛開始一個一個模塊可能比較凌亂,等到積累到一定程度會整理爲一篇系統的 tensorflow 代碼解析的博客。

1,SAME or VALID padding

對於 VALID padding 而言,padding 數量爲0,因此
output_spatial_shape[i] = ceil((input_spatial_shape[i] - (spatial_filter_shape[i]-1) / strides[i]).

對於SAME padding而言利用 nn_ops.py中的_with_space_to_batch_base_paddings 來計算兩端padding的數量。

def _with_space_to_batch_base_paddings(filter_shape, num_spatial_dims,
                                       rate_or_const_rate):
  """Helper function to compute base_paddings."""
  # Spatial dimensions of the filters and the upsampled filters in which we
  # introduce (rate - 1) zeros between consecutive filter values.
  filter_spatial_shape = filter_shape[:num_spatial_dims]
  dilated_filter_spatial_shape = (
      filter_spatial_shape + (filter_spatial_shape - 1) *
      (rate_or_const_rate - 1))
  pad_extra_shape = dilated_filter_spatial_shape - 1

  # When full_padding_shape is odd, we pad more at end, following the same
  # convention as conv2d.
  pad_extra_start = pad_extra_shape // 2
  pad_extra_end = pad_extra_shape - pad_extra_start
  base_paddings = array_ops.stack(
      [[pad_extra_start[i], pad_extra_end[i]] for i in range(num_spatial_dims)])
  return base_paddings

因此output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i])

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章