• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python q2_gradcheck.gradcheck_naive函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中q2_gradcheck.gradcheck_naive函数的典型用法代码示例。如果您正苦于以下问题:Python gradcheck_naive函数的具体用法?Python gradcheck_naive怎么用?Python gradcheck_naive使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: sanity_check

def sanity_check():
    """
    Run python q4_softmaxreg.py.
    """
    random.seed(314159)
    np.random.seed(265)

    dataset = StanfordSentiment()
    tokens = dataset.tokens()
    nWords = len(tokens)

    _, wordVectors0, _ = load_saved_params()
    wordVectors = (wordVectors0[:nWords,:] + wordVectors0[nWords:,:])
    dimVectors = wordVectors.shape[1]

    dummy_weights = 0.1 * np.random.randn(dimVectors, 5)
    dummy_features = np.zeros((10, dimVectors))
    dummy_labels = np.zeros((10,), dtype=np.int32)    
    for i in xrange(10):
        words, dummy_labels[i] = dataset.getRandomTrainSentence()
        dummy_features[i, :] = getSentenceFeature(tokens, wordVectors, words)
    print "==== Gradient check for softmax regression ===="
    gradcheck_naive(lambda weights: softmaxRegression(dummy_features,
        dummy_labels, weights, 1.0, nopredictions = True), dummy_weights)

    print "\n=== Results ==="
    print softmaxRegression(dummy_features, dummy_labels, dummy_weights, 1.0)
开发者ID:KyleGoyette,项目名称:cs224d,代码行数:27,代码来源:q4_softmaxreg.py


示例2: sanity_check

def sanity_check():
    """
    Set up fake data and parameters for the neural network, and test using
    gradcheck.
    """
    print "Running sanity check..."

    N = 20
    ############################################################################
    # The following dimensions are Dx, H, Dy                                   #
    ############################################################################
    dimensions = [10, 5, 10]
    data = np.random.randn(N, dimensions[0])  # each row will be a datum
    labels = np.zeros((N, dimensions[2]))   # lables are 1 only where it is True
    for i in xrange(N):
        labels[i,random.randint(0,dimensions[2]-1)] = 1

    ############################################################################
    # All the params are packed here                                           #
    # I think this a bad way - You need to initialize using normal distribution#
    # See the number of parameters in the q1.pdf                               #
    ############################################################################
    params = np.random.randn((dimensions[0] + 1) * dimensions[1] + (
        dimensions[1] + 1) * dimensions[2], )

    gradcheck_naive(lambda params: forward_backward_prop(data, labels, params,
        dimensions), params)
开发者ID:abhinavkashyap92,项目名称:cs224d,代码行数:27,代码来源:q2_neural.py


示例3: test_word2vec

def test_word2vec():
    # Interface to the dataset for negative sampling
    dataset = type('dummy', (), {})()
    def dummySampleTokenIdx():
        return random.randint(0, 4)

    def getRandomContext(C):
        tokens = ["a", "b", "c", "d", "e"]
        return tokens[random.randint(0,4)], [tokens[random.randint(0,4)] \
           for i in xrange(2*C)]
    dataset.sampleTokenIdx = dummySampleTokenIdx
    dataset.getRandomContext = getRandomContext

    random.seed(31415)
    np.random.seed(9265)
    dummy_vectors = normalizeRows(np.random.randn(10,3))
    dummy_tokens = dict([("a",0), ("b",1), ("c",2),("d",3),("e",4)])
    print "==== Gradient check for skip-gram ===="
    gradcheck_naive(lambda vec: word2vec_sgd_wrapper(skipgram, dummy_tokens, vec, dataset, 5), dummy_vectors)
    gradcheck_naive(lambda vec: word2vec_sgd_wrapper(skipgram, dummy_tokens, vec, dataset, 5, negSamplingCostAndGradient), dummy_vectors)
    #print "\n==== Gradient check for CBOW      ===="
    #gradcheck_naive(lambda vec: word2vec_sgd_wrapper(cbow, dummy_tokens, vec, dataset, 5), dummy_vectors)
    #gradcheck_naive(lambda vec: word2vec_sgd_wrapper(cbow, dummy_tokens, vec, dataset, 5, negSamplingCostAndGradient), dummy_vectors)

    print "\n=== Results ==="
    print skipgram("c", 3, ["a", "b", "e", "d", "b", "c"], dummy_tokens, dummy_vectors[:5,:], dummy_vectors[5:,:], dataset)
    print skipgram("c", 1, ["a", "b"], dummy_tokens, dummy_vectors[:5,:], dummy_vectors[5:,:], dataset, negSamplingCostAndGradient)
开发者ID:situgongyuan,项目名称:DeepLearningForNLP,代码行数:27,代码来源:q3_word2vec.py


示例4: test_word2vec

def test_word2vec():
    # Interface to the dataset for negative sampling
    dataset = type('dummy', (), {})()
    def dummySampleTokenIdx():
        return random.randint(0, 4)

    def getRandomContext(C):
        tokens = ["a", "b", "c", "d", "e"]
        return tokens[random.randint(0,4)], [tokens[random.randint(0,4)] \
           for i in xrange(2*C)]

    def getContexts(C,sz=50):
        contexts = []
        for i in xrange(sz):
            C1 = random.randint(1,C)
            centerword, context = dataset.getRandomContext(C1)
            contexts.append((C1, centerword, context))
        return contexts

    dataset.sampleTokenIdx = dummySampleTokenIdx
    dataset.getRandomContext = getRandomContext

    random.seed(31415)
    np.random.seed(9265)
    dummy_vectors = normalizeRows(np.random.randn(10,3))
    dummy_tokens = dict([("a",0), ("b",1), ("c",2),("d",3),("e",4)])

    def getNegSamples(contexts):
        negsamples = []
        for context in contexts:
            samples = []
            for contextWord in context[2]:
                target = dummy_tokens[contextWord]
                indices = [target]
                for i in xrange(10):
                    k = dataset.sampleTokenIdx()
                    while k == target:
                        k = dataset.sampleTokenIdx()
                    indices.append(k)
                samples.append(indices)
            negsamples.append(samples)
        return negsamples # negsamples: [samples],
                        # samples:[indices], indices:[rndSample1,..,rndSampleK]

    dataset.contexts = getContexts(5)
    print dataset.contexts
    dataset.negsamples = getNegSamples(dataset.contexts)

    print "==== Gradient check for skip-gram ===="
    gradcheck_naive(lambda vec: word2vec_sgd_wrapper(skipgram, dummy_tokens, vec, dataset, 5), dummy_vectors)
    gradcheck_naive(lambda vec: word2vec_sgd_wrapper(skipgram, dummy_tokens, vec, dataset, 5, negSamplingCostAndGradient), dummy_vectors)
    #print "\n==== Gradient check for CBOW      ===="
    #gradcheck_naive(lambda vec: word2vec_sgd_wrapper(cbow, dummy_tokens, vec, dataset, 5), dummy_vectors)
    #gradcheck_naive(lambda vec: word2vec_sgd_wrapper(cbow, dummy_tokens, vec, dataset, 5, negSamplingCostAndGradient), dummy_vectors)

    print "\n=== Results ==="
    print skipgram("c", 3, ["a", "b", "e", "d", "b", "c"], dummy_tokens, dummy_vectors[:5,:], dummy_vectors[5:,:], dataset, dataset.negsamples[0])
    print skipgram("c", 1, ["a", "b"], dummy_tokens, dummy_vectors[:5,:], dummy_vectors[5:,:], dataset, dataset.negsamples[0], negSamplingCostAndGradient)
开发者ID:yysherlock,项目名称:cs224d,代码行数:58,代码来源:q3_word2vec_v1.py


示例5: my_test_word2vec

def my_test_word2vec():
    # Interface to the dataset for negative sampling
    dataset = type('dummy', (), {})()
    def dummySampleTokenIdx():
        return random.randint(0, 4)

    def getRandomContext(C):
        tokens = ["a", "b", "c", "d", "e"]
        return tokens[random.randint(0,4)], [tokens[random.randint(0,4)] \
           for i in xrange(2*C)]
    dataset.sampleTokenIdx = dummySampleTokenIdx
    dataset.getRandomContext = getRandomContext

    random.seed(31415)
    np.random.seed(9265)
    dummy_vectors = normalizeRows(np.random.randn(10,3))
    dummy_tokens = dict([("a",0), ("b",1), ("c",2),("d",3),("e",4)])
    print "==== Gradient check for skip-gram ===="
    # def word2vec_sgd_wrapper(word2vecModel, tokens, wordVectors, dataset, C, word2vecCostAndGradient = softmaxCostAndGradient):
    # params
    word2vecModel = skipgram
    tokens = dummy_tokens
    wordVectors = dummy_vectors
    C=5
    word2vecCostAndGradient = softmaxCostAndGradient

    batchsize = 50
    cost = 0.0
    grad = np.zeros(wordVectors.shape)
    N = wordVectors.shape[0]
    inputVectors = wordVectors[:N/2,:]
    outputVectors = wordVectors[N/2:,:]

    for i in xrange(batchsize):
        C1 = random.randint(1,C)
        centerword, context = dataset.getRandomContext(C1)
        
        if word2vecModel == skipgram:
            denom = 1
        else:
            denom = 1

        def fInVec(inVec):
            c,gin,gout = word2vecModel(centerword, C1, context, tokens, inVec, outputVectors, dataset, word2vecCostAndGradient)
            return (c,gin)
        def fOutVec(outVec):
            c,gin,gout = word2vecModel(centerword, C1, context, tokens, inputVectors, outVec, dataset, word2vecCostAndGradient)
            return (c,gout)

        c, gin, gout = word2vecModel(centerword, C1, context, tokens, inputVectors, outputVectors, dataset, word2vecCostAndGradient)
        # print gin
        # cost += c / batchsize / denom
        # grad[:N/2, :] += gin / batchsize / denom
        # grad[N/2:, :] += gout / batchsize / denom
        
        gradcheck_naive(fInVec, gin)
        gradcheck_naive(fOutVec, gout)
开发者ID:scinart,项目名称:cs224d,代码行数:57,代码来源:q3_word2vec.py


示例6: sanity_check

def sanity_check():
    """
    Run python q4_softmaxreg.py.
    """
    random.seed(314159)
    np.random.seed(265)

    dataset = StanfordSentiment()
    tokens = dataset.tokens()
    nWords = len(tokens)

    _, wordVectors0, _ = load_saved_params()
    N = wordVectors0.shape[0]//2
    #assert N == nWords
    wordVectors = (wordVectors0[:N,:] + wordVectors0[N:,:])
    dimVectors = wordVectors.shape[1]

    dummy_weights = 0.1 * np.random.randn(dimVectors, 5)
    dummy_features = np.zeros((10, dimVectors))
    dummy_labels = np.zeros((10,), dtype=np.int32)    
    for i in range(10):
        words, dummy_labels[i] = dataset.getRandomTrainSentence()
        dummy_features[i, :] = getSentenceFeature(tokens, wordVectors, words)
    print("==== Gradient check for softmax regression ====")
    gradcheck_naive(lambda weights: softmaxRegression(dummy_features,
        dummy_labels, weights, 1.0, nopredictions = True), dummy_weights)

    print("\n=== Results ===")
    print(softmaxRegression(dummy_features, dummy_labels, dummy_weights, 1.0))

    dummy_weights  = 0.1 * np.random.randn(40, 10) + 1.0
    dummy_features = np.random.randn(2000, 40)
    dummy_labels   = np.argmax(np.random.randn(2000, 10), axis=1)

    print(-np.log(0.1))#expected correct classification (random) = 1 in 10;
    #cost then becomes -np.log(0.1)
    print(softmaxRegression(dummy_features, dummy_labels, dummy_weights, 0.0)[0])

    dummy_weights  = 0.1 * np.random.randn(40, 80) + 1.0
    dummy_features = np.random.randn(2000, 40)
    dummy_labels   = np.argmax(np.random.randn(2000, 80), axis=1)

    print(-np.log(1./80))#expected correct classification (random) = 1 in 80;
    #cost then becomes -np.log(1./80)
    print(softmaxRegression(dummy_features, dummy_labels, dummy_weights, 0.0)[0])

    dummy_weights  = 0.1 * np.random.randn(40, 1000) + 1.0
    dummy_features = np.random.randn(40000, 40)
    dummy_labels   = np.argmax(np.random.randn(40000, 1000), axis=1)

    print(-np.log(1./1000))#expected correct classification (random) = 1 in 80;
    #cost then becomes -np.log(1./80)
    print(softmaxRegression(dummy_features, dummy_labels, dummy_weights, 0.0)[0])
    print(np.exp(-softmaxRegression(dummy_features, dummy_labels, dummy_weights, 0.0)[0]))
开发者ID:kingtaurus,项目名称:cs224d,代码行数:54,代码来源:q4_softmaxreg.py


示例7: your_sanity_checks

def your_sanity_checks():
    """
    Use this space add any additional sanity checks by running:
        python q2_neural.py 
    This function will not be called by the autograder, nor will
    your additional tests be graded.
    """
    print "Running your sanity checks..."
    ### YOUR CODE HERE
    input = np.array([[0.5, 0.3, -1.5, 0.0, -0.2]])
    labels = np.array([[0.2, 0.2, 0.2, 0.2, 0.2]])
    dimensions = [5, 3, 5]

    params = np.random.randn((dimensions[0] + 1) * dimensions[1] + (dimensions[1] + 1) * dimensions[2], )

    gradcheck_naive(lambda params: forward_backward_prop(input, labels, params, dimensions), params)
开发者ID:ZhangBanger,项目名称:cs224d,代码行数:16,代码来源:q2_neural.py


示例8: sanity_check

def sanity_check():
    """
    Set up fake data and parameters for the neural network, and test using 
    gradcheck.
    """
    print "Running sanity check..."

    N = 20
    dimensions = [10, 5, 10]
    data = np.random.randn(N, dimensions[0])   # each row will be a datum
    labels = np.zeros((N, dimensions[2]))
    for i in xrange(N):
        labels[i,random.randint(0,dimensions[2]-1)] = 1
    
    params = np.random.randn((dimensions[0] + 1) * dimensions[1] + (dimensions[1] + 1) * dimensions[2], )

    gradcheck_naive(lambda params: forward_backward_prop(data, labels, params,dimensions), params)
开发者ID:cdelichy92,项目名称:DeepLearning-NLP,代码行数:17,代码来源:q2_neural.py


示例9: sanity_check

def sanity_check():
    """
    Set up fake data and parameters for the neural network, and test using
    gradcheck.
    """
    print("Running sanity check...")

    N = 300
    dimensions = [10, 5, 10]
    data = np.random.randn(N, dimensions[0])   # each row will be a datum
    labels = np.zeros((N, dimensions[2]))
    for i in range(N):
        labels[i,random.randint(0,dimensions[2]-1)] = 1
    
    params = np.random.randn((dimensions[0] + 1) * dimensions[1] + (
        dimensions[1] + 1) * dimensions[2], )

    #cost, _ = forward_backward_prop(data, labels, params, dimensions)
    # # expect to get 1 in 10 correct
    #print(np.exp(-cost))
    # #cost is roughly correct

    gradcheck_naive(lambda params: forward_backward_prop(data, labels, params,
        dimensions), params)
开发者ID:kingtaurus,项目名称:cs224d,代码行数:24,代码来源:q2_neural.py


示例10: test_word2vec

def test_word2vec():
    # Interface to the dataset for negative sampling
    dataset = type('dummy', (), {})()
    def dummySampleTokenIdx():
        return random.randint(0, 4)

    def getRandomContext(C):
        tokens = ["a", "b", "c", "d", "e"]
        return tokens[random.randint(0,4)], [tokens[random.randint(0,4)] \
           for i in xrange(2*C)]
    dataset.sampleTokenIdx = dummySampleTokenIdx
    dataset.getRandomContext = getRandomContext

    random.seed(31415)
    np.random.seed(9265)
    dummy_vectors = normalizeRows(np.random.randn(10,3))
    dummy_tokens = dict([("a",0), ("b",1), ("c",2),("d",3),("e",4)])
    print "==== Gradient check for skip-gram ===="

    C1 = random.randint(1,5)
    centerword, context = dataset.getRandomContext(C1)

    negsamples = []
    for contextWord in context:
        target = dummy_tokens[contextWord]
        indices = [target]
        for i in xrange(10):
            k = dataset.sampleTokenIdx()
            while k == target:
                k = dataset.sampleTokenIdx()
            indices.append(k)
        negsamples.append(indices)

    dataset.negsamples = negsamples

    gradcheck_naive(lambda vec: my_sgd_wrapper(skipgram, dummy_tokens, vec, dataset, C1, centerword, context), dummy_vectors)
开发者ID:yysherlock,项目名称:cs224d,代码行数:36,代码来源:q3_word2vec_test.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python q2_sigmoid.sigmoid函数代码示例发布时间:2022-05-26
下一篇:
Python q1_softmax.softmax函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap