资源简介
这是我写的一个基于神经网络的五子棋程序,用python3写的,需要配置tensorflow才能使用
代码片段和文件信息
import tensorflow as tf
from SGFfile import SGFflie
import os
sgf = SGFflie()
class myCNN():
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)
@staticmethod
def weight_varible(shape):
‘‘‘权重变量‘‘‘
initial = tf.truncated_normal(shape stddev=0.1)
return tf.Variable(initial)
@staticmethod
def bias_variable(shape):
‘‘‘偏置变量‘‘‘
initial = tf.constant(0.1 shape=shape)
return tf.Variable(initial)
@staticmethod
def conv2d(x W):
‘‘‘卷积核‘‘‘
return tf.nn.conv2d(x W strides=[1 1 1 1] padding=‘SAME‘)
@staticmethod
def max_pool_2x2(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(writ
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5404 2017-12-17 20:42 CNN.py
文件 18007 2018-01-04 19:24 GoBang.py
文件 22796 2017-11-01 14:11 robot.py
文件 5561 2018-01-08 16:22 SGFfile.py
文件 687 2017-10-19 14:37 tools.py
----------- --------- ---------- ----- ----
52455 5
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论