这个神经网络的界面构建为不和谐的bot。您可以在https://discord.gg/N5Bke3z上找到它。如果在学习命令之前完成了reset命令,则它可以完美地工作。在我的消息侦听器中,learn命令没有响应。在检查了覆盖率之后,我注意到它正在跳过不应该发生的事情,例如if else语句。我使用的两个API是javacord https://github.com/BtoBastian/Javacord和用于处理数据https://github.com/NicksWorld/Networking-DataTypes的个人api。我的代码将丢失不和谐的bot令牌,但是,我向您保证,这不是问题。控制台日志为空,但javacord的启动日志除外:
2017年10月29日上午10:33:48 de.btobastian.javacord.utils.JavacordLogger
信息信息:未找到兼容SLF4J的记录器。使用默认
javacord实现!
我将不胜感激。我的代码在https://github.com/NicksWorld/Java-neural-network处,如下所示:
Bot.java:
package Discord;
import com.google.common.util.concurrent.FutureCallback;
import Discord.message.Message;
import de.btobastian.javacord.DiscordAPI;
import de.btobastian.javacord.Javacord;
public class Bot {
public static void main(String[] args) {
//get login info
DiscordAPI api = Javacord.getApi("*****************", true);
//login
api.connect(new FutureCallback<DiscordAPI>() {
@Override
public void onSuccess(final DiscordAPI api) {
//set game and start listener
api.setGame("Learning through Network001's algorithms");
api.registerListener(new Message());
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
});
}
}
Message.java:
package Discord.message;
import de.btobastian.javacord.DiscordAPI;
import de.btobastian.javacord.listener.message.MessageCreateListener;
import me.NicksWorld.obj.DataCollection;
import me.NicksWorld.obj.IntegerRow;
public class Message implements MessageCreateListener{
Network network = new Network();
public IntegerRow StringToRow(String in) {
String arr = in;
String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "").split(",");
IntegerRow results = new IntegerRow(items.length);
for (int i = 0; i < items.length; i++) {
try {
results.set(i, Integer.parseInt(items[i]));
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
}
return results;
}
@Override
public void onMessageCreate(DiscordAPI api, de.btobastian.javacord.entities.message.Message message) {
//Stop interaction from bots
if(message.getAuthor().isBot()) {
return;
}
//register help command
if (message.getContent().startsWith("!help")) {
message.reply(message.getAuthor().getMentionTag() + "\n!help - Shows this list\n!learn - learns from a dataset in the form of an array ex. [1,2,3,4,5], that array tells it that it has the numbers 1, 2, 3, and 4. It also tells it that the result should be 5\n!find - takes an input of 4 numbers in an array ex. [1,2,3,4] so it can find an output based on conjectures from the training data");
} else if(message.getContent().startsWith("!learn")) {
if (network.learn(StringToRow(message.getContent().substring(7)))) {
message.reply("Succes!");
} else {
message.reply("Fail :C");
}
} else if(message.getContent().startsWith("!find")) {
} else if(message.getContent().startsWith("!reset")) {
network.TrainingData = new DataCollection();
message.reply("done");
}
}
}
Network.java:
package Discord.message;
import me.NicksWorld.obj.DataCollection;
import me.NicksWorld.obj.IntegerRow;
public class Network {
//Initialize collection of training data
public DataCollection TrainingData = new DataCollection();
//End initialize collection of training data
//Initialize result variable
public Double datasetResult = 0.0;
//End initialize result variable
//Initialize fails variable
public Integer fails = 0;
//End initialize fails variable
//Initialize done learning boolean
public boolean doneLearning = false;
//End initialize done learning boolean
//Initialize weights
//Initialize column 1's weight
public double ColumnWeight1 = Math.round(Math.random());
//Initialize column 2's weight
public double ColumnWeight2 = Math.round(Math.random());
//Initialize column 3's weight
public double ColumnWeight3 = Math.round(Math.random());
//Initialize column 4's weight
public double ColumnWeight4 = Math.round(Math.random());
//End initialize weights
//Function to check weights against all datasets
public boolean checkWeights() {
for (Integer indexOfTrainingData = 1; indexOfTrainingData <= TrainingData.get("integer").size(); indexOfTrainingData++) {
//Reset variables for data
datasetResult = 0.0;
IntegerRow rowVar = (IntegerRow)TrainingData.get("integer").get(indexOfTrainingData - 1);
//End reseting of variables
//loop through the row
for (Integer indexOfRow=1; indexOfRow <= 4; indexOfRow++) {
//Determine which weight to use per value
if (indexOfRow==1) {
datasetResult += ColumnWeight1 * rowVar.get().get(0);
} else if (indexOfRow == 2) {
datasetResult += ColumnWeight2 * rowVar.get().get(1);
} else if (indexOfRow == 3) {
datasetResult += ColumnWeight3 * rowVar.get().get(2);
} else if (indexOfRow == 4) {
datasetResult += ColumnWeight4 * rowVar.get().get(3);
}
}
if (datasetResult == rowVar.get().get(4).intValue()) {
} else {
return false;
}
}
return true;
}
//Function to learn
public Boolean learn(IntegerRow ToLearn) {
//if(ToLearn.get().size()!=4) return false;
//Add to training data list
TrainingData.add(ToLearn);
//loop through the training data
fails = 0;
for (Integer indexOfTrainingData = 1; indexOfTrainingData <= TrainingData.get("int").size(); indexOfTrainingData++) {
//Reset variables for data
datasetResult = 0.0;
IntegerRow rowVar = (IntegerRow)TrainingData.get("int").get(indexOfTrainingData - 1);
doneLearning = false;
//End reseting of variables
//determine when the for loop is complete
while (!doneLearning) {
//loop through the row
for (Integer indexOfRow = 1; indexOfRow <= 4; indexOfRow++) {
//Determine which weight to use per value
if (indexOfRow==1) {
datasetResult += ColumnWeight1 * rowVar.get().get(0);
} else if (indexOfRow == 2) {
datasetResult += ColumnWeight2 * rowVar.get().get(1);
} else if (indexOfRow == 3) {
datasetResult += ColumnWeight3 * rowVar.get().get(2);
} else if (indexOfRow == 4) {
datasetResult += ColumnWeight4 * rowVar.get().get(3);
}
}
if (datasetResult == rowVar.get().get(4).intValue()) {
//check if successful with other datasets
if(checkWeights()) {
return true;
}
} else {
fails++;
//Re-randomize weights
ColumnWeight1 = Math.round(Math.random());
ColumnWeight2 = Math.round(Math.random());
ColumnWeight3 = Math.round(Math.random());
ColumnWeight4 = Math.round(Math.random());
}
}
return false;
}
return false;
}
}
编辑:
我发现错误计数未在正确的位置重置,从而修复了错误。
最佳答案
我已经进行了很多调试,并将问题定位到一行。在停止失败计数结束学习过程的时间,重置了失败计数。 Network.java的新代码是:
package Discord.message;
import de.btobastian.javacord.DiscordAPI;
import de.btobastian.javacord.entities.message.Message;
import me.NicksWorld.obj.DataCollection;
import me.NicksWorld.obj.IntegerRow;
public class Network {
//Initialize collection of training data
public DataCollection TrainingData = new DataCollection();
//End initialize collection of training data
//Initialize result variable
public Double datasetResult = 0.0;
//End initialize result variable
//Initialize fails variable
public Integer fails = 0;
//End initialize fails variable
//Initialize done learning boolean
public boolean doneLearning = false;
//End initialize done learning boolean
//Initialize weights
//Initialize column 1's weight
public double ColumnWeight1 = Math.round(Math.random());
//Initialize column 2's weight
public double ColumnWeight2 = Math.round(Math.random());
//Initialize column 3's weight
public double ColumnWeight3 = Math.round(Math.random());
//Initialize column 4's weight
public double ColumnWeight4 = Math.round(Math.random());
//End initialize weights
//Function to check weights against all datasets
public boolean checkWeights() {
for(Integer indexOfTrainingData=1;indexOfTrainingData<=TrainingData.get("int").size();indexOfTrainingData++) {
//Reset variables for data
datasetResult = 0.0;
IntegerRow rowVar = (IntegerRow) TrainingData.get("int").get(indexOfTrainingData - 1);
//End reseting of variables
//loop through the row
for(Integer indexOfRow=1;indexOfRow<=4;indexOfRow++) {
//Determine which weight to use per value
if(indexOfRow==1) {
datasetResult += ColumnWeight1*rowVar.get().get(0);
}else if(indexOfRow==2) {
datasetResult += ColumnWeight2*rowVar.get().get(1);
}else if(indexOfRow==3) {
datasetResult += ColumnWeight3*rowVar.get().get(2);
}else if(indexOfRow==4) {
datasetResult += ColumnWeight4*rowVar.get().get(3);
}
}
if(datasetResult==rowVar.get().get(4).intValue()) {
}else {
return false;
}
}
return true;
}
//Function to learn
public Boolean learn(IntegerRow ToLearn, Message message) {
if(ToLearn.get().size()!=5) {
return false;
}
//Add to training data list
TrainingData.add(ToLearn);
//loop through the training data
for(Integer indexOfTrainingData=1;indexOfTrainingData<=TrainingData.get("int").size();indexOfTrainingData++) {
//Reset variables for data
datasetResult = 0.0;
IntegerRow rowVar = (IntegerRow) TrainingData.get("int").get(indexOfTrainingData - 1);
doneLearning = false;
//End reseting of variables
//determine when the for loop is complete
while(doneLearning != true) {
//loop through the row
datasetResult = 0.0;
for(Integer indexOfRow=1;indexOfRow<=4;indexOfRow++) {
//Determine which weight to use per value
if(indexOfRow==1) {
datasetResult += ColumnWeight1*rowVar.get().get(0);
}else if(indexOfRow==2) {
datasetResult += ColumnWeight2*rowVar.get().get(1);
}else if(indexOfRow==3) {
datasetResult += ColumnWeight3*rowVar.get().get(2);
}else if(indexOfRow==4) {
datasetResult += ColumnWeight4*rowVar.get().get(3);
}
}
if(datasetResult==rowVar.get().get(4).intValue()) {
//check if successful with other datasets
Boolean test = checkWeights();
if(test==true) {
return true;
}
}else {
fails++;
if(fails>1000) {
TrainingData = new DataCollection(); return false;
}
if(fails == 50|fails == 100|fails == 150|fails == 200|fails == 250) message.reply("Working...");
//Re-randomize weights
ColumnWeight1 = Math.round(Math.random());
ColumnWeight2 = Math.round(Math.random());
ColumnWeight3 = Math.round(Math.random());
ColumnWeight4 = Math.round(Math.random());
}
}
TrainingData = new DataCollection();
return false;
}
TrainingData = new DataCollection();
return false;
}
//
public Double findResult = 0.0;
public Double find(IntegerRow in) {
findResult = 0.0;
for(Integer indexOfRow=1;indexOfRow<=4;indexOfRow++) {
//Determine which weight to use per value
if(indexOfRow==1) {
findResult += ColumnWeight1*in.get().get(0);
}else if(indexOfRow==2) {
findResult += ColumnWeight2*in.get().get(1);
}else if(indexOfRow==3) {
findResult += ColumnWeight3*in.get().get(2);
}else if(indexOfRow==4) {
findResult += ColumnWeight4*in.get().get(3);
}
}
return findResult;
}
}