Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you make this codes rmse value lower. #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class

can you make this codes rmse value lower.

#include #include #include #include #include #include #include #include #include #include #include #include #include

using namespace std; class MF_ALS { public: int n_users_; int n_items_; int n_factors_; double lr_; double reg_; vector> user_factors_; vector> item_factors_; //constructor MF_ALS(int n_users, int n_items, int n_factors, double lr, double reg) :n_users_(n_users), n_items_(n_items), n_factors_(n_factors), lr_(lr), reg_(reg){ for (int i = 0; i < n_users_; i++) { vector user_factors; for (int j = 0; j < n_factors_; j++) { user_factors.push_back(rand() / (RAND_MAX + 1.0)); } user_factors_.push_back(user_factors); } for (int i = 0; i < n_items_; i++) { vector item_factors; for (int j = 0; j < n_factors_; j++) { item_factors.push_back(rand() / (RAND_MAX + 1.0)); } item_factors_.push_back(item_factors); } } //prediction double predict(int user, int item) { if (user >= n_users_ || item >= n_items_) { return 0.0; } double prediction = 0; for (int i = 0; i < n_factors_; i++) { prediction += user_factors_[user][i] * item_factors_[item][i]; } return prediction; }

void train(int user, int item, double rating) { if (user >= n_users_ || item >= n_items_) { return; } double prediction = predict(user, item); double error = rating - prediction; // Update user and item factors using gradient descent for (int i = 0; i < n_factors_; i++) { double user_factor = user_factors_[user][i]; double item_factor = item_factors_[item][i]; user_factors_[user][i] += lr_ * (error * item_factor - reg_ * user_factor); item_factors_[item][i] += lr_ * (error * user_factor - reg_ * item_factor); } }

}; void read_input(MF_ALS& model, const std::string& filename) { std::map> ratings; std::ifstream file(filename); std::string line; while (std::getline(file, line)) { // Parse user, item, and rating from line int user; int item; double rating; std::istringstream iss(line); iss >> user; iss.ignore(); // ignore the ',' character iss >> item; iss.ignore(); // ignore the ',' character iss >> rating; ratings[user][item] = rating; } file.close(); // Train model on each rating //double total_squared_error = 0.0; double total_absolute_error = 0.0; int num_ratings = 0; for (const auto& [user, user_ratings] : ratings) { for (const auto& [item, rating] : user_ratings) { model.train(user, item, rating); double error = abs(rating - model.predict(user, item)); total_absolute_error += error; num_ratings++; /*double prediction = model.predict(user, item); double squared_error = (prediction - rating) * (prediction - rating); total_squared_error += squared_error; num_ratings++;*/ } } double MAE = total_absolute_error / num_ratings; //double mse = total_absolute_error / num_ratings; double rmse = sqrt(MAE); std::cout << "RMSE: " << rmse << std::endl; } int main() { // Initialize model MF_ALS model(1000000, 100000, 15, 0.01, 0.001);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. Describe cultural differences that influence perception

Answered: 1 week ago