commit
c6b78e56b1
@ -0,0 +1,70 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include "sampler.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace random {
|
||||
|
||||
Sampler::~Sampler() {}
|
||||
|
||||
UniformSampler::UniformSampler(int64 range)
|
||||
: Sampler(range), inv_range_(1.0 / range) {
|
||||
random_engine_ = std::make_shared<std::mt19937>(seed_);
|
||||
dist_ = std::make_shared<std::uniform_int_distribution<>>(0, range);
|
||||
}
|
||||
|
||||
UniformSampler::UniformSampler(int64 range, unsigned int seed)
|
||||
: Sampler(range, seed), inv_range_(1.0 / range) {
|
||||
random_engine_ = std::make_shared<std::mt19937>(seed_);
|
||||
dist_ = std::make_shared<std::uniform_int_distribution<>>(0, range);
|
||||
}
|
||||
|
||||
int64 UniformSampler::Sample() const { return (*dist_)(*random_engine_); }
|
||||
|
||||
float UniformSampler::Probability(int64 value) const { return inv_range_; }
|
||||
|
||||
LogUniformSampler::LogUniformSampler(int64 range)
|
||||
: Sampler(range), log_range_(log(range + 1)) {
|
||||
random_engine_ = std::make_shared<std::mt19937>(seed_);
|
||||
dist_ = std::make_shared<std::uniform_real_distribution<>>(0, 1);
|
||||
}
|
||||
|
||||
LogUniformSampler::LogUniformSampler(int64 range, unsigned int seed)
|
||||
: Sampler(range, seed), log_range_(log(range + 1)) {
|
||||
random_engine_ = std::make_shared<std::mt19937>(seed_);
|
||||
dist_ = std::make_shared<std::uniform_real_distribution<>>(0, 1);
|
||||
}
|
||||
int64 LogUniformSampler::Sample() const {
|
||||
// Got Log Uniform distribution from uniform distribution by
|
||||
// inverse_transform_sampling method
|
||||
// More details:
|
||||
// https://wanghaoshuang.github.io/2017/11/Log-uniform-distribution-sampler/
|
||||
const int64 value =
|
||||
static_cast<int64>(exp((*dist_)(*random_engine_) * log_range_)) - 1;
|
||||
// Mathematically, value should be <= range_, but might not be due to some
|
||||
// floating point roundoff, so we mod by range_.
|
||||
return value % range_;
|
||||
}
|
||||
|
||||
float LogUniformSampler::Probability(int64 value) const {
|
||||
// Given f(x) = 1/[(x+1) * log_range_]
|
||||
// The value's probability is integral of f(x) from value to (value + 1)
|
||||
// More details:
|
||||
// https://wanghaoshuang.github.io/2017/11/Log-uniform-distribution-sampler
|
||||
return (log((value + 2.0) / (value + 1.0))) / log_range_;
|
||||
}
|
||||
|
||||
} // namespace random
|
||||
} // namespace paddle
|
@ -0,0 +1,100 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <random>
|
||||
typedef long int64;
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
namespace math {
|
||||
|
||||
// TODO(wanghaoshuang): Support for GPU
|
||||
|
||||
/**
|
||||
* Sample integers from [0, range).
|
||||
*/
|
||||
class Sampler {
|
||||
public:
|
||||
explicit Sampler(int64 range) : range_(range) {
|
||||
PADDLE_ENFORCE_GT(range, 0);
|
||||
std::random_device r;
|
||||
seed_ = r();
|
||||
}
|
||||
explicit Sampler(int64 range, unsigned int seed)
|
||||
: range_(range), seed_(seed) {
|
||||
PADDLE_ENFORCE_GT(range, 0);
|
||||
}
|
||||
virtual ~Sampler();
|
||||
// Sample a single value
|
||||
virtual int64 Sample() const = 0;
|
||||
// The probability that a single call to Sample() returns the given value.
|
||||
virtual float Probability(int64 value) const = 0;
|
||||
|
||||
int64 range() { return range_; };
|
||||
|
||||
protected:
|
||||
const int64 range_;
|
||||
unsigned int seed_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sample integers from [0, range).
|
||||
* And the distribution function is:
|
||||
* P(x) = 1 / range
|
||||
*/
|
||||
class UniformSampler : public Sampler {
|
||||
public:
|
||||
explicit UniformSampler(int64 range);
|
||||
|
||||
explicit UniformSampler(int64 range, unsigned int seed);
|
||||
|
||||
~UniformSampler() override {}
|
||||
|
||||
int64 Sample() const override;
|
||||
|
||||
float Probability(int64 value) const override;
|
||||
|
||||
private:
|
||||
const float inv_range_;
|
||||
std::shared_ptr<std::mt19937_64> random_engine_;
|
||||
std::shared_ptr<std::uniform_int_distribution<>> dist_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sample integers from [0, range).
|
||||
* And the distribution function is:
|
||||
* P(x) = (1/ln(range+1)) * ln(1 + 1/(x + 1))
|
||||
*/
|
||||
class LogUniformSampler : public Sampler {
|
||||
public:
|
||||
explicit LogUniformSampler(int64 range);
|
||||
|
||||
explicit LogUniformSampler(int64 range, unsigned int seed);
|
||||
|
||||
~LogUniformSampler() override {}
|
||||
|
||||
int64 Sample() const override;
|
||||
|
||||
float Probability(int64 value) const override;
|
||||
|
||||
private:
|
||||
const float log_range_;
|
||||
std::shared_ptr<std::mt19937_64> random_engine_;
|
||||
std::shared_ptr<std::uniform_real_distribution<>> dist_;
|
||||
};
|
||||
|
||||
} // math
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,66 @@
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import paddle.v2.dataset.wmt16
|
||||
import unittest
|
||||
|
||||
|
||||
class TestWMT16(unittest.TestCase):
|
||||
def checkout_one_sample(self, sample):
|
||||
# train data has 3 field: source language word indices,
|
||||
# target language word indices, and target next word indices.
|
||||
self.assertEqual(len(sample), 3)
|
||||
|
||||
# test start mark and end mark in source word indices.
|
||||
self.assertEqual(sample[0][0], 0)
|
||||
self.assertEqual(sample[0][-1], 1)
|
||||
|
||||
# test start mask in target word indices
|
||||
self.assertEqual(sample[1][0], 0)
|
||||
|
||||
# test en mask in target next word indices
|
||||
self.assertEqual(sample[2][-1], 1)
|
||||
|
||||
def test_train(self):
|
||||
for idx, sample in enumerate(
|
||||
paddle.v2.dataset.wmt16.train(
|
||||
src_dict_size=100000, trg_dict_size=100000)()):
|
||||
if idx >= 10: break
|
||||
self.checkout_one_sample(sample)
|
||||
|
||||
def test_test(self):
|
||||
for idx, sample in enumerate(
|
||||
paddle.v2.dataset.wmt16.test(
|
||||
src_dict_size=1000, trg_dict_size=1000)()):
|
||||
if idx >= 10: break
|
||||
self.checkout_one_sample(sample)
|
||||
|
||||
def test_val(self):
|
||||
for idx, sample in enumerate(
|
||||
paddle.v2.dataset.wmt16.validation(
|
||||
src_dict_size=1000, trg_dict_size=1000)()):
|
||||
if idx >= 10: break
|
||||
self.checkout_one_sample(sample)
|
||||
|
||||
def test_get_dict(self):
|
||||
dict_size = 1000
|
||||
word_dict = paddle.v2.dataset.wmt16.get_dict("en", dict_size, True)
|
||||
self.assertEqual(len(word_dict), dict_size)
|
||||
self.assertEqual(word_dict[0], "<s>")
|
||||
self.assertEqual(word_dict[1], "<e>")
|
||||
self.assertEqual(word_dict[2], "<unk>")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,152 @@
|
||||
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from ..framework import Variable, unique_name
|
||||
from ..registry import OpProtoHolder
|
||||
|
||||
__all__ = ['monkey_patch_variable']
|
||||
|
||||
|
||||
def monkey_patch_variable():
|
||||
def unique_tmp_name():
|
||||
return unique_name("tmp")
|
||||
|
||||
def safe_get_dtype(var):
|
||||
try:
|
||||
dtype = var.dtype
|
||||
except:
|
||||
raise ValueError("Cannot get data type from %s", var.name)
|
||||
return dtype
|
||||
|
||||
def create_tensor(block, value, dtype, shape):
|
||||
value = float(value)
|
||||
tmp_name = unique_tmp_name()
|
||||
var = block.create_var(name=tmp_name, shape=shape, dtype=dtype)
|
||||
block.append_op(
|
||||
type="fill_constant",
|
||||
outputs={'Out': [var]},
|
||||
attrs={'dtype': var.dtype,
|
||||
'shape': shape,
|
||||
'value': value})
|
||||
return var
|
||||
|
||||
def create_scalar(block, value, dtype):
|
||||
return create_tensor(block, value, dtype, shape=[1])
|
||||
|
||||
def create_tensor_with_batchsize(ref_var, value, dtype):
|
||||
assert isinstance(ref_var, Variable)
|
||||
value = float(value)
|
||||
tmp_name = unique_tmp_name()
|
||||
var = ref_var.block.create_var(name=tmp_name, dtype=dtype)
|
||||
ref_var.block.append_op(
|
||||
type='fill_constant_batch_size_like',
|
||||
outputs={'Out': [var]},
|
||||
inputs={'Input': [ref_var]},
|
||||
attrs={'shape': ref_var.shape,
|
||||
'value': value})
|
||||
return var
|
||||
|
||||
def astype(self, dtype):
|
||||
"""
|
||||
Cast a variable to a specified data type.
|
||||
NOTE: The variable must be a Tensor
|
||||
Args:
|
||||
self(Variable): The source variable
|
||||
dtype: The target dtype
|
||||
|
||||
Returns:
|
||||
Variable with new dtype
|
||||
"""
|
||||
tmp_name = unique_tmp_name()
|
||||
out = self.block.create_var(name=tmp_name, dtype=dtype)
|
||||
self.block.append_op(
|
||||
type="cast",
|
||||
inputs={"X": [self]},
|
||||
outputs={"Out": [out]},
|
||||
attrs={"in_dtype": self.dtype,
|
||||
"out_dtype": out.dtype})
|
||||
return out
|
||||
|
||||
def _elemwise_method_creator_(method_name, op_type, reverse=False):
|
||||
def __impl__(self, other_var):
|
||||
lhs_dtype = safe_get_dtype(self)
|
||||
|
||||
if not isinstance(other_var, Variable):
|
||||
if reverse:
|
||||
has_batch_size = False
|
||||
for elem in self.shape:
|
||||
if elem < 0:
|
||||
has_batch_size = True
|
||||
break
|
||||
if not has_batch_size:
|
||||
other_var = create_tensor(
|
||||
self.block,
|
||||
other_var,
|
||||
dtype=lhs_dtype,
|
||||
shape=self.shape)
|
||||
else:
|
||||
other_var = create_tensor_with_batchsize(
|
||||
self, other_var, lhs_dtype)
|
||||
else:
|
||||
# add fill_op to self.block
|
||||
other_var = create_scalar(
|
||||
self.block, value=other_var, dtype=lhs_dtype)
|
||||
|
||||
rhs_dtype = safe_get_dtype(other_var)
|
||||
if lhs_dtype != rhs_dtype:
|
||||
other_var = astype(other_var, lhs_dtype)
|
||||
if reverse:
|
||||
tmp = self
|
||||
self = other_var
|
||||
other_var = tmp
|
||||
|
||||
tmp_name = unique_tmp_name()
|
||||
out = self.block.create_var(name=tmp_name, dtype=lhs_dtype)
|
||||
self.block.append_op(
|
||||
type=op_type,
|
||||
inputs={'X': [self],
|
||||
'Y': [other_var]},
|
||||
outputs={'Out': out})
|
||||
return out
|
||||
|
||||
comment = OpProtoHolder.instance().get_op_proto(op_type).comment
|
||||
|
||||
__impl__.__doc__ = """
|
||||
{0}
|
||||
Args:
|
||||
self(Variable): left hand variable
|
||||
other_var(Variable|float|int): right hand variable
|
||||
|
||||
Returns:
|
||||
Variable
|
||||
""".format(comment)
|
||||
__impl__.__name__ = method_name
|
||||
return __impl__
|
||||
|
||||
# inject methods
|
||||
for method_name, op_type, reverse in (
|
||||
("__add__", "elementwise_add", False),
|
||||
# a+b == b+a. Do not need to reverse explicitly
|
||||
("__radd__", "elementwise_add", False),
|
||||
("__sub__", "elementwise_sub", False),
|
||||
("__rsub__", "elementwise_sub", True),
|
||||
("__mul__", "elementwise_mul", False),
|
||||
# a*b == b*a. Do not need to reverse explicitly
|
||||
("__rmul__", "elementwise_mul", False),
|
||||
("__div__", "elementwise_div", False),
|
||||
("__rdiv__", "elementwise_div", True)):
|
||||
setattr(Variable, method_name,
|
||||
_elemwise_method_creator_(method_name, op_type, reverse))
|
||||
|
||||
Variable.astype = astype
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue