資源簡介
這是我寫的一個基于神經網(wǎng)絡的五子棋程序,用python3寫的,需要配置tensorflow才能使用

代碼片段和文件信息
import?tensorflow?as?tf
from?SGFfile?import?SGFflie
import?os
sgf?=?SGFflie()
class?myCNN():
????def?__init__(self):
????????‘‘‘初始化神經網(wǎng)絡‘‘‘
????????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(“已讀取數(shù)據(jù)“)
????????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
評論
共有 條評論