Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote branch 'upstream/master'

  • Loading branch information...
commit 6ef3e7d7f19f94b147c8c567bb16fa622781a3a4 2 parents 8dd9d87 + c735498
@lisitsyn lisitsyn authored
View
13 src/libshogun/distributions/Gaussian.cpp
@@ -56,8 +56,8 @@ void CGaussian::init()
for (int i = 0; i < m_cov_rows; i++)
m_constant *= m_cov_inverse[i*m_cov_rows+i];
- m_constant = 1/m_constant;
- m_constant *= pow(2*M_PI, (float64_t) -m_cov_rows/2);
+ m_constant = -CMath::log(m_constant);
+ m_constant -= (m_cov_rows/2.0)*CMath::log(2*M_PI);
result = clapack_dpotri(CblasRowMajor, CblasLower, m_cov_rows, m_cov_inverse, m_cov_rows);
}
@@ -106,21 +106,22 @@ float64_t CGaussian::get_log_model_parameter(int32_t num_param)
float64_t CGaussian::get_log_derivative(int32_t num_param, int32_t num_example)
{
+ SG_NOTIMPLEMENTED;
return 0;
}
-float64_t CGaussian::get_likelihood_example(int32_t num_example)
+float64_t CGaussian::get_log_likelihood_example(int32_t num_example)
{
ASSERT(features->has_property(FP_DOT));
float64_t* point;
int32_t point_len;
((CDotFeatures *)features)->get_feature_vector(&point, &point_len, num_example);
- float64_t answer = compute_PDF(point, point_len);
+ float64_t answer = compute_log_PDF(point, point_len);
delete[] point;
return answer;
}
-float64_t CGaussian::compute_PDF(float64_t* point, int32_t point_len)
+float64_t CGaussian::compute_log_PDF(float64_t* point, int32_t point_len)
{
ASSERT(m_mean && m_cov);
ASSERT(point_len == m_mean_length);
@@ -134,7 +135,7 @@ float64_t CGaussian::compute_PDF(float64_t* point, int32_t point_len)
cblas_dsymv(CblasRowMajor, CblasLower, m_mean_length, -1.0/2.0, m_cov_inverse, m_mean_length,
difference, 1, 0, result, 1);
- float64_t answer = m_constant * exp(cblas_ddot(m_mean_length, difference, 1, result, 1));
+ float64_t answer = m_constant+cblas_ddot(m_mean_length, difference, 1, result, 1);
delete[] difference;
delete[] result;
View
27 src/libshogun/distributions/Gaussian.h
@@ -87,29 +87,26 @@ class CGaussian : public CDistribution
* @param num_example which example
* @return log likelihood for example
*/
- virtual float64_t get_log_likelihood_example(int32_t num_example)
- {
- return CMath::log(get_likelihood_example(num_example));
- }
+ virtual float64_t get_log_likelihood_example(int32_t num_example);
- /** compute likelihood for example
- *
- * abstract base method
+ /** compute PDF
*
- * @param num_example which example
- * @return likelihood for example
+ * @param point
+ * @param point_len
+ * @return computed PDF
*/
- virtual float64_t get_likelihood_example(int32_t num_example);
+ virtual inline float64_t compute_PDF(float64_t* point, int32_t point_len)
+ {
+ return CMath::exp(compute_log_PDF(point, point_len));
+ }
- /** compute PDF
- *
- * computes \f$(2\pi)^{-\frac{k}{2}}|\Sigma|^{-\frac{1}{2}}e^{-\frac{1}{2}(x-\mu)'\Sigma^{-1}(x-\mu)}\f$
+ /** compute log PDF
*
* @param point
* @param point_len
- * @return computed PDF
+ * @return computed log PDF
*/
- virtual float64_t compute_PDF(float64_t* point, int32_t point_len);
+ virtual float64_t compute_log_PDF(float64_t* point, int32_t point_len);
/** get mean
*
View
96 src/libshogun/lib/BinaryFile.cpp
@@ -87,33 +87,43 @@ GET_MATRIX(get_real_matrix, float64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT6
GET_MATRIX(get_longreal_matrix, floatmax_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOATMAX))
#undef GET_MATRIX
-void CBinaryFile::get_byte_ndarray(uint8_t*& array, int32_t*& dims, int32_t& num_dims)
-{
-}
-
-void CBinaryFile::get_char_ndarray(char*& array, int32_t*& dims, int32_t& num_dims)
-{
+#define GET_NDARRAY(fname,sg_type,datatype) \
+void CBinaryFile::fname(sg_type *& array, int32_t *& dims,int32_t & num_dims)\
+{ \
+ size_t total = 1; \
+ \
+ if (!file) \
+ SG_ERROR("File invalid.\n"); \
+ \
+ TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); \
+ read_header(&dtype); \
+ \
+ if (dtype!=datatype) \
+ SG_ERROR("Datatype mismatch\n"); \
+ \
+ if (fread(&num_dims,sizeof(int32_t),1,file) != 1) \
+ SG_ERROR("Failed to read number of dimensions"); \
+ \
+ dims = new int32_t[num_dims]; \
+ if (fread(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \
+ SG_ERROR("Failed to read sizes of dimensions!"); \
+ \
+ for (int32_t i = 0;i < num_dims;i++) \
+ total *= dims[i]; \
+ \
+ array = new sg_type[total]; \
+ if (fread(array,sizeof(sg_type),total,file) != (size_t)total) \
+ SG_ERROR("Failed to read array data!"); \
}
-void CBinaryFile::get_int_ndarray(int32_t*& array, int32_t*& dims, int32_t& num_dims)
-{
-}
-
-void CBinaryFile::get_shortreal_ndarray(float32_t*& array, int32_t*& dims, int32_t& num_dims)
-{
-}
-
-void CBinaryFile::get_real_ndarray(float64_t*& array, int32_t*& dims, int32_t& num_dims)
-{
-}
-
-void CBinaryFile::get_short_ndarray(int16_t*& array, int32_t*& dims, int32_t& num_dims)
-{
-}
-
-void CBinaryFile::get_word_ndarray(uint16_t*& array, int32_t*& dims, int32_t& num_dims)
-{
-}
+GET_NDARRAY(get_byte_ndarray,uint8_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_UINT8));
+GET_NDARRAY(get_char_ndarray,char,TSGDataType(CT_NDARRAY, ST_NONE, PT_CHAR));
+GET_NDARRAY(get_int_ndarray,int32_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_INT32));
+GET_NDARRAY(get_short_ndarray,int16_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_INT16));
+GET_NDARRAY(get_word_ndarray,uint16_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_UINT16));
+GET_NDARRAY(get_shortreal_ndarray,float32_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_FLOAT32));
+GET_NDARRAY(get_real_ndarray,float64_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_FLOAT64));
+#undef GET_NDARRAY
#define GET_SPARSEMATRIX(fname, sg_type, datatype) \
void CBinaryFile::fname(TSparse<sg_type>*& matrix, int32_t& num_feat, int32_t& num_vec) \
@@ -254,6 +264,42 @@ SET_MATRIX(set_real_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
SET_MATRIX(set_longreal_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
#undef SET_MATRIX
+#define SET_NDARRAY(fname,sg_type,datatype) \
+void CBinaryFile::fname(sg_type * array, int32_t * dims,int32_t num_dims) \
+{ \
+ size_t total = 1; \
+ \
+ if (!file) \
+ SG_ERROR("File invalid.\n"); \
+ \
+ if (!array) \
+ SG_ERROR("Invalid array!\n"); \
+ \
+ TSGDataType t datatype; \
+ write_header(&t); \
+ \
+ if (fwrite(&num_dims,sizeof(int32_t),1,file) != 1) \
+ SG_ERROR("Failed to write number of dimensions!\n"); \
+ \
+ if (fwrite(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \
+ SG_ERROR("Failed to write sizes of dimensions!\n"); \
+ \
+ for (int32_t i = 0;i < num_dims;i++) \
+ total *= dims[i]; \
+ \
+ if (fwrite(array,sizeof(sg_type),total,file) != (size_t)total) \
+ SG_ERROR("Failed to write array data!\n"); \
+}
+
+SET_NDARRAY(set_byte_ndarray,uint8_t,(CT_NDARRAY, ST_NONE, PT_UINT8));
+SET_NDARRAY(set_char_ndarray,char,(CT_NDARRAY, ST_NONE, PT_CHAR));
+SET_NDARRAY(set_int_ndarray,int32_t,(CT_NDARRAY, ST_NONE, PT_INT32));
+SET_NDARRAY(set_short_ndarray,int16_t,(CT_NDARRAY, ST_NONE, PT_INT16));
+SET_NDARRAY(set_word_ndarray,uint16_t,(CT_NDARRAY, ST_NONE, PT_UINT16));
+SET_NDARRAY(set_shortreal_ndarray,float32_t,(CT_NDARRAY, ST_NONE, PT_FLOAT32));
+SET_NDARRAY(set_real_ndarray,float64_t,(CT_NDARRAY, ST_NONE, PT_FLOAT64));
+#undef SET_NDARRAY
+
#define SET_SPARSEMATRIX(fname, sg_type, dtype) \
void CBinaryFile::fname(const TSparse<sg_type>* matrix, \
int32_t num_feat, int32_t num_vec) \
View
24 src/libshogun/lib/BinaryFile.h
@@ -15,6 +15,7 @@
#include "base/SGObject.h"
#include "lib/io.h"
#include "lib/SimpleFile.h"
+#include "lib/File.h"
namespace shogun
{
@@ -255,6 +256,29 @@ class CBinaryFile: public CFile
const uint16_t* matrix, int32_t num_feat, int32_t num_vec);
//@}
+ /** @name N-Dimensional Array Access Functions
+ *
+ * Functions to access n-dimensional arrays of one of the several base data types.
+ * These functions are used when writing array of num_dims dimensions to e.g. a file.
+ * Dims contain sizes of every dimensions.
+ */
+ //@{
+ virtual void set_byte_ndarray(
+ uint8_t* array, int32_t* dims, int32_t num_dims);
+ virtual void set_char_ndarray(
+ char* array, int32_t* dims, int32_t num_dims);
+ virtual void set_int_ndarray(
+ int32_t* array, int32_t* dims, int32_t num_dims);
+ virtual void set_shortreal_ndarray(
+ float32_t* array, int32_t* dims, int32_t num_dims);
+ virtual void set_real_ndarray(
+ float64_t* array, int32_t* dims, int32_t num_dims);
+ virtual void set_short_ndarray(
+ int16_t* array, int32_t* dims, int32_t num_dims);
+ virtual void set_word_ndarray(
+ uint16_t* array, int32_t* dims, int32_t num_dims);
+ //@}
+
/** @name Sparse Matrix Access Functions
*
* Functions to access sparse matrices of one of the several base data types.
View
3  src/libshogun/lib/DataType.cpp
@@ -60,6 +60,7 @@ TSGDataType::to_string(char* dest, size_t n) const
case CT_SCALAR: strncpy(p, "", n); break;
case CT_VECTOR: strncpy(p, "Vector<", n); break;
case CT_MATRIX: strncpy(p, "Matrix<", n); break;
+ case CT_NDARRAY: strncpy(p, "N-Dimensional Array<", n); break;
}
size_t np = strlen(p);
@@ -67,7 +68,7 @@ TSGDataType::to_string(char* dest, size_t n) const
switch (m_ctype) {
case CT_SCALAR: break;
- case CT_VECTOR: case CT_MATRIX:
+ case CT_VECTOR: case CT_MATRIX: case CT_NDARRAY:
strcat(p, ">"); break;
}
}
View
2  src/libshogun/lib/DataType.h
@@ -47,7 +47,7 @@ template <class T> struct TSparse {
};
enum EContainerType {
- CT_SCALAR, CT_VECTOR, CT_MATRIX
+ CT_SCALAR, CT_VECTOR, CT_MATRIX, CT_NDARRAY
};
enum EStructType {
Please sign in to comment.
Something went wrong with that request. Please try again.