Numpy provides an n-dimensional array , and many functions for manipulating these arrays. Numpy is a generic work 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- 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 datax = np.random.randn(N, D_in)y = np.random.randn(N, D_out)# Randomly initialize weightsw1 = np.random.randn(D_in, H)w2 = np.random.randn(H, D_out)learning_rate = 1e-6for 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继续阅读与本文标签相同的文章
-
Serpent.AI - 游戏代理框架(Python)
2026-05-26栏目: 教程
-
face-alignment:用 pytorch 实现的 2D 和 3D 人脸对齐库
2026-05-26栏目: 教程
-
ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库
2026-05-26栏目: 教程
-
pix2code:从截图生成图形用户界面代码
2026-05-26栏目: 教程
-
SSD:TensorFlow中的单次多重检测器
2026-05-26栏目: 教程
