@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License . */
limitations under the License . */
# pragma once
# pragma once
# include <stddef.h>
# include <stddef.h>
@ -61,8 +60,8 @@ class RangeError {};
/// Not support Error, such as access GPU memory directly, etc.
/// Not support Error, such as access GPU memory directly, etc.
class UnsupportError : public std : : runtime_error {
class UnsupportError : public std : : runtime_error {
public :
public :
UnsupportError ( ) : std : : runtime_error ( " " ) { } ;
UnsupportError ( ) : std : : runtime_error ( " " ) { } ;
UnsupportError ( const std : : string & message ) : std : : runtime_error ( message ) { } ;
UnsupportError ( const std : : string & message ) : std : : runtime_error ( message ) { } ;
} ;
} ;
/// This type will map to python's list of float.
/// This type will map to python's list of float.
@ -112,7 +111,8 @@ public:
/**
/**
* Create A Matrix with height , width , which is filled by zero .
* Create A Matrix with height , width , which is filled by zero .
*/
*/
static Matrix * createZero ( size_t height , size_t width ,
static Matrix * createZero ( size_t height ,
size_t width ,
bool useGpu = isUsingGpu ( ) ) ;
bool useGpu = isUsingGpu ( ) ) ;
/**
/**
@ -124,8 +124,11 @@ public:
*
*
* @ note the default sparse type is SPARSE_CSR .
* @ note the default sparse type is SPARSE_CSR .
*/
*/
static Matrix * createSparse ( size_t height , size_t width , size_t nnz ,
static Matrix * createSparse ( size_t height ,
bool isNonVal = true , bool trans = false ,
size_t width ,
size_t nnz ,
bool isNonVal = true ,
bool trans = false ,
bool useGpu = isUsingGpu ( ) ) ;
bool useGpu = isUsingGpu ( ) ) ;
/**
/**
@ -134,13 +137,17 @@ public:
* @ param data list of float should be passed in python .
* @ param data list of float should be passed in python .
* @ note the value will be copy into a new matrix .
* @ note the value will be copy into a new matrix .
*/
*/
static Matrix * createDense ( const std : : vector < float > & data , size_t height ,
static Matrix * createDense ( const std : : vector < float > & data ,
size_t width , bool useGpu = isUsingGpu ( ) ) ;
size_t height ,
size_t width ,
static Matrix * createDenseFromNumpy ( float * data , int dim1 , int dim2 ,
bool useGpu = isUsingGpu ( ) ) ;
bool copy = true ,
bool useGpu = isUsingGpu ( ) )
static Matrix * createDenseFromNumpy (
throw ( UnsupportError ) ;
float * data ,
int dim1 ,
int dim2 ,
bool copy = true ,
bool useGpu = isUsingGpu ( ) ) throw ( UnsupportError ) ;
/**
/**
* Create Cpu Dense Matrix from numpy matrix , dtype = float32
* Create Cpu Dense Matrix from numpy matrix , dtype = float32
@ -151,7 +158,9 @@ public:
* @ param copy true if copy into a new matrix , false will create
* @ param copy true if copy into a new matrix , false will create
* matrix inplace .
* matrix inplace .
*/
*/
static Matrix * createCpuDenseFromNumpy ( float * data , int dim1 , int dim2 ,
static Matrix * createCpuDenseFromNumpy ( float * data ,
int dim1 ,
int dim2 ,
bool copy = false ) ;
bool copy = false ) ;
/// Create Gpu Dense Matrix from numpy matrix, dtype=float32
/// Create Gpu Dense Matrix from numpy matrix, dtype=float32
@ -171,11 +180,13 @@ public:
* numpy_mat = m . toNumpyMat ( )
* numpy_mat = m . toNumpyMat ( )
* @ endcode
* @ endcode
*/
*/
void toNumpyMatInplace ( float * * view_data , int * dim1 ,
void toNumpyMatInplace ( float * * view_data ,
int * dim1 ,
int * dim2 ) throw ( UnsupportError ) ;
int * dim2 ) throw ( UnsupportError ) ;
/// Copy To numpy mat.
/// Copy To numpy mat.
void copyToNumpyMat ( float * * view_m_data , int * dim1 ,
void copyToNumpyMat ( float * * view_m_data ,
int * dim1 ,
int * dim2 ) throw ( UnsupportError ) ;
int * dim2 ) throw ( UnsupportError ) ;
/// Copy From Numpy Mat
/// Copy From Numpy Mat
@ -248,15 +259,18 @@ public:
static Vector * create ( const std : : vector < float > & data ,
static Vector * create ( const std : : vector < float > & data ,
bool useGpu = isUsingGpu ( ) ) ;
bool useGpu = isUsingGpu ( ) ) ;
static Vector * createVectorFromNumpy ( float * data , int dim , bool copy = true ,
static Vector * createVectorFromNumpy (
bool useGpu = isUsingGpu ( ) )
float * data ,
throw ( UnsupportError ) ;
int dim ,
bool copy = true ,
bool useGpu = isUsingGpu ( ) ) throw ( UnsupportError ) ;
/**
/**
* Create Cpu Vector from numpy array , which dtype = float32
* Create Cpu Vector from numpy array , which dtype = float32
*
*
* If copy is false , it will create vector inplace .
* If copy is false , it will create vector inplace .
*/
*/
static Vector * createCpuVectorFromNumpy ( float * data , int dim ,
static Vector * createCpuVectorFromNumpy ( float * data ,
int dim ,
bool copy = false ) ;
bool copy = false ) ;
/// Create Gpu Vector from numpy array, which dtype=float32
/// Create Gpu Vector from numpy array, which dtype=float32
@ -312,16 +326,19 @@ public:
static IVector * create ( const std : : vector < int > & data ,
static IVector * create ( const std : : vector < int > & data ,
bool useGpu = isUsingGpu ( ) ) ;
bool useGpu = isUsingGpu ( ) ) ;
static IVector * createVectorFromNumpy ( int * data , int dim , bool copy = true ,
static IVector * createVectorFromNumpy (
bool useGpu = isUsingGpu ( ) )
int * data ,
throw ( UnsupportError ) ;
int dim ,
bool copy = true ,
bool useGpu = isUsingGpu ( ) ) throw ( UnsupportError ) ;
/**
/**
* Create Cpu IVector from numpy array , which dtype = int32
* Create Cpu IVector from numpy array , which dtype = int32
*
*
* If copy is false , it will create vector inplace
* If copy is false , it will create vector inplace
*/
*/
static IVector * createCpuVectorFromNumpy ( int * data , int dim ,
static IVector * createCpuVectorFromNumpy ( int * data ,
int dim ,
bool copy = false ) ;
bool copy = false ) ;
/**
/**
* Create Gpu IVector from numpy array , which dtype = int32
* Create Gpu IVector from numpy array , which dtype = int32
@ -605,7 +622,8 @@ class ParameterTraverseCallback {
public :
public :
~ ParameterTraverseCallback ( ) ;
~ ParameterTraverseCallback ( ) ;
void apply ( const std : : vector < Vector * > & vecs , const ParameterConfig & config ,
void apply ( const std : : vector < Vector * > & vecs ,
const ParameterConfig & config ,
size_t sparseId ) ;
size_t sparseId ) ;
private :
private :
@ -638,7 +656,8 @@ public:
void finishBatch ( ) ;
void finishBatch ( ) ;
void update ( const std : : vector < Vector * > & vecs , const ParameterConfig & conf ,
void update ( const std : : vector < Vector * > & vecs ,
const ParameterConfig & conf ,
size_t sparseId = NO_SPARSE_ID ) ;
size_t sparseId = NO_SPARSE_ID ) ;
std : : vector < int > getParameterTypes ( ) const ;
std : : vector < int > getParameterTypes ( ) const ;
@ -678,7 +697,8 @@ public:
* model config by TrainerConfig
* model config by TrainerConfig
*/
*/
static GradientMachine * createByModelConfig (
static GradientMachine * createByModelConfig (
ModelConfig * conf , GradientMatchineCreateMode mode = CREATE_MODE_NORMAL ,
ModelConfig * conf ,
GradientMatchineCreateMode mode = CREATE_MODE_NORMAL ,
const std : : vector < int > & parameterTypes = defaultParamTypes ) ;
const std : : vector < int > & parameterTypes = defaultParamTypes ) ;
/**
/**
@ -701,7 +721,8 @@ public:
/**
/**
* Combine forward / backward
* Combine forward / backward
*/
*/
void forwardBackward ( const Arguments & inArgs , Arguments * outArgs ,
void forwardBackward ( const Arguments & inArgs ,
Arguments * outArgs ,
PassType passType ,
PassType passType ,
const UpdateCallback & callback = UpdateCallback ( ) ) ;
const UpdateCallback & callback = UpdateCallback ( ) ) ;
@ -722,14 +743,17 @@ public:
*/
*/
SequenceGenerator * asSequenceGenerator (
SequenceGenerator * asSequenceGenerator (
const std : : vector < std : : string > & dict = std : : vector < std : : string > ( ) ,
const std : : vector < std : : string > & dict = std : : vector < std : : string > ( ) ,
size_t begin_id = 0UL , size_t end_id = 0UL , size_t max_length = 100UL ,
size_t begin_id = 0UL ,
size_t end_id = 0UL ,
size_t max_length = 100UL ,
size_t beam_size = - 1UL ) ;
size_t beam_size = - 1UL ) ;
private :
private :
GradientMachinePrivate * m ;
GradientMachinePrivate * m ;
static GradientMachine * createFromPaddleModelPtr (
static GradientMachine * createFromPaddleModelPtr (
const void * confPtr , GradientMatchineCreateMode mode ,
const void * confPtr ,
GradientMatchineCreateMode mode ,
const std : : vector < int > & types ) ;
const std : : vector < int > & types ) ;
// Not to use c++ 11 init-list, so we use static var as function default arg.
// Not to use c++ 11 init-list, so we use static var as function default arg.
@ -751,8 +775,8 @@ public:
/// Create A Trainer By TrainerConfig. using paddle command line.
/// Create A Trainer By TrainerConfig. using paddle command line.
static Trainer * createByCommandLine ( ) throw ( IOError ) ;
static Trainer * createByCommandLine ( ) throw ( IOError ) ;
static Trainer * create ( TrainerConfig * optConfig , GradientMachine * gm )
static Trainer * create ( TrainerConfig * optConfig ,
throw ( IOError ) ;
GradientMachine * gm ) throw ( IOError ) ;
/// Start training
/// Start training
void startTrain ( ) ;
void startTrain ( ) ;