zoukankan      html  css  js  c++  java
  • PyTorch 实战-用 Numpy 热身

    Numpy provides an n-dimensional array object, and many functions for manipulating these arrays. Numpy is a generic framework for scientific computing; it does not know anything about computation graphs, or deep learning, or gradients. However we can easily use numpy to fit a two-layer network to random data by manually implementing the forward and backward passes through the network using numpy operations:


    # -*- coding: utf-8 -*-
    import numpy as np
    
    # N is batch size; D_in is input dimension;
    # H is hidden dimension; D_out is output dimension.
    N, D_in, H, D_out = 64, 1000, 100, 10
    
    # Create random input and output data
    x = np.random.randn(N, D_in)
    y = np.random.randn(N, D_out)
    
    # Randomly initialize weights
    w1 = np.random.randn(D_in, H)
    w2 = np.random.randn(H, D_out)
    
    learning_rate = 1e-6
    for t in range(500):
        # Forward pass: compute predicted y
        h = x.dot(w1)
        h_relu = np.maximum(h, 0)
        y_pred = h_relu.dot(w2)
    
        # Compute and print loss
        loss = np.square(y_pred - y).sum()
        print(t, loss)
    
        # Backprop to compute gradients of w1 and w2 with respect to loss
        grad_y_pred = 2.0 * (y_pred - y)
        grad_w2 = h_relu.T.dot(grad_y_pred)
        grad_h_relu = grad_y_pred.dot(w2.T)
        grad_h = grad_h_relu.copy()
        grad_h[h < 0] = 0
        grad_w1 = x.T.dot(grad_h)
    
        # Update weights
        w1 -= learning_rate * grad_w1
        w2 -= learning_rate * grad_w2


    更多教程:http://www.tensorflownews.com/

  • 相关阅读:
    程序的编码问题
    man DMIDECODE
    Github熟悉一
    man uname
    第一轮铁大树洞APP开发冲刺(2)
    第九周学习进度
    第九周安卓开发学习总结(3)
    第一轮铁大树洞APP开发冲刺(1)
    第九周安卓开发学习总结(2)
    第九周安卓开发学习总结(1)
  • 原文地址:https://www.cnblogs.com/panchuangai/p/12568321.html
Copyright © 2011-2022 走看看