Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Matrix Multiplication

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

1 ≤ n, k, m ≤ 50

-100 ≤ each value ≤ 100

Ví dụ

Ví dụ 1
Đầu vào:
a = [
    [1.0, 2.0],
    [3.0, 4.0]
]
b = [
    [5.0, 6.0],
    [7.0, 8.0]
]

Kết quả mong đợi:
expected = [
    [19.0, 22.0],
    [43.0, 50.0]
]
Đầu vào:
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}
};

Kết quả mong đợi:
std::vector<std::vector<double>> expected = {
    {19.0, 22.0},
    {43.0, 50.0}
};
Đầu vào:
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}
};

Kết quả mong đợi:
double[][] expected = new double[][] {
    new double[] {19.0, 22.0},
    new double[] {43.0, 50.0}
};

Giải thích

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.

Ví dụ 2
Đầu vào:
a = [
    [1.0, 0.0, 2.0]
]
b = [
    [3.0],
    [4.0],
    [5.0]
]

Kết quả mong đợi:
expected = [
    [13.0]
]
Đầu vào:
std::vector<std::vector<double>> a = {
    {1.0, 0.0, 2.0}
};
std::vector<std::vector<double>> b = {
    {3.0},
    {4.0},
    {5.0}
};

Kết quả mong đợi:
std::vector<std::vector<double>> expected = {
    {13.0}
};
Đầu vào:
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}
};

Kết quả mong đợi:
double[][] expected = new double[][] {
    new double[] {13.0}
};

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Each output cell comes from one row of the first matrix and one column of the second matrix.

Gợi ý 2

Accumulate products across the shared inner dimension, then place the result in the matching output position.

Gợi ý 3

Be careful with the shapes: the first matrix’s column count must match the second matrix’s row count.

Độ phức tạp mong đợi

O(n k m) time, O(n m) extra space

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.