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


Python Network.learn方法代码示例

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


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

示例1: simpleRecall

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import learn [as 别名]
def simpleRecall():
    network = Network(5, 4)

    memoryLayer = network.createLayer(4, name="memory")
    inputGateLayer = network.createLayer(4, name="inputGate", bias=True)
    forgetGateLayer = network.createLayer(4, name="forgetGate", bias=True)
    outputGateLayer = network.createLayer(4, name="outputGate", bias=True)

    network.connectLayers(network.inputLayer, memoryLayer, interconnected=True)
    network.connectLayers(network.inputLayer, inputGateLayer, interconnected=True)
    network.connectLayers(network.inputLayer, forgetGateLayer, interconnected=True)
    network.connectLayers(network.inputLayer, outputGateLayer, interconnected=True)

    network.connectLayers(memoryLayer, inputGateLayer, recurrent=True, interconnected=True)
    network.connectLayers(memoryLayer, forgetGateLayer, recurrent=True, interconnected=True)
    network.connectLayers(memoryLayer, outputGateLayer, recurrent=True, interconnected=True)
    network.connectLayers(memoryLayer, network.outputLayer, interconnected=False)

    network.addSelfRecurrence(memoryLayer, forgetGateLayer)

    network.gateIncomingConnections(network.outputLayer, outputGateLayer)
    network.gateIncomingConnections(memoryLayer, inputGateLayer)

    printLayer(network.inputLayer)
    printLayer(inputGateLayer)
    printLayer(memoryLayer)
    printLayer(outputGateLayer)
    printLayer(network.outputLayer)

    inputSet = []

    # Create set of input sequences.
    for _ in xrange(200):
        length = 15
        # Fill 10 blank vectors.
        inputSequence = [[0.0 for _ in xrange(5)] for __ in xrange(length)]

        # Add one relevant vector.
        relevant = [0.0, 0.0, 0.0, 0.0, 0.0]
        relevant[random.randint(0, 3)] = 1.0
        inputSequence.append(relevant)

        random.shuffle(inputSequence)

        # Add prompt
        inputSequence.append([0.0, 0.0, 0.0, 0.0, 1.0])

        # Create output sequence.
        outputSequence = [[0.0] * 4] * (length + 1)
        #outputSequence = [None] * 11
        expected = [0.0, 0.0, 0.0, 0.0]
        expected[relevant.index(1.0)] = 1.0
        outputSequence.append(expected)

        inputSet.append((inputSequence, outputSequence))

    testSet = inputSet[:20]
    inputSet = inputSet[20:]

    def calcDiff(x,y):
        diff = 0.0
        zipped = zip(x,y)
        for actual,expected in zipped[:-1]:
            for a in actual:
                if a > 0.5: diff += a
        actual,expected = zipped[-1]
        for a,e in zip(actual, expected):
            if e == 1.0 and a < 0.5:
                #print(a, " != 1")
                diff += 1.0 - a
            elif e == 0.0 and a > 0.5:
                #print(a, " != 0")
                diff += a
        return diff

    beforeDiff = calculateDiff(network, testSet, calcDiff, printValues=False)
    print("Before diff: %f" % beforeDiff)

    #for _ in xrange(100):
    iterations = 0
    while True:
        print("Iteration %d" % iterations)
        iterations += 1
        for inputSequence, outputSequence in inputSet:
            #print(".")
            output = network.learn(inputSequence, outputSequence)
        diff = calculateDiff(network, testSet, calcDiff, printValues=False)
        print(diff)
        if diff < 1: break

    afterDiff = calculateDiff(network, testSet, calcDiff, printValues=False)
    print("Before diff: %f" % beforeDiff)
    print("After diff: %f" % afterDiff)
开发者ID:vicariousgreg,项目名称:speakerIdentification,代码行数:95,代码来源:test.py

示例2: distractedRecall

# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import learn [as 别名]
def distractedRecall():
    network = Network(10, 4)

    memoryLayer = network.createLayer(8, name="memory")
    inputGateLayer = network.createLayer(8, name="inputGate", bias=True)
    forgetGateLayer = network.createLayer(8, name="forgetGate", bias=True)
    outputGateLayer = network.createLayer(8, name="outputGate", bias=True)

    network.connectLayers(network.inputLayer, memoryLayer, interconnected=True)
    network.connectLayers(network.inputLayer, inputGateLayer, interconnected=True)
    network.connectLayers(network.inputLayer, forgetGateLayer, interconnected=True)
    network.connectLayers(network.inputLayer, outputGateLayer, interconnected=True)

    network.connectLayers(memoryLayer, inputGateLayer, recurrent=True, interconnected=True)
    network.connectLayers(memoryLayer, forgetGateLayer, recurrent=True, interconnected=True)
    network.connectLayers(memoryLayer, outputGateLayer, recurrent=True, interconnected=True)
    network.connectLayers(memoryLayer, network.outputLayer, interconnected=True)

    network.addSelfRecurrence(memoryLayer, forgetGateLayer)

    network.gateOutgoingConnections(memoryLayer, outputGateLayer)
    network.gateIncomingConnections(memoryLayer, inputGateLayer)


    inputSet = []

    # Create set of input sequences.
    for _ in xrange(100):
        length = 10

        # Create input sequence.
        inputSequence = []

        # Fill random distractors.
        for _ in xrange(length):
            inputVector = [0.0 for _ in xrange(10)]
            inputVector[random.randint(4,7)] = 1
            inputSequence.append(inputVector)

        # Select two random vectors and inject random target symbols.
        firstIndex = random.randint(0,length-1)
        secondIndex = firstIndex
        while secondIndex == firstIndex: secondIndex = random.randint(0,length-1)
        if firstIndex > secondIndex:
            temp = firstIndex
            firstIndex = secondIndex
            secondIndex = temp
        firstTarget = random.randint(0,3)
        secondTarget = random.randint(0,3)
        #print(firstIndex, secondIndex)
        #print(firstTarget, secondTarget)

        inputSequence[firstIndex] = [1.0 if x == firstTarget else 0.0 for x in xrange(10)]
        inputSequence[secondIndex] = [1.0 if x == secondTarget else 0.0 for x in xrange(10)]

        # Add prompts
        inputSequence.append([1.0 if x == 8 else 0.0 for x in xrange(10)])
        inputSequence.append([1.0 if x == 9 else 0.0 for x in xrange(10)])

        outputSequence = [[0] * 4] * length
        #outputSequence = [None] * length
        outputSequence.append([1 if x == firstTarget else 0 for x in xrange(4)])
        outputSequence.append([1 if x == secondTarget else 0 for x in xrange(4)])
        #for line in inputSequence: print(line)
        #for line in outputSequence: print(line)
        #sys.exit()

        inputSet.append((inputSequence, outputSequence))

    testSet = inputSet[:10]
    inputSet = inputSet[10:]

    def calcDiff(x,y):
        diff = 0.0
        zipped = zip(x,y)
        for actual,expected in zipped[:-1]:
            for a in actual:
                if a > 0.5: diff += a
        actual,expected = zipped[-1]
        for a,e in zip(actual, expected):
            if e == 1.0 and a < 0.5:
                print(a, " != 1")
                diff += 1.0 - a
            elif e == 0 and a > 0.5:
                print(a, " != 0")
                diff += a
        return diff


    beforeDiff = calculateDiff(network, testSet, calcDiff, printValues=False)
    print("Before diff: %f" % beforeDiff)

    #for _ in xrange(10):
    iterations = 0
    while True:
        print("Iteration %d" % iterations)
        iterations += 1
        for inputSequence, outputSequence in inputSet:
            #print(".")
            output = network.learn(inputSequence, outputSequence)
#.........这里部分代码省略.........
开发者ID:vicariousgreg,项目名称:speakerIdentification,代码行数:103,代码来源:test.py


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