src/trainer.cpp
//Copy the needed parameter from buffer to local blocks
Copy the needed parameter from buffer to local blocks
Copy the needed parameter from buffer to local blocks
int Trainer<T>::CopyParameterFromMultiverso(std::vector<int>& input_layer_nodes, std::vector<int>& output_layer_nodes, void* local_word2vector_neural_network)
{
	SkipGramMixtureNeuralNetwork<T>* word2vector_neural_network = (SkipGramMixtureNeuralNetwork<T>*)local_word2vector_neural_network;
	for (int i = 0; i < input_layer_nodes.size(); ++i)
	{
		T* ptr = (T*)calloc(m_word_sense_info->word_sense_cnts_info[input_layer_nodes[i]] * m_option->embeding_size, sizeof(T));
		int row_id_base = m_word_sense_info->p_input_embedding[input_layer_nodes[i]];
		for (int j = 0, row_id = row_id_base; j < m_word_sense_info->word_sense_cnts_info[input_layer_nodes[i]]; ++j, ++row_id)
			CopyMemory(ptr + j * m_option->embeding_size, GetRow<T>(kInputEmbeddingTableId, row_id), m_option->embeding_size);
		word2vector_neural_network->SetInputEmbeddingWeights(input_layer_nodes[i], ptr);
	}
	for (int i = 0; i < output_layer_nodes.size(); ++i)
	{
		T* ptr = (T*)calloc(m_option->embeding_size, sizeof(T));
		CopyMemory(ptr, GetRow<T>(kEmbeddingOutputTableId, output_layer_nodes[i]), m_option->embeding_size);
		for (int j = 0; j < m_option->embeding_size; j += 5)
			if (!Util::ValidF(static_cast<real>(ptr[j])))
			{
				printf("invalid number\n");
				fflush(stdout);
				throw std::runtime_error("Invalid output embeddings");
			}
		word2vector_neural_network->SetOutputEmbeddingWeights(output_layer_nodes[i], ptr);
	}
	for (int i = 0; i < input_layer_nodes.size(); ++i)
	{
		if (m_word_sense_info->word_sense_cnts_info[input_layer_nodes[i]] > 1)
		{
			T* ptr = (T*)calloc(m_option->sense_num_multi, sizeof(T));
			T* para_ptr = (T*)calloc(m_option->sense_num_multi, sizeof(T));
			CopyMemory(para_ptr, GetRow<T>(kWordSensePriorTableId, m_word_sense_info->p_wordidx2sense_idx[input_layer_nodes[i]]), m_option->sense_num_multi);
			if (!m_option->store_multinomial)
				Util::SoftMax(para_ptr, ptr, m_option->sense_num_multi);
			word2vector_neural_network->SetSensePriorWeights(input_layer_nodes[i], ptr);
			word2vector_neural_network->SetSensePriorParaWeights(input_layer_nodes[i], para_ptr);
		}
	}
	return 0;
}
int Trainer < T > :: CopyParameterFromMultiverso ( std :: vector < int > & input_layer_nodes , std :: vector < int > & output_layer_nodes , void * local_word2vector_neural_network ) { SkipGramMixtureNeuralNetwork < T > * word2vector_neural_network = ( SkipGramMixtureNeuralNetwork < T > * ) local_word2vector_neural_network ; for ( int i = 0 ; i < input_layer_nodes . size ( ) ; ++ i ) { T * ptr = ( T * ) calloc ( m_word_sense_info -> word_sense_cnts_info [ input_layer_nodes [ i ] ] * m_option -> embeding_size , sizeof ( T ) ) ; int row_id_base = m_word_sense_info -> p_input_embedding [ input_layer_nodes [ i ] ] ; for ( int j = 0 , row_id = row_id_base ; j < m_word_sense_info -> word_sense_cnts_info [ input_layer_nodes [ i ] ] ; ++ j , ++ row_id ) CopyMemory ( ptr + j * m_option -> embeding_size , GetRow < T > ( kInputEmbeddingTableId , row_id ) , m_option -> embeding_size ) ; word2vector_neural_network -> SetInputEmbeddingWeights ( input_layer_nodes [ i ] , ptr ) ; } for ( int i = 0 ; i < output_layer_nodes . size ( ) ; ++ i ) { T * ptr = ( T * ) calloc ( m_option -> embeding_size , sizeof ( T ) ) ; CopyMemory ( ptr , GetRow < T > ( kEmbeddingOutputTableId , output_layer_nodes [ i ] ) , m_option -> embeding_size ) ; for ( int j = 0 ; j < m_option -> embeding_size ; j += 5 ) if ( ! Util :: ValidF ( static_cast < real > ( ptr [ j ] ) ) ) { printf ( " \n " ) ; fflush ( stdout ) ; throw std :: runtime_error ( " " ) ; } word2vector_neural_network -> SetOutputEmbeddingWeights ( output_layer_nodes [ i ] , ptr ) ; } for ( int i = 0 ; i < input_layer_nodes . size ( ) ; ++ i ) { if ( m_word_sense_info -> word_sense_cnts_info [ input_layer_nodes [ i ] ] > 1 ) { T * ptr = ( T * ) calloc ( m_option -> sense_num_multi , sizeof ( T ) ) ; T * para_ptr = ( T * ) calloc ( m_option -> sense_num_multi , sizeof ( T ) ) ; CopyMemory ( para_ptr , GetRow < T > ( kWordSensePriorTableId , m_word_sense_info -> p_wordidx2sense_idx [ input_layer_nodes [ i ] ] ) , m_option -> sense_num_multi ) ; if ( ! m_option -> store_multinomial ) Util :: SoftMax ( para_ptr , ptr , m_option -> sense_num_multi ) ; word2vector_neural_network -> SetSensePriorWeights ( input_layer_nodes [ i ] , ptr ) ; word2vector_neural_network -> SetSensePriorParaWeights ( input_layer_nodes [ i ] , para_ptr ) ; } } return 0 ; }
Copy the needed parameter from buffer to local blocks
Copy the needed parameter from buffer to local blocks
//Copy input embedding //Copy output embedding //Copy sense prior //softmax the para_ptr to obtain the multinomial parameters