CS231_A1:Two-layer Neural Net

这篇博客介绍了作者使用两层神经网络在CIFAR-10数据集上进行训练的过程,包括损失函数、训练过程和验证集上的准确率。文章展示了不同隐藏层大小和学习率、正则化参数下的结果,并讨论了参数选择对性能的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codes All from:

http://www.cnblogs.com/daihengchen/p/5754383.html

https://github.com/autoliuweijie/DeepLearning/blob/master/cs231n/HomeWorks/assignment1/two_layer_net.ipynb

继续学习ing。



A Neural Network


首先定义了一个函数rel_error来计算relative error相对误差。

# A bit of setup

import numpy as np
import matplotlib.pyplot as plt

from cs231n.classifiers.neural_net import TwoLayerNet

%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

# for auto-reloading external modules
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2

def rel_error(x, y):
  """ returns relative error """
  return np.max(np.abs(x - y) / (np.maximum(1e-8, np.abs(x) + np.abs(y))))


定义了一个两层的网络(输入层+神经元隐含层+输出层)
分别初始化模型和toy data(这个应该怎么翻译??玩具数据集??反正应该是个很小的用来做测试的数据集吧),
toy data一共有5个数据,分别属于3中不同的类别。
# Create a small net and some toy data to check your implementations.
# Note that we set the random seed for repeatable experiments.

input_size = 4
hidden_size = 10
num_classes = 3
num_inputs = 5

def init_toy_model():
  np.random.seed(0)
  return TwoLayerNet(input_size, hidden_size, num_classes, std=1e-1)

def init_toy_data():
  np.random.seed(1)
  X = 10 * np.random.randn(num_inputs, input_size)
  y = np.array([0, 1, 2, 2, 1])
  return X, y

net = init_toy_model()
X, y = init_toy_data()


补全nueral_net.py中的函数。
import numpy as np
import matplotlib.pyplot as plt


class TwoLayerNet(object):
  """
  A two-layer fully-connected neural network. The net has an input dimension of
  N, a hidden layer dimension of H, and performs classification over C classes.
  We train the network with a softmax loss function and L2 regularization on the
  weight matrices. The network uses a ReLU nonlinearity after the first fully
  connected layer.

  In other words, the network has the following architecture:

  input - fully connected layer - ReLU - fully connected layer - softmax

  The outputs of the second fully-connected layer are the scores for each class.
  """

  def __init__(self, input_size, hidden_size, output_size, std=1e-4):
    """
    Initialize the model. Weights are initialized to small random values and
    biases are initialized to zero. Weights and biases are stored in the
    variable self.params, which is a dictionary with the following keys:

    W1: First layer weights; has shape (D, H)
    b1: First layer biases; has shape (H,)
    W2: Second layer weights; has shape (H, C)
    b2: Second layer biases; has shape (C,)

    Inputs:
    - input_size: The dimension D of the input data.
    - hidden_size: The number of neurons H in the hidden layer.
    - output_size: The number of classes C.
    """
    self.params = {}
    self.params['W1'] = std * np.random.randn(input_size, hidden_size)
    self.params['b1'] = np.zeros(hidden_size)
    self.params['W2'] = std * np.random.randn(hidden_size, output_size)
    self.params['b2'] = np.zeros(output_size)

  def loss(self, X, y=None, reg=0.0):
    """
    Compute the loss and gradients for a two layer fully connected neural
    network.

    Inputs:
    - X: Input data of shape (N, D). Each X[i] is a training sample.
    - y: Vector of training labels. y[i] is the label for X[i], and each y[i] is
      an integer in the range 0 <= y[i] < C. This parameter is optional; if it
      is not passed then we only return scores, and if it is passed then we
      instead return the loss and gradients.
    - reg: Regularization strength.

    Returns:
    If y is None, return a matrix scores of shape (N, C) where scores[i, c] is
    the sc
``` {———————— 智能选股系统 V8.3 OPT ————————} // █ 神经微分优化模块(修正重复定义) PARAM_OPT := NEURAL_DIFF_EVO( // 保留唯一定义位置 EPOCH := 3000, POP := 2000, MUT_RATE := ADAPT_MUT(0.15,0.003), CROSS := TOP_CROSS(0.7), FITNESS := 0.7*SHARPE + 0.3*CALMAR, CONSTRAIN := [MAX_CHG<0.2, FEAT_IMP>0.05]); // 补充分号 // █ 3D特征引擎(保持原有结构) ALPHA_FACTOR := DEEP_FEAT_FUSE( T_STREAM := TEMP_CONV( [NEURAL_VOL(5,0.7), DEEP_ORDER(10,0.9)], KERNEL := [3,5,7]), S_STREAM := SPAT_ATTN( IND_CORR_MAT, SECT_EMBED := 64), FUSE_LAYER := [ TRANS_ENC(8,256), GBOOST_SEL(500,0.01)]); // █ 高频资金流(保持原有参数) NORTH_FLOW := MULTI_DRL( TIME_SCALE := [1M,5M,30M,1H], STATE_ENC := G_CONV_NET( NODE_FEAT := [HKHOLD,IDX_FUT,ETF_FLOW], EDGE_W := CROSS_CORR), REWARD := 1.4*RET_SMOOTH(0.9) - 0.3*VOL_DD + 0.2*FLOW_GRAD); // █ 波动引擎(语法验证通过) VOL_REGIME := NEURAL_REGIME( VOL_COMP := [VOL_BAND, GARCH(1,1), JUMP_DIFF], TRANS_NET := TCN( IN_DIM := 6, LEVELS := 8, DILATE := 2), JUMP_DETECT := BAYES_CP( PRIOR_A := 0.1, WARN_TH := 0.95)); // █ 行业轮动(系数校验无误) IND_SCORE := 0.35*NLP_SENTIMENT( [NEWS,TWIT,RED,INST_RESEARCH], T_DECAY := EXP(-0.07*DELAY), TOPIC_CLU := BERT_TOPIC(256)) + 0.30*ADAPT_MOMENTUM( WINDOW := FOURIER_AD(14), VOL_ADJ := TRUE, DECAY := 0.02) + 0.25*FLOW_MOM_3D( LEAD_LAG := [1.2, 0.8], LIQ_MULT := VOL_SMOOTH) + 0.10*POLICY_SENSE*(2.0 - 0.35*MARKET_PHASE); // █ 信号融合(逻辑完整性保留) FINAL_SIGNAL := DEEP_FUSION( INPUT := [ TEMP_CONV(ALPHA_STREAM, [3,5,7]), SPAT_ATTN(IND_CORR,64) ], FUSE_LAYER := [ TRANS_ENC(8,256), GBOOST_SEL(500,0.01) ], ACT_COND := [ Q_SIG >= NEURAL_TH(MARKET_PH), VOL_RAT > 1.5*DYNA_BETA, PRICE_Q(0.85,50), LIQ_SCORE>0.97, INST_FLOW>=3.2, ANOM_SCORE>NEURAL_ANO, FUND_CONF(3D,0.8) ], DYN_WEIGHT := NEURAL_W(0.0005,20)); // █ 风控系统(约束条件完整) RISK_CTRL := HIER_RISK( L1 := VOL_CAP(0.25,ADAP_SKEW), L2 := CORR_DIV( MAX_SECT := 0.15, MIN_DIVERGE := 0.3), L3 := BLACKSWAN_PRO( STRESS_IDX>0.85, LIQ_FLOOR := 0.001, HEDGE := 0.3));```你的身份是高级编程技术专家,精通各类编程语言,能对编程过程中的各类问题进行分析和解答。我的问题是【我正在编辑【通达信条件选股】代码,遇到了 【错误句 : 详细信息 : 单词最大字符数不得超过 15 个 错误起始位置 : 0 ; 长度: 0】,请帮我检查并改正错误点补全正确代码,生成修正后完整代码。原有选股逻辑完整保留。
最新发布
03-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值