如何编写人工神经网络(井字游戏)?

发布于 2024-07-17 04:46:12 字数 1435 浏览 12 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

极度宠爱 2024-07-24 04:46:12

好吧,您有一个包含 18 个神经元的输入层和一个包含 1 个神经元的输出层。 没关系。 但是,您需要让神经网络有机会将输入关联起来。 为此,您至少需要一层中间层。 我建议在中间层使用 9 个神经元。 其中每个神经元都应连接到每个输入神经元,输出神经元应连接到每个中间神经元。 每个这样的连接都有一个权重,每个神经元都有一个激活级别。

然后,你一次一层地遍历所有神经元。 输入层刚刚被板状态激活。 对于所有其他神经元,您将遍历其所有各自的连接,并对连接的神经元的激活水平与连接权重的乘积求和。 最后,通过对该总和应用 sigmoid 函数来计算激活级别。

这就是工作原理。 现在,您需要训练该网络以获得更好的结果。 有几种算法可以实现这一点,您将需要进行一些谷歌搜索和阅读。 最后,当结果不够快时,您可能需要调整神经元和层的数量。 例如,您可以将输入层减少到 9 个神经元,并用 +1(代表 X)和 -1(代表 O)来激活它们。也许添加另一个中间层会产生更好的结果,或者增加一层的神经元数量。

Well, you have an input layer of 18 neurons, and an output layer of 1 neuron. That's OK. However, you need to give your neural net the opportunity to put the inputs into relation. For that, you need at least one intermediate layer. I would propose to use 9 neurons in the intermediate layer. Each of these should be connected to each input neuron, and the output neuron should be connected to each intermediate. Each such connection has a weight, and each neuron has an activation level.

Then, you go through all neurons, a layer at a time. The input layer is just activated with the board state. For all further neurons, you go through all its respective connections and sum over the product of the connected neuron's activation level and the weight of the connection. Finally, you calculate the activation level by applying a sigmoid function on this sum.

This is the working principle. Now, you need to train this net to get better results. There are several algorithms for this, you will have to do some googling and reading. Finally, you might want to adjust the number of neurons and layers when the results don't get convincing fast enough. For example, you could reduce the input layer to 9 neurons and activate them with +1 for an X and -1 for an O. Perhaps adding another intermediate layer yields better results, or increasing the number of neurons of a layer.

红衣飘飘貌似仙 2024-07-24 04:46:12

我不太明白你希望如何从一个输出神经元中获得对棋盘情况的有意义的总结。 我更倾向于考虑:

    I I I             O O O
    I I I      x      O O O
    I I I             O O O
9 input neurons  9 output neurons

在完全连接的网络中,即 81 个权重。 然后训练输出神经元以了解在该位置进行比赛的相对意愿。

I don't particularly understand how you expect to get a meaningful summary of the board situation out of one output neuron. I would more look at having:

    I I I             O O O
    I I I      x      O O O
    I I I             O O O
9 input neurons  9 output neurons

in a fully connected network, i.e. 81 weights. Then train the output neurons for the relative desirability of playing in that position.

薄荷梦 2024-07-24 04:46:12

看看我的 Tic 项目。 我用神经网络和遗传算法解决了这个问题。 源代码是免费提供的。

http://www.roncemer.com/tic-tac -机器学习实验

Have a look at my Tic project. I've solved this problem with both neural network and genetic algorithm. The source code is freely available.

http://www.roncemer.com/tic-tac-toe-an-experiment-in-machine-learning

爱已欠费 2024-07-24 04:46:12

我认为您应该使用传递函数实现“传统”前馈 ANN,因为这样可以您可以使用反向传播来训练它。 这些代码通常最终是几行代码,如下所示:

SetupInputs();
for (l = 1 .. layers.count)
    for (i = 0 .. layers[l].count)
        sum = 0
        for (j = 0 .. layers[l-1].count)
            sum += layers[l-1][j] * weights[l-1][j]
        layers[l][i] = TransferFunction(sum)

I think you should implement a 'traditional' feed-forward ANN using transfer functions, as that allows you to train it using back-propagation. The code for these usually ends up being a few lines of code, something like this:

SetupInputs();
for (l = 1 .. layers.count)
    for (i = 0 .. layers[l].count)
        sum = 0
        for (j = 0 .. layers[l-1].count)
            sum += layers[l-1][j] * weights[l-1][j]
        layers[l][i] = TransferFunction(sum)
独夜无伴 2024-07-24 04:46:12

这是一个优秀的人工智能编码入门项目,但提出一个完整的解决方案将是一个很大的答案。

与大多数软件一样,我建议使用面向对象的设计。 例如:定义一个具有输入、权重和输出函数的 Neuron 类。 然后,创建多个 Neuron 对象以构建您的网络。

请参阅关于人工神经网络的维基百科文章作为一个良好的起点。

祝代码好运! 听起来很有趣。

This is an excellent starter project for AI coding, but coming up with a complete solution will be way to big of an answer for SO.

As with most software, I recommend using an object-oriented design. For example: Define a Neuron class which has inputs, weights, and an output function. Then, create several of these Neuron objects in order to build your network.

See the wikipedia article on artificial neural networks for a good starting point.

Good luck with the code! Sounds like a lot of fun.

伤痕我心 2024-07-24 04:46:12

它不是对您问题的直接答案,但您应该查看以下框架/工具: SNNS 或其 Java 对应部分 JavaNNS。 我很确定您会在那里找到问题的答案。

It is not a direct answer to your question, but you should have a look at the following framework/tool: SNNS or its Java counterpart JavaNNS. I'm pretty sure that there you'll find an answer to your question.

一个人的旅程 2024-07-24 04:46:12

添加权重后,您需要使用函数对总和进行归一化,如果您想允许负数,人们通常使用 TANH。

编辑:

这是我几年前开发的一个java多层感知器实现。 这个用于跳棋,但输入较少,您也可以将其用于跳棋。

另外,你可能需要找到一种方法来教它获胜,但这是另一个问题

After adding the weights, you need to normalize the sum using a function, people usually use TANH, if you want to allow negative numbers.

edit:

Here is a java multilayer perceptron implementation that i worked on a few years ago. this one was used for checkers, but with less inputs you can use it for checkers too.

Also, you need to probably figure out a way to teach it to win, but thats another problem

待天淡蓝洁白时 2024-07-24 04:46:12

如果您使用 FANN 或 Neuroph 等神经网络库,您将节省时间。

对输入进行编码的一种方法是使用 9 个输入神经元。 输出也可以是 9 个神经元。 我在其他重播中没有看到隐藏层的大小。 我想您将使用具有传统 3 层的 MLP。 隐藏层的大小始终是个谜。 我会尝试 10 个隐藏神经元。

如果传递函数是 sigmoid,您可以按如下方式对输入进行编码:

0.0 - O 玩家。

1.0 - X 玩家。

0.5 - 空。

ANN 的输出将是 9 个实数。 在这种情况下,一些单元格将已经被占用。 您可以搜索与空单​​元格相对应的最高输出值。

You will save time if you use neural network library like FANN or Neuroph.

One way to encode your input is by 9 input neurons. The output is also good to be 9 neurons. What I do not see in the other replays is the size of the hidden layer. I suppose you are going to use MLP with traditional 3 layers. The size of the hidden layer is always mystery. I would try 10 hidden neurons.

If the transfer function is sigmoid you can encode input as follows:

0.0 - O player.

1.0 - X player.

0.5 - Empty.

The output of the ANN will be 9 real numbers. In this case some of the cells will be occupied already. You can search for the highest output value which corresponds to an empty cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文