gpt4 book ai didi

java - 神经网络没有主动响应已学习的内容

转载 作者:行者123 更新时间:2023-12-02 12:04:28 29 4
gpt4 key购买 nike

这个神经网络的界面被构建为一个不和谐的机器人。您可以在 https://discord.gg/N5Bke3z 找到它。如果重置命令在学习命令之前完成,则它可以正常工作。学习命令没有按照我的消息监听器中的指示进行响应。检查覆盖范围后,我注意到它跳过了不应该发生的事情,例如 if else 语句。我使用的两个api是javacord https://github.com/BtoBastian/Javacord ,以及我用于处理数据的个人 api https://github.com/NicksWorld/Networking-DataTypes 。我的代码将缺少不和谐机器人 token ,但我向您保证 token 不是问题。除了 javacord 的启动日志之外,控制台日志为空:

Oct 29, 2017 10:33:48 AM de.btobastian.javacord.utils.JavacordLogger info INFO: No SLF4J compatible logger was found. Using default javacord implementation!

如果我能得到任何帮助,我将不胜感激。我的代码位于 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();
}
});
}
}

消息.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");
}
}
}

网络.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;
}
}

关于java - 神经网络没有主动响应已学习的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47001640/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com