Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<vector<double>> transpose(const vector<vector<double>>& matrix){
- unsigned short rows = matrix.size();
- unsigned short cols = matrix[0].size();
- vector<vector<double>> transposed(cols, vector<double>(rows));
- for(unsigned char i = 0; i < rows; ++i){
- for(char j = 0; j < cols; ++j){
- transposed[j][i] = matrix[i][j];
- }
- }
- return transposed;
- }
- vector<vector<double>> dot_product(const vector<vector<double>>& inputs, const vector<vector<double>>& weights){
- short input_rows = inputs.size();
- short input_cols = inputs[0].size();
- short weight_rows = weights.size();
- short weight_cols = weights[0].size();
- vector<vector<double>> transposed_weights = transpose(weights);
- if(input_cols != transposed_weights.size()){
- printf("Shape error\n");
- exit(1);
- }
- vector<vector<double>> result(input_rows, vector<double>(weight_cols, 0.0));
- for(char i = 0; i < input_rows; ++i){
- for(char j = 0; j < weight_rows; ++j){
- for(char k = 0; k < input_cols; ++k){
- if(k < input_cols && j < weight_cols){
- result[i][j] += inputs[i][k] * transposed_weights [j][k];
- }
- }
- }
- }
- return result;
- }
- int main(){
- vector<vector<double>> inputs = {
- {1., 2., 3., 2.5},
- {2. , 5. , -1., 2.},
- {-1.5 , 2.7 , 3.3, -0.8}
- };
- vector<vector<double>> weights = {
- {0.2, 0.8, -0.5, 1.},
- {0.5, -0.91, 0.26, -0.5},
- {-0.26, -0.27, 0.17, 0.87}
- };
- vector<double> biases = {2, 3, 0.5};
- vector<vector<double>> result = dot_product(inputs, weights);
- for(char i = 0; i < result.size(); ++i){
- for(char j = 0; j < result.size(); ++j){
- result[i][j] += biases[j];
- }
- }
- printf("Should be right: %f \n", result[2][3]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement