Java 在android应用程序中使用neuroph源代码
我曾尝试使用java中的neuroph源代码创建一个android神经网络,但失败了。 源链接,在页面末尾: 图书馆地址: 我的活动课:Java 在android应用程序中使用neuroph源代码,java,android,Java,Android,我曾尝试使用java中的neuroph源代码创建一个android神经网络,但失败了。 源链接,在页面末尾: 图书馆地址: 我的活动课: package com.example.test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.util.Lo
package com.example.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.nnet.MultiLayerPerceptron;
import org.neuroph.util.TransferFunctionType;
public class TestNeuroph extends Activity {
MyAdapter adapter;
ListView listview;
List<Row> Rows = new ArrayList<Row>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_neuroph);
adapter = new MyAdapter(this);
listview = (ListView)findViewById(R.id.listView1);
Button btnProccess = (Button) findViewById(R.id.buttonProccess);
btnProccess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// create training set (logical XOR function)
DataSet trainingSet = new DataSet(2, 1);
trainingSet.addRow(new DataSetRow(new double[]{0, 0}, new double[]{0}));
trainingSet.addRow(new DataSetRow(new double[]{0, 1}, new double[]{1}));
trainingSet.addRow(new DataSetRow(new double[]{1, 0}, new double[]{1}));
trainingSet.addRow(new DataSetRow(new double[]{1, 1}, new double[]{0}));
// create multi layer perceptron
MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.TANH, 2, 3, 1);
// learn the training set
myMlPerceptron.learn(trainingSet);
// test perceptron
//Toast.makeText(getBaseContext(), "Testing trained neural network", Toast.LENGTH_LONG).show();
testNeuralNetwork(myMlPerceptron, trainingSet);
Log.d("Add_Food", "save trained neural network");
// save trained neural network
myMlPerceptron.save("myMlPerceptron.nnet");
// load saved neural network
NeuralNetwork loadedMlPerceptron = NeuralNetwork.createFromFile("myMlPerceptron.nnet");
// test loaded neural network
//Toast.makeText(getBaseContext(), "Testing loaded neural network", Toast.LENGTH_LONG).show();
testNeuralNetwork(loadedMlPerceptron, trainingSet);
adapter.setData(Rows);
listview.setAdapter(adapter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_test_neuroph, menu);
return true;
}
public void testNeuralNetwork(NeuralNetwork nnet, DataSet testSet) {
for(DataSetRow dataRow : testSet.getRows()) {
nnet.setInput(dataRow.getInput());
nnet.calculate();
double[ ] networkOutput = nnet.getOutput();
Row r = new Row();
r.setInput(Arrays.toString(dataRow.getInput()));
r.setOutput(Arrays.toString(networkOutput));
Rows.add(r);
}
}
}
my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
有人能帮我吗
向您致意。有吗?我将所有库逐一添加到我的android项目中
NoClassDefFoundError
表示运行时找不到库。这可能意味着您需要设置类路径。请参阅我在上面发布的链接。
package com.example.test;
public class Row {
private String _Input;
private String _Output;
public void setInput(String id)
{this._Input = id;}
public String GetInput()
{return this._Input;}
public void setOutput(String id)
{this._Output = id;}
public String GetOutput()
{return this._Output;}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestNeuroph" >
<Button
android:id="@+id/buttonProccess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Proccess" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_marginLeft="30dp"
android:text="Input:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="55dp"
android:layout_marginTop="14dp"
android:text="OutPut:" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonProccess"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1" >
</ListView>
</RelativeLayout>
09-21 11:50:40.341: E/AndroidRuntime(972): FATAL EXCEPTION: main
09-21 11:50:40.341: E/AndroidRuntime(972): java.lang.NoClassDefFoundError: org.neuroph.core.data.DataSet
09-21 11:50:40.341: E/AndroidRuntime(972): at com.example.test.TestNeuroph$1.onClick(TestNeuroph.java:38)
09-21 11:50:40.341: E/AndroidRuntime(972): at android.view.View.performClick(View.java:4202)
09-21 11:50:40.341: E/AndroidRuntime(972): at android.view.View$PerformClick.run(View.java:17340)
09-21 11:50:40.341: E/AndroidRuntime(972): at android.os.Handler.handleCallback(Handler.java:725)
09-21 11:50:40.341: E/AndroidRuntime(972): at android.os.Handler.dispatchMessage(Handler.java:92)
09-21 11:50:40.341: E/AndroidRuntime(972): at android.os.Looper.loop(Looper.java:137)
09-21 11:50:40.341: E/AndroidRuntime(972): at android.app.ActivityThread.main(ActivityThread.java:5039)
09-21 11:50:40.341: E/AndroidRuntime(972): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 11:50:40.341: E/AndroidRuntime(972): at java.lang.reflect.Method.invoke(Method.java:511)
09-21 11:50:40.341: E/AndroidRuntime(972): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-21 11:50:40.341: E/AndroidRuntime(972): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-21 11:50:40.341: E/AndroidRuntime(972): at dalvik.system.NativeStart.main(Native Method)