Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
RoberLopez committed Dec 24, 2024
1 parent 82b2a60 commit 2342a95
Show file tree
Hide file tree
Showing 25 changed files with 110 additions and 351 deletions.
27 changes: 5 additions & 22 deletions examples/translation/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ int main()

srand(static_cast<unsigned>(time(nullptr)));

/*
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
*/

// _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
// _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);


// Data set

Expand All @@ -42,16 +42,14 @@ int main()
// cout<<language_data_set.get_context_length()<<endl;
// cout<<language_data_set.get_completion_length()<<endl;
cout << language_data_set.get_data().dimensions() << endl;
/*
const Index embedding_dimension = 64;
const Index perceptron_depth = 128;
const Index heads_number = 4;
const Index number_of_layers = 1;
const vector <Index> complexity = {embedding_dimension, perceptron_depth, heads_number, number_of_layers};
// Neural network
const dimensions target_dimensions = language_data_set.get_completion_dimensions();
const dimensions input_dimensions = language_data_set.get_context_dimensions();
Expand Down Expand Up @@ -154,14 +152,9 @@ int main()
cout<<prediction<<endl;
cout<<"Target: bad"<<endl;
cout<<endl;
*/

//----------------------------------------------------------------------------------------------------------------------------------------------//
// Data Set

/*
LanguageDataSet language_data_set({0},{0});
language_data_set.load("/home/artelnics/Escritorio/andres_alonso/ViT/dataset/amazon_reviews/language_data_set.xml");
Expand Down Expand Up @@ -195,7 +188,6 @@ int main()
cout<<"Target: bad"<<endl;
cout<<endl;
// cout << "Calculating confusion...." << endl;
// const Tensor<Index, 2> confusion = testing_analysis.calculate_transformer_confusion();
// cout << "\nConfusion matrix:\n" << confusion << endl;
Expand Down Expand Up @@ -240,9 +232,6 @@ int main()
cout<<"Target: good"<<endl;
cout<<endl;
// //only good reviews:
// string prediction = testing_analysis.test_transformer({"I have to use the smallest earpieces provided, but it stays on pretty well."},false);
Expand Down Expand Up @@ -288,10 +277,6 @@ int main()
// string translation = testing_analysis.test_transformer({"I like dogs."},true);
// cout<<translation<<endl;
// //only bad reviews:
// string prediction = testing_analysis.test_transformer({"Tied to charger for conversations lasting more than 45 minutes.MAJOR PROBLEMS!!"},false);
// cout<<prediction<<endl;
Expand Down Expand Up @@ -332,8 +317,6 @@ int main()
// prediction = testing_analysis.test_transformer({"This is essentially a communications tool that does not communicate."},false);
// cout<<prediction<<endl;
// cout<<endl;
*/
cout << "Bye!" << endl;

Expand Down
8 changes: 4 additions & 4 deletions opennn/adaptive_moment_estimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ TrainingResults AdaptiveMomentEstimation::perform_training()
const vector<Index> input_variable_indices = data_set->get_variable_indices(DataSet::VariableUse::Input);

const vector<Index> target_variable_indices = data_set->get_variable_indices(DataSet::VariableUse::Target);
vector<Index> context_variable_indices;
vector<Index> decoder_variable_indices;

if(is_language_model)
{
LanguageDataSet* language_data_set = static_cast<LanguageDataSet*>(data_set);
context_variable_indices = language_data_set->get_variable_indices(DataSet::VariableUse::Decoder);
decoder_variable_indices = language_data_set->get_variable_indices(DataSet::VariableUse::Decoder);
}

const vector<Index> training_samples_indices = data_set->get_sample_indices(DataSet::SampleUse::Training);
Expand Down Expand Up @@ -281,7 +281,7 @@ TrainingResults AdaptiveMomentEstimation::perform_training()
training_batch.fill(training_batches[iteration],
input_variable_indices,
target_variable_indices,
context_variable_indices);
decoder_variable_indices);

// Neural network

Expand Down Expand Up @@ -339,7 +339,7 @@ TrainingResults AdaptiveMomentEstimation::perform_training()
selection_batch.fill(selection_batches[iteration],
input_variable_indices,
target_variable_indices,
context_variable_indices);
decoder_variable_indices);
// Neural network

neural_network->forward_propagate(selection_batch.get_input_pairs(),
Expand Down
2 changes: 1 addition & 1 deletion opennn/batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void Batch::fill(const vector<Index>& sample_indices,
{
/*
// @TODO
Tensor<type, 2>& augmented_data = perform_augmentation(data);
Tensor<type, 2> augmented_data = perform_augmentation(data);
fill_tensor_data(augmented_data, sample_indices, input_indices, input_data);
*/
Expand Down
2 changes: 1 addition & 1 deletion opennn/convolutional_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void ConvolutionalLayer::forward_propagate(const vector<pair<type*, dimensions>>
*/
}

auto start_activations = chrono::high_resolution_clock::now();
//auto start_activations = chrono::high_resolution_clock::now();
if (is_training)
calculate_activations(outputs, activation_derivatives);
else
Expand Down
9 changes: 0 additions & 9 deletions opennn/cross_entropy_error_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ void CrossEntropyError3D::calculate_error(const Batch& batch,

matches.device(*thread_pool_device) = (predictions == targets) && mask;

/*
cout<<"predictions: "<<endl;
cout<<predictions<<endl;
cout<<"targets: "<<endl;
cout<<targets<<endl;
cout<<"matches: "<<endl;
cout<<matches<<endl;
*/

accuracy.device(*thread_pool_device) = matches.cast<type>().sum() / mask_sum(0);

if(isnan(error())) throw runtime_error("Error is NAN");
Expand Down
Binary file modified opennn/language_data_set.h
Binary file not shown.
1 change: 0 additions & 1 deletion opennn/learning_rate_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ void LearningRateAlgorithm::set(LossIndex* new_loss_index)

void LearningRateAlgorithm::set_default()
{

/*
delete thread_pool;
delete thread_pool_device;
Expand Down
16 changes: 8 additions & 8 deletions opennn/loss_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ void LossIndex::set_display(const bool& new_display)
}


void LossIndex::check() const
{
if(!neural_network)
throw runtime_error("Pointer to neural network is nullptr.\n");

if(!data_set)
throw runtime_error("Pointer to data set is nullptr.\n");
}
//void LossIndex::check() const
//{
// if(!neural_network)
// throw runtime_error("Pointer to neural network is nullptr.\n");

// if(!data_set)
// throw runtime_error("Pointer to data set is nullptr.\n");
//}


void LossIndex::calculate_errors_lm(const Batch& batch,
Expand Down
2 changes: 1 addition & 1 deletion opennn/loss_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class LossIndex

// Checking

void check() const;
// void check() const;

// Numerical differentiation

Expand Down
17 changes: 0 additions & 17 deletions opennn/minkowski_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,6 @@ void MinkowskiError::calculate_output_delta(const Batch& batch,
const type coefficient = type(1.0 / batch_samples_number);

deltas.device(*thread_pool_device) = errors*(errors.abs().pow(minkowski_parameter - type(2)))*minkowski_parameter*coefficient;

/*
// p_norm_derivative.device(*thread_pool_device)
// = ;
// if(abs(p_norm_derivative()) < NUMERIC_LIMITS_MIN)
// {
// deltas.setZero();
// return;
// }
const type coefficient = type(1.0 / (p_norm_derivative() * batch_samples_number));
deltas.device(*thread_pool_device) = errors.abs().pow(minkowski_parameter - 1)*(minkowski_parameter/(type)batch_samples_number);
*/
}


Expand Down
26 changes: 0 additions & 26 deletions opennn/multihead_attention_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,27 +415,6 @@ void MultiheadAttentionLayer::calculate_output_projection(const Tensor<type, 4>&
}


// void MultiheadAttentionLayer::compute_attention_scores(const Tensor<type, 4>& query,
// const Tensor<type, 4>& key,
// Tensor<type, 4>& attention_scores,
// Tensor<type, 4>& attention_weights) const
// {
// batch_matrix_multiplication(thread_pool_device.get(), key, query, attention_scores, A_BT);

// attention_scores.device(*thread_pool_device) = attention_scores * scaling_factor;

// if(use_causal_mask)
// apply_causal_mask(attention_scores);
// /*
// * // @todo make sure about this
// softmax(attention_scores, attention_weights);
// */

// softmax(attention_scores);
// attention_weights = attention_scores;

// }

void MultiheadAttentionLayer::compute_attention_scores(const Tensor<type, 4>& query,
const Tensor<type, 4>& key,
Tensor<type, 4>& attention_scores) const
Expand All @@ -446,16 +425,11 @@ void MultiheadAttentionLayer::compute_attention_scores(const Tensor<type, 4>& qu

if(use_causal_mask)
apply_causal_mask(attention_scores);
/*
* // @todo make sure about this
softmax(attention_scores, attention_weights);
*/

softmax(attention_scores);
}



void MultiheadAttentionLayer::compute_attention_outputs(const Tensor<type, 4>& value,
const Tensor<type, 4>& attention_weights,
Tensor<type, 4>& attention_outputs) const
Expand Down
2 changes: 1 addition & 1 deletion opennn/neural_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ Index NeuralNetwork::calculate_image_output(const filesystem::path& image_path)

const Index pixels_number = height * width * image_channels;

#pragma omp parallel for
#pragma omp parallel for
for (Index j = 0; j < pixels_number; j++)
input_data(j) = resized_image_data(j);

Expand Down
2 changes: 1 addition & 1 deletion opennn/neural_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class NeuralNetwork

Tensor<type, 2> calculate_directional_inputs(const Index&, const Tensor<type, 1>&, const type&, const type&, const Index& = 101) const;

Index calculate_image_output(const string&);
Index calculate_image_output(const filesystem::path&);

// Serialization

Expand Down
4 changes: 2 additions & 2 deletions opennn/stochastic_gradient_descent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ TrainingResults StochasticGradientDescent::perform_training()
const vector<Index> input_variable_indices = data_set->get_variable_indices(DataSet::VariableUse::Input);
const vector<Index> target_variable_indices = data_set->get_variable_indices(DataSet::VariableUse::Target);

const vector<Index> context_variable_indices = is_instance_of<LanguageDataSet>(data_set)
const vector<Index> decoder_variable_indices = is_instance_of<LanguageDataSet>(data_set)
? static_cast<LanguageDataSet*>(data_set)->get_variable_indices(DataSet::VariableUse::Decoder)
: vector<Index>();

Expand Down Expand Up @@ -305,7 +305,7 @@ TrainingResults StochasticGradientDescent::perform_training()
training_batch.fill(training_batches[iteration],
input_variable_indices,
target_variable_indices,
context_variable_indices);
decoder_variable_indices);

// Neural network

Expand Down
67 changes: 0 additions & 67 deletions opennn/strings_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,34 +106,6 @@ Tensor<type, 1> to_type_vector(const string& text, const string& separator)
}


Tensor<Index, 1> to_index_vector(const string& text, const string& separator)
{
const vector<string> tokens = get_tokens(text, separator);

const Index tokens_size = tokens.size();

Tensor<Index, 1> index_vector(tokens_size);

for(Index i = 0; i < tokens_size; i++)
{
try
{
stringstream buffer;

buffer << tokens[i];

index_vector(i) = Index(stoi(buffer.str()));
}
catch(const exception&)
{
index_vector(i) = Index(-1);
}
}

return index_vector;
}


vector<string> get_unique(const vector<string>& tokens)
{
string result;
Expand All @@ -146,21 +118,6 @@ vector<string> get_unique(const vector<string>& tokens)
}


Tensor<Index, 1> count_unique(const vector<string>& tokens)
{
const vector<string> unique_elements = get_unique(tokens);

const Index unique_size = unique_elements.size();

Tensor<Index, 1> unique_count(unique_size);

for(Index i = 0; i < unique_size; i++)
unique_count(i) = Index(count(tokens.data(), tokens.data() + tokens.size(), unique_elements[i]));

return unique_count;
}


bool is_numeric_string(const string& text)
{
try
Expand Down Expand Up @@ -527,16 +484,6 @@ bool has_numbers(const vector<string>& string_list)
}


bool is_not_numeric(const vector<string>& string_list)
{
for(size_t i = 0; i < string_list.size(); i++)
if(is_numeric_string(string_list[i]))
return false;

return true;
}


void delete_non_printable_chars(string& text)
{
typedef ctype<wchar_t> ctype;
Expand Down Expand Up @@ -744,20 +691,6 @@ void to_lower(vector<vector<string>>& text)
}


vector<vector<string>> get_tokens(const vector<string>& documents, const string& separator)
{
const Index documents_number = documents.size();

vector<vector<string>> tokens(documents_number);

for(Index i = 0; i < documents_number-1; i++)
tokens[i] = get_tokens(documents[i], separator);

return tokens;
}



void delete_extra_spaces(vector<string>& documents)
{
vector<string> new_documents(documents);
Expand Down
Loading

0 comments on commit 2342a95

Please sign in to comment.