想用神经网络做一个AI五子棋吗?文末有源码和演示视频哦

没错,今天我们这个项目又是免费给出源代码的例子。

有的人说为什么我的很多文章都没有细讲代码,反而把所有代码共享呢!

原因很简单,因为我懒,哈哈。。。。

当然我觉得我当初在学习的时候经常发现网上很多例子实战项目都只是讲解,要么代码没给全,要么代码就是错的,根本不能运行。

所以我就特例独行,给出源代码,具体原理,网上很多无需赘述。

拿到了源代码,你可以自己玩玩,钻研学习,也可以拿去做毕设等等都行。

项目整体架构:游戏框架搭建和智能决策方面,以及神经网络训练数据集的棋谱。当然棋谱可以自己保存即可。

部分代码


def __init__(self):
        '''初始化神经网络'''
        self.sess = tf.InteractiveSession()

        # paras
        self.W_conv1 = self.weight_varible([5, 5, 1, 32])
        self.b_conv1 = self.bias_variable([32])
        # conv layer-1
        self.x = tf.placeholder(tf.float32, [None, 225])
        self.y = tf.placeholder(tf.float32, [None, 225])
        self.x_image = tf.reshape(self.x, [-1, 15, 15, 1])

        self.h_conv1 = tf.nn.relu(self.conv2d(self.x_image, self.W_conv1) + self.b_conv1)
        self.h_pool1 = self.max_pool_2x2(self.h_conv1)

        # conv layer-2
        self.W_conv2 = self.weight_varible([5, 5, 32, 64])
        self.b_conv2 = self.bias_variable([64])

        self.h_conv2 = tf.nn.relu(self.conv2d(self.h_pool1, self.W_conv2) + self.b_conv2)
        self.h_pool2 = self.max_pool_2x2(self.h_conv2)

        # full connection
        self.W_fc1 = self.weight_varible([4 * 4 * 64, 1024])
        self.b_fc1 = self.bias_variable([1024])

        self.h_pool2_flat = tf.reshape(self.h_pool2, [-1, 4 * 4 * 64])
        self.h_fc1 = tf.nn.relu(tf.matmul(self.h_pool2_flat, self.W_fc1) + self.b_fc1)

        # dropout
        self.keep_prob = tf.placeholder(tf.float32)
        self.h_fc1_drop = tf.nn.dropout(self.h_fc1, self.keep_prob)

        # output layer: softmax
        self.W_fc2 = self.weight_varible([1024, 225])
        self.b_fc2 = self.bias_variable([225])

        self.y_conv = tf.nn.softmax(tf.matmul(self.h_fc1_drop, self.W_fc2) + self.b_fc2)

        # model training
        self.cross_entropy = -tf.reduce_sum(self.y * tf.log(self.y_conv))
        self.train_step = tf.train.AdamOptimizer(1e-3).minimize(self.cross_entropy)

        self.correct_prediction = tf.equal(tf.argmax(self.y_conv, 1), tf.argmax(self.y, 1))
        self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))
        self.saver = tf.train.Saver()

        init = tf.global_variables_initializer()  # 不存在就初始化变量
        self.sess.run(init)

    def weight_varible(self, shape):
        '''权重变量'''
        initial = tf.truncated_normal(shape, stddev=0.1)
        return tf.Variable(initial)

    def bias_variable(self, shape):
        '''偏置变量'''
        initial = tf.constant(0.1, shape=shape)
        return tf.Variable(initial)

    def conv2d(self, x, W):
        '''卷积核'''
        return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

    def max_pool_2x2(self, x):
        '''池化核'''
        return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

    def restore_save(self, method=1):
        '''保存和读取模型'''
        if method == 1:
            self.saver.restore(self.sess, 'save\model.ckpt')
            #print("已读取数据")
        elif method == 0:
            saver = tf.train.Saver(write_version=tf.train.SaverDef.V2)
            saver.save(self.sess, 'save\model.ckpt')
            #print('已保存')

    def predition(self, qiju):
        '''预测函数'''
        _qiju = self.createdataformqiju(qiju)
        pre = self.sess.run(tf.argmax(self.y_conv, 1), feed_dict={self.x: _qiju, self.keep_prob: 1.0})

        point = [0, 0]
        l = pre[0]
        for i in range(15):
            if ((i + 1) * 15) > l:
                point[0] = int(i*30 + 25)
                point[1] = int((l - i * 15) * 30 + 25)
                break
        return point

    def train(self, qiju):
        '''训练函数'''
        sgf = SGFflie()
        _x, _y = sgf.createTraindataFromqipu(qiju)
        for i in range(10):
            self.sess.run(self.train_step, feed_dict={
                self.x: _x,
                self.y: _y
            })
        self.restore_save(method=0)

    def train1(self, x, y):
        '''另一个训练函数'''
        for i in range(100):
            self.sess.run(self.train_step, feed_dict={
                self.x: x,
                self.y: y,
                self.keep_prob: 0.5
            })
        print('训练好了一次')
        #self.restore_save(method=0)

完整代码获取关注公众号,后台回复“AI五子棋
扫码关注
在这里插入图片描述

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐