CoreML:无法执行矩阵乘法

时间:2019-08-13 13:32:27

标签: coreml coremltools

我正在尝试使用NetworkBuilder在我的网络中实现矩阵乘法运算。 我希望将两个张量分别为(20x50)和(50x100)的张量相乘,以获得一个张量为(20x100)的张量。

我该怎么做?我尝试使用add_batched_mat_mul,但是在coremltools == 3.0b3和coremltools == 3.0b4

上出现以下错误

如何使用上述张量尺寸执行matmul操作?

coremltools == 3.0b3上的错误

RuntimeWarning: You will not be able to run predict() on this Core ML model. Underlying exception message was: Error compiling model: "Error reading protobuf spec. validator error: Unsupported layer type (CoreML.Specification.NeuralNetworkLayer) for layer 'matmul'.".
  RuntimeWarning)

coremltools == 3.0b4上的错误

  File "test2.py", line 28, in <module>
    out = model.predict({"matrix_left": np.zeros((20, 50, 1))})
  File "python2.7/site-packages/coremltools/models/model.py", line 345, in predict
    raise Exception('Unable to load CoreML.framework. Cannot make predictions.')
Exception: Unable to load CoreML.framework. Cannot make predictions.
exception loading model proxy: dlopen(python2.7/site-packages/coremltools/libcoremlpython.so, 2): Symbol not found: _objc_opt_class
  Referenced from: python2.7/site-packages/coremltools/libcoremlpython.so (which was built for Mac OS X 10.15)
  Expected in: /usr/lib/libobjc.A.dylib
 in python2.7/site-packages/coremltools/libcoremlpython.so

使用的脚本:

import coremltools.models.datatypes as datatypes
from coremltools.models.neural_network import NeuralNetworkBuilder
from coremltools.models import MLModel

import numpy as np

model_input_features = [
    ("matrix_left", datatypes.Array(20, 50, 1)),
]
model_output_features = [
    ("y", datatypes.Array(20, 100, 1)),
]

builder = NeuralNetworkBuilder(input_features=model_input_features, output_features=model_output_features)

np.random.seed(42)

matrix_right = np.random.rand(50, 100, 1)
builder.add_load_constant(name="matrix_right", output_name="y",
                          constant_value=matrix_right, shape=(50, 100, 1))


builder.add_batched_mat_mul(name="matmul", input_names=["matrix_left", "matrix_right"],
                            output_name="y")

model = MLModel(builder.spec)
out = model.predict({"matrix_left": np.zeros((20, 50, 1))})
y = out["y"]
print(y)
print(y.shape)

我也尝试通过add_elementwise使用点积,但出现以下错误:

RuntimeWarning: You will not be able to run predict() on this Core ML model. 
Underlying exception message was: Error compiling model: "compiler error:  Dot product layer: 'matmul': 
height dimension of the input blob must be 1.".

脚本:

matrix_right = np.random.rand(50, 100, 1)
builder.add_load_constant(name="matrix_right", output_name="matrix_right", constant_value=matrix_right, shape=(50, 100, 1))
builder.add_elementwise("matmul", input_names=["matrix_left", "matrix_right"], output_name="y", mode="DOT")

1 个答案:

答案 0 :(得分:1)

尝试一下:

builder.add_load_constant(name="matrix_right", output_name="matrix_right",
                      constant_value=matrix_right, shape=(50, 100, 1))

请注意,输出名称现在是"matrix_right",而不是"y"

该模型仍不能与3.0b3或3.0b4一起使用,但至少现在是有效模型。 :-)