coreml - CoreML:无法执行矩阵乘法
问题描述
我正在尝试使用 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")
解决方案
尝试这个:
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,但至少它现在是一个有效的模型。:-)
推荐阅读
- python - results.txt 文件保存在哪里?
- mkdocs - 如何同时使用 codehilite 和美人鱼?
- java - 从嵌套的 RecyclerView 中删除项目
- shopify - 订单 9999 以外的 Shopify 预览通知
- ruby-on-rails - 如何根据提供的密钥正确映射 ruby 哈希
- d3.js - 让我们制作一个 TopoJSON 地图并使用 D3.js 查看它
- debugging - Intellij 调试:如何检查实现 Map 的类的成员变量?
- c# - 当我针对多个版本的 .net 时,如何调试我的代码?
- javascript - Fetch API:字符串开头的无效字符
- java - 一些 N 皇后问题场景的回溯失败