mlpack
output_param_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_UTIL_OUTPUT_PARAM_IMPL_HPP
13 #define MLPACK_CORE_UTIL_OUTPUT_PARAM_IMPL_HPP
14 
15 #include "output_param.hpp"
17 #include <iostream>
18 
19 namespace mlpack {
20 namespace bindings {
21 namespace cli {
22 
24 template<typename T>
27  const typename std::enable_if<!arma::is_arma_type<T>::value>::type* /* junk */,
28  const typename std::enable_if<!util::IsStdVector<T>::value>::type* /* junk */,
29  const typename std::enable_if<!data::HasSerialize<T>::value>::type* /* junk */,
30  const typename std::enable_if<!std::is_same<T,
31  std::tuple<data::DatasetInfo, arma::mat>>::value>::type* /* junk */)
32 {
33  std::cout << data.name << ": " << *boost::any_cast<T>(&data.value)
34  << std::endl;
35 }
36 
38 template<typename T>
41  const typename std::enable_if<util::IsStdVector<T>::value>::type* /* junk */)
42 {
43  std::cout << data.name << ": ";
44  const T& t = *boost::any_cast<T>(&data.value);
45  for (size_t i = 0; i < t.size(); ++i)
46  std::cout << t[i] << " ";
47  std::cout << std::endl;
48 }
49 
51 template<typename T>
54  const typename std::enable_if<arma::is_arma_type<T>::value>::type* /* junk */)
55 {
56  typedef std::tuple<T, std::tuple<std::string, size_t, size_t>> TupleType;
57  const T& output = std::get<0>(*boost::any_cast<TupleType>(&data.value));
58  const std::string& filename =
59  std::get<0>(std::get<1>(*boost::any_cast<TupleType>(&data.value)));
60 
61  if (output.n_elem > 0 && filename != "")
62  {
63  if (arma::is_Row<T>::value || arma::is_Col<T>::value)
64  data::Save(filename, output, false);
65  else
66  data::Save(filename, output, false, !data.noTranspose);
67  }
68 }
69 
71 template<typename T>
74  const typename std::enable_if<!arma::is_arma_type<T>::value>::type* /* junk */,
75  const typename std::enable_if<data::HasSerialize<T>::value>::type* /* junk */)
76 {
77  // The const cast is necessary here because Serialize() can't ever be marked
78  // const. In this case we can assume it though, since we will be saving and
79  // not loading.
80  typedef std::tuple<T*, std::string> TupleType;
81  T*& output = const_cast<T*&>(std::get<0>(*boost::any_cast<TupleType>(
82  &data.value)));
83  const std::string& filename =
84  std::get<1>(*boost::any_cast<TupleType>(&data.value));
85 
86  if (filename != "")
87  data::Save(filename, "model", *output);
88 }
89 
91 template<typename T>
94  const typename std::enable_if<std::is_same<T,
95  std::tuple<data::DatasetInfo, arma::mat>>::value>::type* /* junk */)
96 {
97  // Output the matrix with the mappings.
98  typedef std::tuple<T, std::tuple<std::string, size_t, size_t>> TupleType;
99  const T& tuple = std::get<0>(*boost::any_cast<TupleType>(&data.value));
100  const std::string& filename =
101  std::get<0>(std::get<1>(*boost::any_cast<TupleType>(&data.value)));
102  const arma::mat& matrix = std::get<1>(tuple);
103 
104  // The mapping isn't taken into account. We should write a data::Save()
105  // overload for this.
106  if (filename != "")
107  data::Save(filename, matrix, false, !data.noTranspose);
108 }
109 
110 } // namespace cli
111 } // namespace bindings
112 } // namespace mlpack
113 
114 #endif
Definition: has_serialize.hpp:47
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
bool noTranspose
True if this is a matrix that should not be transposed.
Definition: param_data.hpp:69
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
Metaprogramming structure for vector detection.
Definition: is_std_vector.hpp:23
void OutputParamImpl(util::ParamData &data, const typename std::enable_if<!arma::is_arma_type< T >::value >::type *=0, const typename std::enable_if<!util::IsStdVector< T >::value >::type *=0, const typename std::enable_if<!data::HasSerialize< T >::value >::type *=0, const typename std::enable_if<!std::is_same< T, std::tuple< data::DatasetInfo, arma::mat >>::value >::type *=0)
Output an option (print to stdout).
Definition: output_param_impl.hpp:25
boost::any value
The actual value that is held.
Definition: param_data.hpp:82
std::string name
Name of this parameter.
Definition: param_data.hpp:56
bool Save(const std::string &filename, const arma::Mat< eT > &matrix, const bool fatal=false, bool transpose=true, arma::file_type inputSaveType=arma::auto_detect)
Saves a matrix to file, guessing the filetype from the extension.
Definition: save_impl.hpp:47