首页 > 解决方案 > 如何使用 Python NLTK 构建多维神经网络

问题描述

作为一个更大项目的一部分,我正在尝试构建一个神经网络,该网络使用两个独立的训练集针对一个输入训练系列构建。换句话说,我有一个输入数组,我想构建两个独立的突触进行训练。

简单来说,它看起来像这样:

x = [[0,0,0],[0,1,1],[0,1,0]...] y = [[0,1],[0,0],[1,0] ...] z = [[0,0,0,0,1],[1,0,0,0,0],[1,1,0,1,1]...]

其中 y 和 z 将被训练为针对训练集 x 的独立突触。

我一直无法修改我的代码以使其正常工作,我知道它不会太复杂,但我已经被困了一天多,需要一些帮助。

下面是我似乎无法使用多个维度的代码。

# Import Necessary Modules for Function
    import numpy as np
    import time

# Computer a non-linear Sigmoid curve (__--)
    def sigmoid(x):
        sigmoidOutput = 1/(1+np.exp(-x))
        return sigmoidOutput

# Convert the output of the signmoid function to its derivative
    def sigmoidDerivative(sigmoidOutput):
        return sigmoidOutput*(1-sigmoidOutput)

    def cleanSentence(sentence):
        # Tokenize the words within the input sentence
        sentenceWords = nltk.word_tokenize(sentence)
        # Stem the words within the tokenized setence
        sentenceWords = [stemmer.stem(userInput.lower()) for userInput in sentenceWords]
        return sentenceWords

# Return a binary bag of words [0 or 1] to evaluate whether or not a word exists within
#  a sentence.
    def bagWordCheck(sentence, userInput, show_details=False):
        # Tokenize the sentence
        sentenceWords = cleanSentence(sentence)
        # Create a word bag using the training-data from the user-input
        wordBag = [0]*len(userInput)  
        for sWord in sentenceWords:
            for i,w in enumerate(userInput):
                if w == sWord: 
                    wordBag[i] = 1
                    if show_details:
                        print ("found in bag: %s" % w)

        return(np.array(wordBag))

# Evaluate the user's input 
    def think(sentence, showDetails=False):
        x = bagWordCheck(sentence.lower(), userInput, showDetails)
        if showDetails:
            print ("sentence:", sentence, "\n bagWordCheck:", x)
        # input layer is our bag of words
        l0 = x
        # matrix multiplication of input and hidden layer
        l1 = sigmoid(np.dot(l0, synapse0))
        # output layer
        l2 = sigmoid(np.dot(l1, synapse2))
        return l2


# ANN and Gradient Descent code from https://iamtrask.github.io//2015/07/27/python-network-part2/
    def train(X, y, hidden_neurons=10, alpha=1, epochs=50000, dropout=False, dropout_percent=0.5):

        print ("Training with %s neurons, alpha:%s, dropout:%s %s" % (hidden_neurons, str(alpha), dropout, dropout_percent if dropout else '') )
        print ("Input matrix: %sx%s    Output matrix: %sx%s" % (len(X),len(X[0]),1, len(conversationTypes)) )
        np.random.seed(1)

        lastMeanError = 1

        # randomly initialize our weights with mean 0
        synapse0 = 2*np.random.random((len(X[0]), hidden_neurons)) - 1
        synapse2 = 2*np.random.random((hidden_neurons, len(conversations))) - 1
        #synapse2 = 2*np.random.random((hidden_neurons, len(conversationTypes))) - 1
        #synapse2 = 2*np.random.random((hidden_neurons, len(conversationSubjects))) - 1

        prev_synapse0_weight_update = np.zeros_like(synapse0)
        prev_synapse2_weight_update = np.zeros_like(synapse2)

        synapse0_direction_count = np.zeros_like(synapse0)
        synapse2_direction_count = np.zeros_like(synapse2)

        for j in iter(range(epochs+1)):

            # Feed forward through layers 0, 1, and 2
            layer_0 = X
            layer_1 = sigmoid(np.dot(layer_0, synapse0))

            if(dropout):
                layer_1 *= np.random.binomial([np.ones((len(X),hidden_neurons))],1-dropout_percent)[0] * (1.0/(1-dropout_percent))

            layer_2 = sigmoid(np.dot(layer_1, synapse2))

            # how much did we miss the target value?
            layer_2_error = y - layer_2

            if (j% 10000) == 0 and j > 5000:
                # if this 10k iteration's error is greater than the last iteration, break out
                if np.mean(np.abs(layer_2_error)) < lastMeanError:
                    print ("delta after "+str(j)+" iterations:" + str(np.mean(np.abs(layer_2_error))) )
                    lastMeanError = np.mean(np.abs(layer_2_error))
                else:
                    print ("break:", np.mean(np.abs(layer_2_error)), ">", lastMeanError )
                    break

            # in what direction is the target value?
            # were we really sure? if so, don't change too much.
            layer_2_delta = layer_2_error * sigmoidDerivative(layer_2)

            # how much did each l1 value contribute to the l2 error (according to the weights)?
            layer_1_error = layer_2_delta.dot(synapse2.T)

            # in what direction is the target l1?
            # were we really sure? if so, don't change too much.
            layer_1_delta = layer_1_error * sigmoidDerivative(layer_1)

            synapse2_weight_update = (layer_1.T.dot(layer_2_delta))
            synapse0_weight_update = (layer_0.T.dot(layer_1_delta))

            if(j > 0):
                synapse0_direction_count += np.abs(((synapse0_weight_update > 0)+0) - ((prev_synapse0_weight_update > 0) + 0))
                synapse2_direction_count += np.abs(((synapse2_weight_update > 0)+0) - ((prev_synapse2_weight_update > 0) + 0))        

            synapse2 += alpha * synapse2_weight_update
            synapse0 += alpha * synapse0_weight_update

            prev_synapse0_weight_update = synapse0_weight_update
            prev_synapse2_weight_update = synapse2_weight_update

        now = datetime.datetime.now()

        # persist synapses
        synapse = {'synapse0': synapse0.tolist(), 'synapse2': synapse2.tolist(),
                   'datetime': now.strftime("%Y-%m-%d %H:%M"),
                   'userInput': userInput,
                   'conversations' : conversations,
                   'conversationTypes': conversationTypes,
                   'conversationSubjects' : conversationSubjects
                  }
        synapse_file = "synapses.json"

        with open(synapse_file, 'w') as outfile:
            json.dump(synapse, outfile, indent=4, sort_keys=True)
        print ("saved synapses to:", synapse_file)

    X = np.array(trainingSet)
    y = np.array(completeConversations)
    y1 = np.array(completeTypes)
    y2 = np.array(completeSubjects)

    start_time = time.time()

    train(X, y, hidden_neurons=5, alpha=0.1, epochs=30000, dropout=False, dropout_percent=0.2)
    #train(X, y1, hidden_neurons=5, alpha=0.1, epochs=30000, dropout=False, dropout_percent=0.2)
    #train(X, y2, hidden_neurons=5, alpha=0.1, epochs=30000, dropout=False,        dropout_percent=0.2)

    elapsed_time = time.time() - start_time
    print ("processing time:", elapsed_time, "seconds")


    # probability threshold
    ERROR_THRESHOLD = 0.75
    # load our calculated synapse values
    synapse_file = 'synapses.json' 
    with open(synapse_file) as data_file: 
        synapse = json.load(data_file) 
        synapse0 = np.asarray(synapse['synapse0']) 
        synapse2 = np.asarray(synapse['synapse2'])

    def classify(sentence, showDetails=False):
        results = think(sentence, showDetails)

        results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD ] 
        results.sort(key=lambda x: x[1], reverse=True) 
        return_results =[[conversations[r[0]],r[1]] for r in results]
        #return_results =[[conversationSubjects[r[0]],r[1]] for r in results]
        #return_results =[[conversationTypes[r[0]],r[1]] for r in results]
        return return_results


    classify("charlotte explorer is not letting me ")

请帮忙!

标签: pythonpython-3.xtensorflowneural-networknltk

解决方案


推荐阅读