码迷,mamicode.com
首页 > 其他好文 > 详细

基于神经元分析手牌

时间:2020-04-18 14:15:36      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:line   color   array   训练   sig   tran   ted   rate   __init__   

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 17 20:13:37 2020

@author: ASUS
"""

# load the file

import numpy as np 
import scipy.special
class neuralNetwork:
    def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
        self.inodes=inputnodes
        self.hnodes=hiddennodes
        self.onodes=outputnodes
        self.lr=learningrate
        #link weight 
        self.wih=np.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
        self.who=np.random.normal(0.0,pow(self.hnodes,-0.5),(self.onodes,self.hnodes))
        #activation function
        self.activation_function=lambda x: scipy.special.expit(x)
        pass
    def train(self,input_list,target_list):
        # convert inputs list to 2d array
        inputs=np.array(input_list,ndmin=2).T 
        targets=np.array(target_list,ndmin=2).T 
        #calculate signals into hidden layer
        hidden_inputs=np.dot(self.wih,inputs)
        hidden_output=self.activation_function(hidden_inputs)
        #calculate signals into final layer
        final_inputs=np.dot(self.who,hidden_output)
        final_outputs=self.activation_function(final_inputs)
        # the error
        output_errors=targets-final_outputs
        #hidden layer error is the output_errors,split by weights recombined at hidden nodes
        hidden_errors=np.dot(self.who.T,output_errors)
        # update the weights for the links between the hidden and output layers
        self.who+=self.lr*np.dot((output_errors*final_outputs*(1.0-final_outputs)),np.transpose(hidden_output))
        self.wih+=self.lr*np.dot((hidden_errors*hidden_output*(1.0-hidden_output)),np.transpose(inputs))
        pass
    def query(self,input_list):
        #convert inputs list to 2d array
        inputs=np.array(input_list,ndmin=2).T 
        #calculate signals into hidden layer
        hidden_inputs=np.dot(self.wih,inputs)
        hidden_output=self.activation_function(hidden_inputs)
        #calculate signals into out layer
        final_inputs=np.dot(self.who,hidden_output)
        final_outputs=self.activation_function(final_inputs)
        return final_outputs
        
input_nodes=17
hidden_nodes=100
output_nodes=2
learning_rate=0.3
    # create instance of neural network
n=neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)
    # load the mnist training data csv file into list
training_data_file=open(C:\\Users\\ASUS\\test.csv,r)
training_data_list=training_data_file.readlines()
training_data_file.close()
# train the nerual network
# go through all records in the training data set

for record in  training_data_list:
    #split the record by the ‘,‘ commas
    all_values=record.split(,)
    all_values = list(map(int, all_values))#需要对导入数据转换数值类型
    #scale and shift the inputs
    inputs=(np.asfarray(all_values[1:])/15*0.99)+0.01
    #creat the target output values
    targets=all_values[0]
    n.train(inputs,targets)
    pass

这是一个单个隐藏层的神经元网络

训练文件的格式为18位数组,首位表示输赢,后17位表示手牌状态

基于神经元分析手牌

标签:line   color   array   训练   sig   tran   ted   rate   __init__   

原文地址:https://www.cnblogs.com/yuvejxke/p/12725465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!