alldata = load('alldata.txt');
alldata = alldata(:,:);
|
clc;
clear;
%導入300組數據
alldata = load('alldata.txt');
alldata = alldata(:,:);
%輸入輸出數據
input = alldata(:,2:33);
outputtemp = alldata(:,1);
%輸出數據須要處理一下
output = zeros(300,2);%預先分配內存
for i=1:300
switch outputtemp(i)
case 0
output(i,:) = [1 0];%意思是若是數據結果是0,則輸出層的狀態是[1 0],或者用第一個輸出節點表示
case 1 %能直接識別帶小數位的數據
output(i,:) = [0 1];
end
end
%從中隨機抽取280組數據做爲訓練數據,20組數據做爲預測數據
k = rand(1,300);
[m,n] = sort(k);
input_train = input(n(1:280),:)';
output_train = output(n(1:280),:)';
input_test = input(n(281:300),:)';
output_test = output(n(281:300),:)';
%輸入輸出數據進行歸一化處理
[inputn,inputps] = mapminmax(input_train);
[outputn,outputps] = mapminmax(output_train);
%網絡結構構建32-6-2
net=newff(inputn,outputn,6);
%網絡參數配置(迭代次數,學習率,目標)
net.trainParam.epochs=100;
net.trainParam.lr=0.1;
net.trainParam.goal=0.0004;
%網絡訓練
net=train(net,inputn,outputn);
%BP網絡預測
%預測數據歸一化
inputn_test=mapminmax('apply',input_test,inputps);
%網絡預測輸出
an=sim(net,inputn_test);
%網絡輸出反歸一化
BPoutput=mapminmax('reverse',an,outputps);
%結果分析
figure(1)
plot(BPoutput,':og')
hold on
plot(output_test,'-*');
legend('預測輸出','指望輸出')
title('BP網絡預測輸出','fontsize',12)
ylabel('函數輸出','fontsize',12)
xlabel('樣本','fontsize',12)
%預測偏差
error=BPoutput-output_test;
figure(2)
plot(error,'-*')
title('BP網絡預測偏差','fontsize',12)
ylabel('偏差','fontsize',12)
xlabel('樣本','fontsize',12)
figure(3)
plot((output_test-BPoutput)./BPoutput,'-*');
title('神經網絡預測偏差百分比')
errorsum = sum(abs(error))
|
MATLAB官方論壇中看到這樣一個回答:
You will not be able to compile any function which trains the network (like ADAPT). Though the link does not explicitly list these funcions (like ADAPT), they fall under the 'All other command line functionality'.
However, you can deploy a M function code which uses a pre-trained network. I believe the SIM function will deploy fine.
The workflow I see is:
1. In MATLAB, train you network using test input/output
2. Save the network (mat file?)
3. Create a deployable function which then uses the pretrained network for new data. The network itself would not change/adapt/train in this function
4. Compile and deploy the above function
大意:MATLAB compile不支持對神經網絡工具箱中涉及到訓練的函數命令進行編譯,只能編譯那些用在已訓練好的網絡上的函數命令
|
function
output = annforecastthi(input_test)
%ANNFORECAST
% 輸入長度爲32的行矩陣,輸出爲1或2
A = load(
'annnet.mat'
);
B = fieldnames(A);
net = A.(B{1});
net = network(net);
C = load(
'anninputps.mat'
);
D = fieldnames(C);
inputps = C.(D{1});
E = load(
'annoutputps.mat'
);
F = fieldnames(E);
outputps = E.(F{1});
%BP網絡預測
%預測數據歸一化
inputn_test=mapminmax(
'apply'
,input_test',inputps);
%網絡預測輸出
an=sim(net,inputn_test);
%網絡輸出反歸一化
BPoutput=mapminmax(
'reverse'
,an,outputps);
%結果分析
%根據網絡輸出找出數據屬於哪類
output=find(BPoutput(:,1)==max(BPoutput(:,1)));
end
|
public class
TestMatlab {
public static void main(String[] args){ try {
ANNMatlab annMatlab =
new ANNMatlab();
double
[] array = {
74.5
,
75.5
,
83.3
,
93.4
,
93.9
,
90.1
,
86.1
,... };
Object result[]=annMatlab.annforecastthi(
1
,array);
//
函數第一個參數是輸出數據個數,以後的就是
是輸入數據。
System.
out
.println(
"result=="
+result[
0
]);
} catch (MWException e) { e.printStackTrace(); }
}
}
|
The .jar files that Compiler SDK generates cannot be run on Android. The .jar has a small interface to call upon the machine code library that is MCR, and otherwise the .jar contains encrypted data files. The encrypted data files are the "compiled" .m code, which is not compiled to java, but rather to MATLAB's internal threaded interpreter. MCR decrypts the encrypted pcode'd .m files and uses them as data to be processed by the threaded interpreter. The encrypted data files themselves can be fairly operating system independent, but you need MCR to interpret them, and MCR is in x86 or x64 machine code for all versions of MATLAB since about R2009a. Android does not run on x86 or x64: Android runs on ARM processors (or possibly PowerPC as well, I am not certain.)
In short, you cannot use Compiler SDK to generate for anything useful on Android.
At this time, the only way to deploy for Android is to use Simulink with Target set to Android. You can have your Simulink blocks call a MATLAB Function block which is specialized MATLAB code (that has to be careful about how it allocates memory.) There is not much graphics you can do with this mechanism but it is the best that is available at this time.
I happened to look last night at some of the blocks available for deployment to a couple of the Android Galaxy devices. There is a block which accepts R, G, and B signals and displays the result as the screen. It would require computing the entire screen, I suspect. Some of the routines in the Vision toolbox help in that.
大意:matlab生成的jar包不能直接在android上運行,還須要用一個叫MCR的東西進行解碼(The MATLAB Compiler Runtime (MCR) has the same System requirements as MATLAB. See System Requirements - Android is not one of them.它是matlab代碼解析器),而MCR目前只能運行在X86或者X64的CPU上,但由於android是運行在ARM CPU上,因此是不可能使用的。
而後它說:此時,部署Android的惟一方法是使用Simulink與目標設定爲Android。你能夠有你的Simulink模塊調用MATLAB函數的MATLAB代碼塊是專業(需注意如何分配內存)沒有多大的圖形能夠作這種機制,但它是最好的,此時可用。
後邊這個就看不懂了:我碰巧看的最後一個晚上的一些塊可供部署到一對夫婦的安卓銀河設備。有一個塊,它接受R,G和B信號,並顯示屏幕的結果。它將須要計算整個屏幕,我懷疑。一些例程中的「視覺工具箱」中的幫助。
I have not read about what can be done in R2016a. The situation as of R2015b was that in order to generate code for Android from MATLAB, what you needed to do was include the MATLAB code in a MATLAB Function block in Simulink and tell Simulink to target Android; there was no direct path for MATLAB to Android.
大意:你須要作的是包括在Simulink MATLAB功能塊的matlab代碼告訴Simulink目標Android
However, you should be able to use the MATLAB Coder product to generate a standalone C/C++ library from your MATLAB code and then invoke that from your Android application, possibly using JNI.
大意:你可使用一個叫MATLAB Coder的工具來生成能夠獨立運行的C/C++庫,他們能夠在android中經過JNI來直接運行
MATLAB code that is put into a Simulink MATLAB Function Block (with appropriate adjustments made) can be generated for Android target using Simulink. There are a bunch of restrictions on this, but I think it can access the Android Sensor information for supported devices (Galaxy S4, Galaxy Note 2)
大意:matlab代碼,把MATLAB的一個Simulink功能塊(適當調整)能夠爲Android使用Simulink生成目標。有一些限制,但我認爲它能夠訪問支持的設備Android傳感器信息(Galaxy S4,Galaxy Note 2)
From MATLAB, you can communicate with Android camera and with Android sensors (at least for some models), but it is not possible to generate code for Android. You need to use Simulink to generate code for Android.
MATLAB Coder does not know about Android, so MATLAB Coder cannot generate Android calls for user interaction, networking, graphics, and so on. MATLAB Coder can generally generate C or C++, but unless it has been given information about the target system, MATLAB Coder has a library of calls that is not even as complete as the Standard C Library. This is not enough to create an Android "app"; at most it is enough to create a utility program.
Simulink does know how to target Android, so if you have a Simulink model and use the Android-specific blocks, Simulink can create apps. See http://www.mathworks.com/hardware-support/android-programming-simulink.html
大意:能夠用Simulink
.so files are Linux Shared Object libraries. Those .so files are only available for Intel x86 (32 bit) and x64 (64 bit) instruction set (including AMD CPUs that implement those.) They are not available for ARM or other instruction architectures.
大意:.so文件自己也只能再X86的系統上使用,而不能在ARM上使用(這個存疑)
The .jar file requires the MATLAB Compiler Runtime (a freely redistributable component that you get with MATLAB Compiler and MATLAB Builder products) to be present. The MCR has a much larger footprint than is suitable for the typical Android device (it's like a copy of MATLAB itself, without the user interface).
You could think about either
1)Running your MATLAB .jar file remotely on a server, and having your Android application connect to it, or
2)Instead of using MATLAB Compiler and Builder products, use
MATLAB Coder, which will convert a subset of the MATLAB language directly into C code. This C code doesn't require the MCR, and could be compiled to run directly on Android. Make sure your MATLAB algorithm falls within, or can be expressed in, the appropriate subset of the MATLAB language.
大意:前面都同樣不說了,後邊提了兩個解決辦法:1)不要再app上用Matlab代碼,轉移到服務器上;2)用
MATLAB Coder編譯成能夠不須要MCR支持才能運行的C/C++文件
A new feature in Matlab 2014a:
http://www.mathworks.com/help/simulink/samsung-galaxy-android-devices.html
You can now directly install (limited set of) models to Samsung Android devices, and this should work actually on any Android device.
大意:2014的新版本中,你能夠直接把matlab模型安裝在三星安卓設備上,實際上就能夠安裝在全部安卓設備上
|