コード例 #1
0
TYPED_TEST(EmbedLayerTest, TestForwardWithBias) {
  typedef typename TypeParam::Dtype Dtype;
  LayerParameter layer_param;
  EmbedParameter* embed_param = layer_param.mutable_embed_param();
  const int kNumOutput = 10;
  const int kInputDim = 5;
  embed_param->set_num_output(kNumOutput);
  embed_param->set_input_dim(kInputDim);
  embed_param->mutable_weight_filler()->set_type("uniform");
  embed_param->mutable_weight_filler()->set_min(-10);
  embed_param->mutable_weight_filler()->set_max(10);
  embed_param->mutable_bias_filler()->CopyFrom(embed_param->weight_filler());
  embed_param->set_bias_term(true);
  shared_ptr<EmbedLayer<Dtype> > layer(new EmbedLayer<Dtype>(layer_param));
  layer->SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
  ASSERT_EQ(2, layer->blobs().size());
  vector<int> weight_shape(2);
  weight_shape[0] = kInputDim;
  weight_shape[1] = kNumOutput;
  ASSERT_TRUE(weight_shape == layer->blobs()[0]->shape());
  for (int i = 0; i < this->blob_bottom_->count(); ++i) {
    this->blob_bottom_->mutable_cpu_data()[i] = caffe_rng_rand() % kInputDim;
  }
  layer->Forward(this->blob_bottom_vec_, this->blob_top_vec_);
  vector<int> bias_offset(1, 0);
  vector<int> weight_offset(2, 0);
  vector<int> top_offset(5, 0);
  for (int i = 0; i < this->blob_bottom_->count(); ++i) {
    weight_offset[0] = static_cast<int>(this->blob_bottom_->cpu_data()[i]);
    weight_offset[1] = 0;
    top_offset[0] = i;
    top_offset[4] = 0;
    bias_offset[0] = 0;
    for (int j = 0; j < kNumOutput; ++j) {
      EXPECT_EQ(layer->blobs()[0]->data_at(weight_offset) +
                layer->blobs()[1]->data_at(bias_offset),
                this->blob_top_->data_at(top_offset));
      ++top_offset[4];
      ++weight_offset[1];
      ++bias_offset[0];
    }
  }
}
コード例 #2
0
ファイル: reform_layer.cpp プロジェクト: liruoteng/FlowNet
void ReformLayer<Dtype>::reform_copy(const vector<Blob<Dtype>*>& bottom, 
        const vector<Blob<Dtype>*>& top, 
        const Dtype* src_data, 
        Dtype* dest_data,
        bool is_forward)  {
    int slice_num = bottom[0]->shape(0);
    int offset_length = top[0]->num_axes();
    int height = top[0]->shape(2);
    int width = top[0]->shape(3);
    CHECK_EQ(height, 384) << "height";
    CHECK_EQ(width, 512) << "width";
    CHECK_EQ(slice_num,96) << "slice_num";
    CHECK_EQ(patch_size_, 64) << "patch size";
    vector<int> bottom_offset(offset_length, 0);
    vector<int> top_offset(offset_length, 0);
    for (int i = 0; i < slice_num; ++i) {
        for (int r = 0; r < patch_size_; ++r) {
            //bottom_offset[0] = 0;
            bottom_offset[0] = i;
            bottom_offset[2] = r;
            //bottom_offset[3] = 0;
            // top
            top_offset[1] = i * patch_size_ * patch_size_/ (height * width) ; // channel
            top_offset[2] = r + ((i%48) * patch_size_ / width) * patch_size_; // height
            top_offset[3] = (i * patch_size_ ) % width;
            // do the copy
            if (is_forward) {
                caffe_copy(patch_size_, src_data + bottom[0]->offset(bottom_offset),
                    dest_data + top[0]->offset(top_offset));
            } else {
                caffe_copy(patch_size_, src_data + top[0]->offset(top_offset),
                    dest_data + bottom[0]->offset(bottom_offset));
            }
        }  // for loop <r>
    }  // for loop <i>
}  // reform_copy()