簡単 0件解決済み 正答率0%

Matrix Multiplication

Multiply two matrices, the operation behind every layer of a neural network.

このチャレンジで学べること

  • Multiply matrices row by column
  • Check that shapes are compatible
  • See why layers are matrix products

問題

翻訳を表示

Neural network layers, attention and most of machine learning boil down to matrix multiplication. For an n × k matrix A and a k × m matrix B, the product C = A × B is n × m, where each entry is the dot product of a row of A and a column of B:

C[i][j] = A[i][0] × B[0][j] + A[i][1] × B[1][j] + … + A[i][k-1] × B[k-1][j]

Implement matmul(a, b).

Input

a: An n × k matrix.

b: A k × m matrix (its number of rows equals the number of columns of a).

Output

Return the n × m product matrix.

制約

1 ≤ n, k, m ≤ 50

-100 ≤ each value ≤ 100

例

例 1
入力:
a = [
    [1.0, 2.0],
    [3.0, 4.0]
]
b = [
    [5.0, 6.0],
    [7.0, 8.0]
]

期待される出力:
expected = [
    [19.0, 22.0],
    [43.0, 50.0]
]
入力:
std::vector<std::vector<double>> a = {
    {1.0, 2.0},
    {3.0, 4.0}
};
std::vector<std::vector<double>> b = {
    {5.0, 6.0},
    {7.0, 8.0}
};

期待される出力:
std::vector<std::vector<double>> expected = {
    {19.0, 22.0},
    {43.0, 50.0}
};
入力:
double[][] a = new double[][] {
    new double[] {1.0, 2.0},
    new double[] {3.0, 4.0}
};
double[][] b = new double[][] {
    new double[] {5.0, 6.0},
    new double[] {7.0, 8.0}
};

期待される出力:
double[][] expected = new double[][] {
    new double[] {19.0, 22.0},
    new double[] {43.0, 50.0}
};

解説

C[0][0] = 1×5 + 2×7 = 19, C[0][1] = 1×6 + 2×8 = 22, C[1][0] = 3×5 + 4×7 = 43 and C[1][1] = 3×6 + 4×8 = 50.

例 2
入力:
a = [
    [1.0, 0.0, 2.0]
]
b = [
    [3.0],
    [4.0],
    [5.0]
]

期待される出力:
expected = [
    [13.0]
]
入力:
std::vector<std::vector<double>> a = {
    {1.0, 0.0, 2.0}
};
std::vector<std::vector<double>> b = {
    {3.0},
    {4.0},
    {5.0}
};

期待される出力:
std::vector<std::vector<double>> expected = {
    {13.0}
};
入力:
double[][] a = new double[][] {
    new double[] {1.0, 0.0, 2.0}
};
double[][] b = new double[][] {
    new double[] {3.0},
    new double[] {4.0},
    new double[] {5.0}
};

期待される出力:
double[][] expected = new double[][] {
    new double[] {13.0}
};

解説

A 1 × 3 matrix times a 3 × 1 matrix gives a 1 × 1 matrix: 1×3 + 0×4 + 2×5 = 13.

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。