MATLAB面向对象编程类初始化问题求助:从零搭建神经网络
Hey there, let's figure out why your MATLAB network class initialization is failing and get it fixed up. Looking at your code snippet, there are a few key issues that are causing problems—let's go through them one by one:
1. 构造函数命名错误
In MATLAB, a class's constructor must have the exact same name as the class itself. Your method is named layers, which means when you try to create a network object (like net = network()), MATLAB won't run this initialization logic at all. You need to rename this method to network.
2. 未初始化的Cell数组
You're trying to assign values to self.biases{i} but never initialized biases as a cell array first. This will throw an indexing error because MATLAB doesn't know biases is supposed to be a cell yet.
3. 不完整的循环逻辑
Your inner loop (for j = 1:self....) is cut off—this is a syntax error on its own, even if other parts were fixed. We'll need to complete that to initialize the weights properly.
4. 值类 vs Handle 类的问题
By default, MATLAB classes are value classes—this means when you modify self inside a method, those changes don't persist outside the method unless you return the modified object. For a neural network class (where you'll want to update weights/biases over training), using a handle class is way more convenient. To make it a handle class, add < handle after the class name.
Fixed Code Example
Here's a corrected version of your class with proper initialization, using a handle class and standard neural network weight/bias initialization:
classdef network < handle properties sizes % e.g., [input_size, hidden_size, output_size] biases % Cell array of column vectors weights % Cell array of matrices nLayers % Number of layers in the network end methods % Constructor: same name as the class function obj = network(sizes) % First, store the layer sizes and number of layers obj.sizes = sizes; obj.nLayers = length(sizes); % Initialize biases: random normal distribution (mean 0, std 1) obj.biases = cell(1, obj.nLayers); for i = 2:obj.nLayers % Input layer usually has no biases obj.biases{i} = randn(sizes(i), 1); end % Initialize weights: random normal scaled by sqrt(1/input_size) obj.weights = cell(1, obj.nLayers - 1); for i = 1:obj.nLayers - 1 input_size = sizes(i); output_size = sizes(i+1); obj.weights{i} = randn(output_size, input_size) / sqrt(input_size); end end end end
How to Use It
You can create a network object like this (e.g., 2 input neurons, 3 hidden, 1 output):
net = network([2, 3, 1]);
Key Notes
- Constructor Input: I added
sizesas an input to the constructor—you need to pass the layer sizes when creating the network, since the class can't guess what architecture you want. - Bias Initialization: Typically, input layers don't have biases, so we start the loop at layer 2.
- Weight Initialization: Scaling the random weights by
sqrt(1/input_size)helps prevent vanishing/exploding gradients during training, which is a standard best practice. - Handle Class: Since we used
< handle, any changes you make tonet(like updating weights during training) will be applied directly to the object without needing to return it.
内容的提问来源于stack exchange,提问作者Adam Johnston

