我试图在java中使用neuroph源代码来创建一个android神经网络但它崩溃了。 源链接,在页面末尾: http://neuroph.sourceforge.net/tutorials/MultiLayerPerceptron.html 图书馆地址: http://neuroph.sourceforge.net/download.html
我的活动课程:
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);
}
}
}
我正在使用适配器在listview中显示输入和输出:
package com.example.test;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyAdapter extends android.widget.BaseAdapter {
private LayoutInflater myInflater;
private List<Row> list;
ViewHolder holder;
public void setData(List<Row> list)
{ this.list = list; }
public MyAdapter(Context context)
{myInflater = LayoutInflater.from(context);}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.rowneuroph, null);
holder = new ViewHolder();
holder.Input = (TextView) convertView.findViewById(R.id.row_txt_Input);
holder.Output = (TextView) convertView.findViewById(R.id.row_txt_Output);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.Input.setText (list.get(position).GetInput());
holder.Output.setText(list.get(position).GetOutput());
}
catch (Exception e)
{
//MainActivity.Alert(null, e.toString());
}
return convertView;
}
static class ViewHolder {
TextView Input;
TextView Output;
}
}
我的“行”类:
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;}
}
我的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>
我的活动xml文件:
<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)
任何人都可以帮助我吗?
最好的问候。