Commit bc2f02d7 authored by Davis King's avatar Davis King

Implemented the CPU side of the tools in cuda_dlib.h and also cleaned some

things up a bit.
parent 2bc5202b
......@@ -12,6 +12,19 @@ namespace dlib
namespace cpu
{
// -----------------------------------------------------------------------------------
void multiply (
tensor& dest,
const tensor& src
)
{
const auto d = dest.host();
const auto s = src.host();
for (size_t i = 0; i < src.size(); ++i)
d[i] *= s[i];
}
// -----------------------------------------------------------------------------------
void affine_transform(
......@@ -21,7 +34,11 @@ namespace dlib
const float B
)
{
// TODO
dest.copy_size(src);
const auto d = dest.host();
const auto s = src.host();
for (size_t i = 0; i < src.size(); ++i)
d[i] = A*s[i] + B;
}
// -----------------------------------------------------------------------------------
......@@ -33,7 +50,36 @@ namespace dlib
const tensor& B
)
{
// TODO
DLIB_CASSERT(
((A.num_samples()==1 && B.num_samples()==1) ||
(A.num_samples()==src.num_samples() && B.num_samples()==src.num_samples())) &&
A.nr()==B.nr() && B.nr()==src.nr() &&
A.nc()==B.nc() && B.nc()==src.nc() &&
A.k() ==B.k() && B.k()==src.k(),"");
dest.copy_size(src);
auto d = dest.host();
auto s = src.host();
const auto a = A.host();
const auto b = B.host();
if (A.num_samples() == 1)
{
const long num = src.size()/src.num_samples();
for (size_t i = 0; i < src.num_samples(); ++i)
{
for (long j = 0; j < num; ++j)
{
*d = a[j]*(*s) + b[j];
d++;
s++;
}
}
}
else
{
for (size_t i = 0; i < src.size(); ++i)
d[i] = a[i]*s[i] + b[i];
}
}
// -----------------------------------------------------------------------------------
......@@ -397,41 +443,19 @@ namespace dlib
// -----------------------------------------------------------------------------------
dropout::
dropout(
float drop_rate
)
{
}
dropout::
dropout(
float drop_rate,
int seed
)
{
}
void dropout::
operator() (
resizable_tensor& dest,
resizable_tensor& random_mask,
const tensor& src
)
{
}
void dropout::
get_gradient(
const tensor& gradient_input,
const tensor& random_mask,
tensor& grad
void threshold (
tensor& data,
float thresh
)
{
const auto d = data.host();
for (size_t i = 0; i < data.size(); ++i)
d[i] = d[i]>thresh ? 1:0;
}
// -----------------------------------------------------------------------------------
}
}
......
......@@ -12,6 +12,13 @@ namespace dlib
namespace cpu
{
// -----------------------------------------------------------------------------------
void multiply (
tensor& dest,
const tensor& src
);
// -----------------------------------------------------------------------------------
void affine_transform(
......@@ -74,37 +81,10 @@ namespace dlib
// -----------------------------------------------------------------------------------
class dropout
{
public:
// not copyable
dropout(const dropout&) = delete;
dropout& operator=(const dropout&) = delete;
// but is movable
dropout(dropout&& item) : dropout() { swap(item); }
dropout& operator=(dropout&& item) { swap(item); return *this; }
dropout(float drop_rate = 0.5);
dropout(float drop_rate, int seed);
void swap(dropout& item)
{
// TODO
}
void operator() (
resizable_tensor& dest,
resizable_tensor& random_mask,
const tensor& src
);
void get_gradient(
const tensor& gradient_input,
const tensor& random_mask,
tensor& grad
);
};
void threshold (
tensor& data,
float thresh
);
// -----------------------------------------------------------------------------------
......
......@@ -12,8 +12,20 @@ namespace dlib
namespace cuda
{
// TODO, remove this
void add_arrays(const gpu_data& a, const gpu_data& b, gpu_data& out);
// -----------------------------------------------------------------------------------
void multiply (
tensor& dest,
const tensor& src
);
/*!
requires
- have_same_dimensions(dest,src) == true
ensures
- #dest == dest*src
That is, for all valid i:
#dest.host()[i] == dest.host()[i]*src.host()[i]
!*/
// -----------------------------------------------------------------------------------
......@@ -39,15 +51,22 @@ namespace dlib
);
/*!
requires
- A.num_samples() == 1
- B.num_samples() == 1
- if (A.num_samples() == 1) then
- B.num_samples() == 1
- else
- A.num_samples() == src.num_samples()
- B.num_samples() == src.num_samples()
- A.nr() == B.nr() == src.nr()
- A.nc() == B.nc() == src.nc()
- A.k() == B.k() == src.k()
ensures
- have_same_dimensions(#dest,src) == true
- #dest == A*src + B
(done for each sample in src)
- if (A.num_samples() == 1) then
- #dest == A*src + B
(done for each sample in src)
- else
- for all valid i:
- #dest.host()[i] == A.host()[i]*src.host()[i] + B.host()[i]
!*/
// -----------------------------------------------------------------------------------
......@@ -108,9 +127,9 @@ namespace dlib
ensures
- Let f(src,gamma,beta) == dot(gradient_input, dest output of
batch_normalize(dest,means,vars,src,gamma,beta))
- Adds the gradient of f() with respect to src to #src
- Adds the gradient of f() with respect to gamma to #gamma
- Adds the gradient of f() with respect to beta to #beta
- Adds the gradient of f() with respect to src to #src_grad.
- Adds the gradient of f() with respect to gamma to #gamma_grad.
- Adds the gradient of f() with respect to beta to #beta_grad.
!*/
void batch_normalize_conv (
......@@ -147,59 +166,41 @@ namespace dlib
tensor& gamma_grad,
tensor& beta_grad
);
/*!
requires
- vars and means should be the output of a call to
batch_normalize_conv(dest,means,vars,src,gamma,beta)
- have_same_dimensions(gradient_input, src) == true
- have_same_dimensions(src, src_grad) == true
- src.num_samples() > 1
- gamma.num_samples()==gamma.nr()==gamma.nc() == 1
- have_same_dimensions(gamma, gamma_grad) == true
- have_same_dimensions(gamma, beta_grad) == true
- gamma.k() == src.k()
- have_same_dimensions(means, gamma) == true
- have_same_dimensions(vars, gamma) == true
ensures
- Let f(src,gamma,beta) == dot(gradient_input, dest output of
batch_normalize_conv(dest,means,vars,src,gamma,beta))
- Adds the gradient of f() with respect to src to #src_grad.
- Adds the gradient of f() with respect to gamma to #gamma_grad.
- Adds the gradient of f() with respect to beta to #beta_grad.
!*/
// -----------------------------------------------------------------------------------
class dropout
{
/*!
!*/
public:
// not copyable
dropout(const dropout&) = delete;
dropout& operator=(const dropout&) = delete;
// but is movable
dropout(dropout&& item) : dropout() { swap(item); }
dropout& operator=(dropout&& item) { swap(item); return *this; }
dropout(float drop_rate = 0.5);
dropout(float drop_rate, int seed);
void swap(dropout& item)
{
// TODO
}
void operator() (
resizable_tensor& dest,
resizable_tensor& random_mask,
const tensor& src
);
/*!
ensures
- have_same_dimensions(src, #dest) == true
- have_same_dimensions(src, #random_mask) == true
!*/
void get_gradient(
const tensor& gradient_input,
const tensor& random_mask,
tensor& grad
);
/*!
requires
- have_same_dimensions(gradient_input, random_mask) == true
- have_same_dimensions(gradient_input, grad) == true
ensures
- let OUT and MASK be the output of (*this)(OUT,MASK,src)
- let f(src) == dot(gradient_input,OUT)
- Then this function computes the gradient of f() with respect to src
and adds it to grad.
!*/
};
void threshold (
tensor& data,
float thresh
);
/*!
ensures
- Sets all elements of data to 1 or 0 depending on if they are above or
below the given threshold. Specifically, for all valid i:
- #data.host()[i] == data.host()[i]>thresh ? 1 : 0
!*/
// -----------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment