Commit 60938073 authored by Sebastian Wolf's avatar Sebastian Wolf

Merge branch 'v3' of git:xerus/xerus into v3

parents c87eef84 7f4af266
Pipeline #668 failed with stages
in 3 minutes and 44 seconds
......@@ -59,7 +59,7 @@ namespace xerus { namespace misc {
template<class T>
void stream_writer(std::ostream& _stream, const std::vector<T> &_value, FileFormat _format) {
write_to_stream<uint64>(_stream, _value.size(), _format);
write_to_stream<size_t>(_stream, _value.size(), _format);
for (size_t i = 0; i < _value.size(); ++i) {
write_to_stream<T>(_stream, _value[i], _format);
}
......@@ -90,7 +90,7 @@ namespace xerus { namespace misc {
template<class T>
void stream_reader(std::istream& _stream, std::vector<T> &_obj, const FileFormat _format) {
_obj.resize(read_from_stream<uint64>(_stream, _format));
_obj.resize(read_from_stream<size_t>(_stream, _format));
for (size_t i = 0; i < _obj.size(); ++i) {
read_from_stream<T>(_stream, _obj[i], _format);
}
......
......@@ -41,24 +41,27 @@ static misc::UnitTest als_id("ALS", "identity", [](){
X(k^3) = I(k^3,l^3)*B(l^3);
TEST(frob_norm(X(k^3) - B(k^3)) < 1e-13);
TTTensor ttB(B, 0.001);
TTTensor ttX(X, 0.001);
TTOperator ttI(I, 0.001);
TTTensor ttB(B, 1e-8);
TTTensor ttX(X, 1e-8);
TTOperator ttI(I, 1e-8);
ttX(k^3) = ttI(k^3,l^3)*ttB(l^3);
TEST(frob_norm(ttI(k^3,l^3)*ttB(l^3) - ttB(k^3)) < 1e-13);
TEST(frob_norm(ttI(k^3,l^3)*ttX(l^3) - ttX(k^3)) < 1e-13);
TEST(frob_norm(ttI(k^3,l^3)*ttB(l^3) - ttB(k^3)) < 1e-8);
TEST(frob_norm(ttI(k^3,l^3)*ttX(l^3) - ttX(k^3)) < 1e-8);
PerformanceData perfdata;
value_t result = ALS_SPD(ttI, ttX, ttB, 0.001, perfdata);
MTEST(result < 0.01, "1 " << result);
auto ourALS = ALS_SPD;
ourALS.useResidualForEndCriterion = true;
value_t result = ourALS(ttI, ttX, ttB, 0.0001, perfdata);
MTEST(result/frob_norm(ttB) < 0.01, "1 " << result/frob_norm(ttB));
MTEST(frob_norm(ttX - ttB) < 1e-13 * 1000, "1 " << frob_norm(ttX - ttB));
perfdata.reset();
ttX = TTTensor::random(ttX.dimensions, ttX.ranks());
result = ALS_SPD(ttI, ttX, ttB, 0.001, perfdata);
MTEST(result < 0.01, "2 " << result);
result = ourALS(ttI, ttX, ttB, 0.0001, perfdata);
MTEST(result/frob_norm(ttB) < 0.01, "2 " << result/frob_norm(ttB));
MTEST(frob_norm(ttX - ttB) < 1e-9, "2 " << frob_norm(ttX - ttB)); // approx 1e-16 * dim * max_entry
});
......
......@@ -62,35 +62,35 @@ static misc::UnitTest sparse_creation("SparseTensor", "Creation", [](){
fullX += fullA;
sparseX += sparseA;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX -= fullB;
sparseX -= sparseB;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = fullA + fullB;
sparseX = sparseA + sparseB;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = fullA - fullB;
sparseX = sparseA - sparseB;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = 2.0*fullA;
sparseX = 2.0*sparseA;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = 10.0*fullA*2;
sparseX = 10.0*sparseA*2;
MTEST(approx_entrywise_equal(fullX, sparseX, 1e-16), std::scientific << frob_norm(fullX-sparseX));
MTEST(approx_entrywise_equal(fullX, sparseX, 1e-13), std::scientific << frob_norm(fullX-sparseX));
fullX = fullA/10.0;
sparseX = sparseA/10.0;
MTEST(approx_entrywise_equal(fullX, sparseX, 1e-16), std::scientific << frob_norm(fullX-sparseX));
MTEST(approx_entrywise_equal(fullX, sparseX, 1e-13), std::scientific << frob_norm(fullX-sparseX));
fullX = 0*fullA + fullB;
sparseX = 0*sparseA + sparseB;
MTEST(approx_entrywise_equal(fullX, sparseX, 1e-16), std::scientific << frob_norm(fullX-sparseX));
MTEST(approx_entrywise_equal(fullX, sparseX, 1e-13), std::scientific << frob_norm(fullX-sparseX));
fullX = 7.3*fullA + fullB*5;
sparseX = 7.3*sparseA + sparseB*5;
......@@ -100,8 +100,8 @@ static misc::UnitTest sparse_creation("SparseTensor", "Creation", [](){
sparseX = 7.9*sparseA/13.7 + sparseB*5;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
TEST(approx_entrywise_equal(fullA, sparseA, 1e-16));
TEST(approx_entrywise_equal(fullB, sparseB, 1e-16));
TEST(approx_entrywise_equal(fullA, sparseA, 1e-13));
TEST(approx_entrywise_equal(fullB, sparseB, 1e-13));
// Two times to be sure
......@@ -115,27 +115,27 @@ static misc::UnitTest sparse_creation("SparseTensor", "Creation", [](){
fullX = fullA + fullB;
sparseX = sparseA + sparseB;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = fullA - fullB;
sparseX = sparseA - sparseB;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = 2.0*fullA;
sparseX = 2.0*sparseA;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = 10.0*fullA*2;
sparseX = 10.0*sparseA*2;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = fullA/10.0;
sparseX = sparseA/10.0;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = 0*fullA + fullB;
sparseX = 0*sparseA + sparseB;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-16));
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
fullX = 7.3*fullA + fullB*5;
sparseX = 7.3*sparseA + sparseB*5;
......@@ -145,6 +145,6 @@ static misc::UnitTest sparse_creation("SparseTensor", "Creation", [](){
sparseX = 7.9*sparseA/13.7 + sparseB*5;
TEST(approx_entrywise_equal(fullX, sparseX, 1e-13));
TEST(approx_entrywise_equal(fullA, sparseA, 1e-16));
TEST(approx_entrywise_equal(fullB, sparseB, 1e-16));
TEST(approx_entrywise_equal(fullA, sparseA, 1e-13));
TEST(approx_entrywise_equal(fullB, sparseB, 1e-13));
});
......@@ -1698,20 +1698,20 @@ namespace xerus {
}
// storage version number
write_to_stream<uint64>(_stream, 1, _format);
write_to_stream<size_t>(_stream, 1, _format);
write_to_stream(_stream, _obj.dimensions, _format);
if (_obj.representation == Tensor::Representation::Dense) {
write_to_stream<uint64>(_stream, 1, _format);
write_to_stream<size_t>(_stream, 1, _format);
for (size_t i = 0; i < _obj.size; ++i) {
write_to_stream<value_t>(_stream, _obj[i], _format);
}
} else {
write_to_stream<uint64>(_stream, 2, _format);
write_to_stream<uint64>(_stream, _obj.get_unsanitized_sparse_data().size(), _format);
write_to_stream<size_t>(_stream, 2, _format);
write_to_stream<size_t>(_stream, _obj.get_unsanitized_sparse_data().size(), _format);
for (const auto &d : _obj.get_unsanitized_sparse_data()) {
write_to_stream<uint64>(_stream, d.first, _format);
write_to_stream<size_t>(_stream, d.first, _format);
write_to_stream<value_t>(_stream, _obj.factor*d.second, _format);
}
}
......@@ -1719,7 +1719,7 @@ namespace xerus {
void stream_reader(std::istream& _stream, Tensor &_obj, const FileFormat _format) {
IF_CHECK(uint64 ver = )read_from_stream<uint64>(_stream, _format);
IF_CHECK(size_t ver = )read_from_stream<size_t>(_stream, _format);
REQUIRE(ver == 1, "Unknown stream version to open (" << ver << ")");
// Load dimensions
......@@ -1727,7 +1727,7 @@ namespace xerus {
read_from_stream(_stream, dims, _format);
// Load representation
const uint64 rep = read_from_stream<uint64>(_stream, _format);
const size_t rep = read_from_stream<size_t>(_stream, _format);
// Load data
if (rep == 1) { // Dense
......@@ -1744,13 +1744,13 @@ namespace xerus {
REQUIRE(rep == 2, "Unknown tensor representation " << rep << " in stream");
_obj.reset(std::move(dims), Tensor::Representation::Sparse);
const uint64 num = read_from_stream<uint64>(_stream, _format);
const uint64 num = read_from_stream<size_t>(_stream, _format);
REQUIRE(num < std::numeric_limits<size_t>::max(), "The stored Tensor is to large to be loaded using 32 Bit xerus.");
for (size_t i = 0; i < num; ++i) {
REQUIRE(_stream, "Unexpected end of stream in reading sparse Tensor.");
// NOTE inline function calls can be called in any order by the compiler, so we have to cache the results to ensure correct order
uint64 pos = read_from_stream<uint64>(_stream, _format);
uint64 pos = read_from_stream<size_t>(_stream, _format);
value_t val = read_from_stream<value_t>(_stream, _format);
_obj.get_unsanitized_sparse_data().emplace(pos, val);
}
......
......@@ -1431,7 +1431,7 @@ namespace xerus {
_stream << std::setprecision(std::numeric_limits<value_t>::digits10 + 1);
}
// storage version number
write_to_stream<uint64>(_stream, 1, _format);
write_to_stream<size_t>(_stream, 1, _format);
// Save dimensions
write_to_stream(_stream, _obj.dimensions, _format);
......@@ -1439,22 +1439,22 @@ namespace xerus {
// save external links
for(const TensorNetwork::Link& el : _obj.externalLinks) {
write_to_stream<uint64>(_stream, el.other, _format);
write_to_stream<uint64>(_stream, el.indexPosition, _format);
write_to_stream<uint64>(_stream, el.dimension, _format);
write_to_stream<size_t>(_stream, el.other, _format);
write_to_stream<size_t>(_stream, el.indexPosition, _format);
write_to_stream<size_t>(_stream, el.dimension, _format);
}
if(_format == FileFormat::TSV) { _stream << "\n\n"; }
// Save nodes with their links
write_to_stream<uint64>(_stream, _obj.nodes.size(), _format);
write_to_stream<size_t>(_stream, _obj.nodes.size(), _format);
if(_format == FileFormat::TSV) { _stream << '\n'; }
for(const TensorNetwork::TensorNode& node : _obj.nodes) {
write_to_stream<uint64>(_stream, node.neighbors.size(), _format);
write_to_stream<size_t>(_stream, node.neighbors.size(), _format);
for(const TensorNetwork::Link& link : node.neighbors) {
write_to_stream<bool>(_stream, link.external, _format);
write_to_stream<uint64>(_stream, link.other, _format);
write_to_stream<uint64>(_stream, link.indexPosition, _format);
write_to_stream<uint64>(_stream, link.dimension, _format);
write_to_stream<size_t>(_stream, link.other, _format);
write_to_stream<size_t>(_stream, link.indexPosition, _format);
write_to_stream<size_t>(_stream, link.dimension, _format);
}
}
if(_format == FileFormat::TSV) { _stream << '\n'; }
......@@ -1467,7 +1467,7 @@ namespace xerus {
}
void stream_reader(std::istream& _stream, TensorNetwork &_obj, const FileFormat _format) {
IF_CHECK( uint64 ver = ) read_from_stream<uint64>(_stream, _format);
IF_CHECK( size_t ver = ) read_from_stream<size_t>(_stream, _format);
REQUIRE(ver == 1, "Unknown stream version to open (" << ver << ")");
// Load dimensions
......@@ -1477,21 +1477,21 @@ namespace xerus {
_obj.externalLinks.resize(_obj.dimensions.size());
for(TensorNetwork::Link& el : _obj.externalLinks) {
el.external = false;
el.other = read_from_stream<uint64>(_stream, _format);
el.indexPosition = read_from_stream<uint64>(_stream, _format);
el.dimension = read_from_stream<uint64>(_stream, _format);
el.other = read_from_stream<size_t>(_stream, _format);
el.indexPosition = read_from_stream<size_t>(_stream, _format);
el.dimension = read_from_stream<size_t>(_stream, _format);
}
// Load nodes with their links
_obj.nodes.resize(read_from_stream<uint64>(_stream, _format));
_obj.nodes.resize(read_from_stream<size_t>(_stream, _format));
for(TensorNetwork::TensorNode& node : _obj.nodes) {
node.neighbors.resize(read_from_stream<uint64>(_stream, _format));
node.neighbors.resize(read_from_stream<size_t>(_stream, _format));
node.erased = false;
for(TensorNetwork::Link& link : node.neighbors) {
link.external = read_from_stream<bool>(_stream, _format);
link.other = read_from_stream<uint64>(_stream, _format);
link.indexPosition = read_from_stream<uint64>(_stream, _format);
link.dimension = read_from_stream<uint64>(_stream, _format);
link.other = read_from_stream<size_t>(_stream, _format);
link.indexPosition = read_from_stream<size_t>(_stream, _format);
link.dimension = read_from_stream<size_t>(_stream, _format);
}
}
......
......@@ -1450,11 +1450,12 @@ namespace xerus {
_stream << std::setprecision(std::numeric_limits<value_t>::digits10 + 1);
}
// storage version number
write_to_stream<uint64>(_stream, 1, _format);
write_to_stream<size_t>(_stream, 1, _format);
// store TN specific data
write_to_stream<bool>(_stream, _obj.canonicalized, _format);
write_to_stream<uint64>(_stream, _obj.corePosition, _format);
write_to_stream<size_t>(_stream, _obj.corePosition, _format);
// save rest of TN
write_to_stream<TensorNetwork>(_stream, _obj, _format);
......@@ -1465,12 +1466,13 @@ namespace xerus {
template<bool isOperator>
void stream_reader(std::istream& _stream, TTNetwork<isOperator> &_obj, const misc::FileFormat _format) {
IF_CHECK( uint64 ver = ) read_from_stream<uint64>(_stream, _format);
IF_CHECK( size_t ver = ) read_from_stream<size_t>(_stream, _format);
REQUIRE(ver == 1, "Unknown stream version to open (" << ver << ")");
// load TN specific data
read_from_stream<bool>(_stream, _obj.canonicalized, _format);
read_from_stream<uint64>(_stream, _obj.corePosition, _format);
read_from_stream<size_t>(_stream, _obj.corePosition, _format);
// load rest of TN
read_from_stream<TensorNetwork>(_stream, _obj, _format);
......
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