资源简介
这是我写的一个基于神经网络的五子棋程序,用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-BDD100K大规模多样化驾驶视频数据
- Instant Pygame for Python Game Development How
- Biopython Tutorial
- Think Python 2nd
- 一个小小的表白程序(python)
- Python课堂笔记(高淇400集第一季)
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
评论
共有 条评论