当前位置: 首页>>代码示例>>Python>>正文


Python NeuralNetwork.getAlpha方法代码示例

本文整理汇总了Python中NeuralNetwork.NeuralNetwork.getAlpha方法的典型用法代码示例。如果您正苦于以下问题:Python NeuralNetwork.getAlpha方法的具体用法?Python NeuralNetwork.getAlpha怎么用?Python NeuralNetwork.getAlpha使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NeuralNetwork.NeuralNetwork的用法示例。


在下文中一共展示了NeuralNetwork.getAlpha方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import getAlpha [as 别名]
class Classifier:

    def __init__(self, classifier_type, **kwargs):
        """
        Initialize a classifier for managing learning model.
        Args:
            classifier_type: the type of learning model. e.g. neural_network
            **kwargs: store parameter in a dictionary
        """
        self.classifier_type = classifier_type
        self.params = kwargs

        self.clf = None
        self.file = open('result/trial_' + str(datetime.datetime.today()).replace("/", "_", -1) + ".txt", 'w', 0)

    def train(self, training_data, testData, classNum, batchSize):
        """
        Create a learning model. Train the model with the training data. Print the training accuracy every certain iterations.
        If the learning rate is not chosen appropriately, let the user to enter a new
        """
        # find the numbers for feature and label
        featureNum = training_data.shape[1] - 1

        # #this will find all the unique labels automatically, but will have problem when training data is lacking some labels
        # labelNum = len(np.unique(training_data[:, :1]))
        labelNum = classNum

        # get the number of nodes for each layer
        if "hidden_layer" in self.params and self.params["hidden_layer"] is not None:
            nodeNum = [featureNum] + self.params["hidden_layer"] + [labelNum]
        else:
            nodeNum = [featureNum, featureNum * 2, labelNum]

        # get the mode for initializing the weight
        if "weightInitMode" in self.params and self.params["weightInitMode"] is not None:
            weightInitMode = self.params["weightInitMode"]
        else:
            weightInitMode = None

        # get the momentum factor
        if "momentumFactor" in self.params:
            momentumFactor = self.params["momentumFactor"]
        else:
            momentumFactor = 0.0

        self.clf = NeuralNetwork(training_data, nodeNum, weightInitMode, momentumFactor)
        iteration = 5
        totalIter = 0
        testSize  = 100000
        while iteration > 0:

            if iteration < 10:
                self.clf.train(iteration, batchSize)
                totalIter += iteration
                print "---------- Settings ----------"
                print "Examples                 :", training_data.shape[0]
                print "Batch size               :", batchSize
                print "Alpha                    :", self.clf.getAlpha()
                print "Momentum factor          :", momentumFactor
                print "# of Nodes in all layers :", nodeNum
                print "Training iteration so far:", totalIter
                self.file.write("\n")
                self.file.write("---------- Settings ----------" + "\n")
                self.file.write("Examples                 : " + str(training_data.shape[0]) + "\n")
                self.file.write("Batch size               : " + str(batchSize) + "\n")
                self.file.write("Alpha                    : " + str(self.clf.getAlpha()) + "\n")
                self.file.write("Momentum factor          : " + str(momentumFactor) + "\n")
                self.file.write("# of Nodes in all layers : " + str(nodeNum) + "\n")
                self.file.write("Training iteration so far: " + str(totalIter) + "\n")
                self.test(training_data, "training")
                self.test(testData, "testing")
                iteration = 0

            while iteration >= testSize:
                self.clf.train(testSize, batchSize)
                totalIter += testSize
                print "---------- Settings ----------"
                print "Examples                 :", training_data.shape[0]
                print "Batch size               :", batchSize
                print "Alpha                    :", self.clf.getAlpha()
                print "Momentum factor          :", momentumFactor
                print "# of Nodes in all layers :", nodeNum
                print "Training iteration so far:", totalIter
                self.file.write("\n")
                self.file.write("---------- Settings ----------" + "\n")
                self.file.write("Examples                 : " + str(training_data.shape[0]) + "\n")
                self.file.write("Batch size               : " + str(batchSize) + "\n")
                self.file.write("Alpha                    : " + str(self.clf.getAlpha()) + "\n")
                self.file.write("Momentum factor          : " + str(momentumFactor) + "\n")
                self.file.write("# of Nodes in all layers : " + str(nodeNum) + "\n")
                self.file.write("Training iteration so far: " + str(totalIter) + "\n")
                self.test(training_data, "training")
                self.test(testData, "testing")
                iteration -= testSize

            if iteration > 0:
                self.clf.train(iteration, batchSize)
                totalIter += iteration
                print "---------- Settings ----------"
                print "Examples                 :", training_data.shape[0]
#.........这里部分代码省略.........
开发者ID:jasonlingo,项目名称:Machine-Learning-Assignments,代码行数:103,代码来源:classifier.py


注:本文中的NeuralNetwork.NeuralNetwork.getAlpha方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。