【问题标题】:cell2mat not supported for C code generation in MATLAB CoderMATLAB Coder 中的 C 代码生成不支持 cell2mat
【发布时间】:2018-06-01 09:46:35
【问题描述】:

我正在尝试使用 MATLAB Codel APP 将用 MATLAB 编写的神经网络函数转换为 C 函数。但是当我尝试转换时,我得到了

代码生成不支持cell2mat 代码生成不支持arrayfun

如何替换这些函数?

我的代码如下所示。输入 X 是一个 1x2500 矩阵,output 是一个 1x6 矩阵。

完整代码可在此链接boneGrwNN中获得

function Y = boneGrwNN(X)
x1_step1.ymin = -1;    
% ===== SIMULATION ========    
% Format Input Arguments
isCellX = iscell(X);
if ~isCellX, X = {X}; end;    
% Dimensions
TS = size(X,2); % timesteps
if ~isempty(X)
    Q = size(X{1},1); % samples/series
else
    Q = 0;
end    
% Allocate Outputs
Y = cell(1,TS);    
% Time loop
for ts=1:TS        
    % Input 1
    X{1,ts} = X{1,ts}';
    Xp1 = mapminmax_apply(X{1,ts},x1_step1);        
    % Layer 1
    a1 = tansig_apply(repmat(b1,1,Q) + IW1_1*Xp1);        
    % Layer 2
    a2 = softmax_apply(repmat(b2,1,Q) + LW2_1*a1);        
    % Output 1
    Y{1,ts} = a2;
    Y{1,ts} = Y{1,ts}';
end   
% Format Output Arguments
if ~isCellX, Y = cell2mat(Y); end
end

% ===== MODULE FUNCTIONS ========    
% Map Minimum and Maximum Input Processing Function
function y = mapminmax_apply(x,settings)
y = bsxfun(@minus,x,settings.xoffset);
y = bsxfun(@times,y,settings.gain);
y = bsxfun(@plus,y,settings.ymin);
end    
% Competitive Soft Transfer Function
function a = softmax_apply(n,~)
if isa(n,'gpuArray')
    a = iSoftmaxApplyGPU(n);
else
    a = iSoftmaxApplyCPU(n);
end
end
function a = iSoftmaxApplyCPU(n)
nmax = max(n,[],1);
n = bsxfun(@minus,n,nmax);
numerator = exp(n);
denominator = sum(numerator,1);
denominator(denominator == 0) = 1;
a = bsxfun(@rdivide,numerator,denominator);
end
function a = iSoftmaxApplyGPU(n)
nmax = max(n,[],1);
numerator = arrayfun(@iSoftmaxApplyGPUHelper1,n,nmax);
denominator = sum(numerator,1);
a = arrayfun(@iSoftmaxApplyGPUHelper2,numerator,denominator);
end
function numerator = iSoftmaxApplyGPUHelper1(n,nmax)
numerator = exp(n - nmax);
end
function a = iSoftmaxApplyGPUHelper2(numerator,denominator)
if (denominator == 0)
    a = numerator;
else
    a = numerator ./ denominator;
end
end

% Sigmoid Symmetric Transfer Function
function a = tansig_apply(n,~)
a = 2 ./ (1 + exp(-2*n)) - 1;
end

如何为这个函数生成 C 代码?

【问题讨论】:

  • 如果您了解cell2mat 的作用,并且您了解您传递给它的输入,那么您为什么不能在不使用cell2mat 本身的情况下手动复制该行为?

标签: matlab code-generation matlab-deployment


【解决方案1】:

您只能从functions supported by code generation 生成独立的C 代码。如果你真的需要C代码,因为你的环境不支持Matlab,那么你必须手动转换不支持的函数或使用coder.ceval,以便使用实现相同功能的外部C代码。

在您的示例中,您可以将 arrayfun 调用替换为传统的 for-loops。为了实现您自己的cell2mat 代码,只需输入open cell2mat 即可查看函数的源代码并尝试在您的代码中复制其逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多