加载网络权重,去除全连接层的权重

仅作为记录,大佬请跳过。

感谢老师的示范。

fc_keys = [k for k in state_dict.keys() if "fc" in k]
for k in fc_keys:
    del state_dict[k]

查看设计的网络加载的网络权重的有没有不同的层

def load_from_pretrained(self, ckpt_path):
    print(f"==============> Loading weight {ckpt_path} for fine-tuning......")
    ckpt = torch.load(ckpt_path, map_location='cpu')
    state_dict = ckpt

    fc_keys = [k for k in state_dict.keys() if "fc" in k]
    for k in fc_keys:
        del state_dict[k]

    from pprint import pprint
    missing_keys, unexpected_keys = self.load_state_dict(state_dict, strict=False)
    print('missing_keys = ')
    pprint(missing_keys)
    print('unexpected_keys = ')
    pprint(unexpected_keys)
    print(f"=> loaded successfully '{ckpt_path}'")
    print('ok')

其中,self指设计的网络

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的卷积神经网络的代码示例,包括一个卷积,一个池化,一个全连接层和反向传播算法。本代码使用的是sklearn库中的手写数字数据集,将手写数字图片转换为28*28的像素矩阵。代码注释详细,希望对你有帮助。 ```python import numpy as np from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split class ConvolutionalNeuralNetwork: def __init__(self): self.lr = 0.1 # 学习率 self.epoch = 10 # 迭代次数 self.batch_size = 32 # 批次大小 self.conv_w = np.random.randn(3, 3, 1, 32) # 卷积核权重,大小为3*3,通道数为1,数量为32 self.conv_b = np.random.randn(32) # 卷积核偏置,数量为32 self.fc_w = np.random.randn(1568, 10) # 全连接层权重,输入为28*28*32,输出为10 self.fc_b = np.random.randn(10) # 全连接层偏置,数量为10 def conv2d(self, x, w, b, stride=1, padding=0): n, h, w, c = x.shape # 输入数据的维度 kh, kw, _, kn = w.shape # 卷积核的维度 ph, pw = padding, padding # 上下左右填充像素数 oh, ow = int((h + 2 * ph - kh) / stride) + 1, int((w + 2 * pw - kw) / stride) + 1 # 输出特征图的尺寸 # 对输入数据进行填充 pad_x = np.zeros((n, h + 2 * ph, w + 2 * pw, c)) pad_x[:, ph:h + ph, pw:w + pw, :] = x # 初始化输出特征图和卷积核的梯度 out = np.zeros((n, oh, ow, kn)) dw = np.zeros(w.shape) db = np.zeros(b.shape) # 卷积运算 for i in range(oh): for j in range(ow): for k in range(kn): out[:, i, j, k] = np.sum(pad_x[:, i*stride:i*stride+kh, j*stride:j*stride+kw, :] * w[:, :, :, k], axis=(1,2,3)) + b[k] # 计算卷积核和偏置的梯度 for i in range(oh): for j in range(ow): for k in range(kn): db[k] += np.sum(out[:, i, j, k]) for l in range(c): dw[:, :, l, k] += np.sum(pad_x[:, i*stride:i*stride+kh, j*stride:j*stride+kw, l] * out[:, i, j, k][:, np.newaxis, np.newaxis], axis=0) # 去掉填充的像素 out = out[:, padding:oh-padding, padding:ow-padding, :] return out, dw, db def max_pool2d(self, x, size=2, stride=2): n, h, w, c = x.shape # 输入数据的维度 oh, ow = int((h - size) / stride) + 1, int((w - size) / stride) + 1 # 输出特征图的尺寸 # 初始化输出特征图 out = np.zeros((n, oh, ow, c)) # 最大池化运算 for i in range(oh): for j in range(ow): out[:, i, j, :] = np.max(x[:, i*stride:i*stride+size, j*stride:j*stride+size, :], axis=(1,2)) return out def softmax(self, x): # 防止指数爆炸 exp_x = np.exp(x - np.max(x, axis=1, keepdims=True)) return exp_x / np.sum(exp_x, axis=1, keepdims=True) def forward(self, x): # 卷积 conv_out, conv_dw, conv_db = self.conv2d(x, self.conv_w, self.conv_b, padding=1) conv_out = np.maximum(0, conv_out) # ReLU激活函数 # 池化 pool_out = self.max_pool2d(conv_out, size=2, stride=2) # 全连接层 fc_in = np.reshape(pool_out, (pool_out.shape[0], -1)) fc_out = np.dot(fc_in, self.fc_w) + self.fc_b # 输出 out = self.softmax(fc_out) # 保存中间结果和梯度 self.conv_out = conv_out self.pool_out = pool_out self.fc_in = fc_in self.fc_out = fc_out self.conv_dw = conv_dw self.conv_db = conv_db self.fc_dw = np.dot(fc_in.T, (out - self.y)) # 计算全连接层的梯度 self.fc_db = np.sum(out - self.y, axis=0) # 计算偏置的梯度 return out def backward(self): # 反向传播 fc_in_grad = np.dot(self.conv_dw.reshape(-1, self.conv_w.shape[-1]), self.fc_dw.T) fc_in_grad = np.reshape(fc_in_grad, self.pool_out.shape) pool_out_grad = fc_in_grad * (self.conv_out > 0) conv_out_grad = np.zeros_like(self.conv_out) _, kh, kw, kn = self.conv_w.shape for i in range(self.conv_out.shape[1]): for j in range(self.conv_out.shape[2]): for k in range(kn): conv_out_grad[:, i:i+kh, j:j+kw, :] += np.sum(pool_out_grad[:, i:i+1, j:j+1, k:k+1] * self.conv_w[:, :, :, k:k+1], axis=-1) # 更新权重和偏置 self.conv_w -= self.lr * self.conv_dw / self.batch_size self.conv_b -= self.lr * self.conv_db / self.batch_size self.fc_w -= self.lr * self.fc_dw / self.batch_size self.fc_b -= self.lr * self.fc_db / self.batch_size def train(self, X_train, y_train): n_samples = X_train.shape[0] n_batches = int(np.ceil(n_samples / self.batch_size)) for i in range(self.epoch): for j in range(n_batches): # 获取当前批次的数据和标签 start_idx = j * self.batch_size end_idx = min(start_idx + self.batch_size, n_samples) X_batch = X_train[start_idx:end_idx] y_batch = y_train[start_idx:end_idx] self.y = np.zeros((y_batch.shape[0], 10)) self.y[np.arange(y_batch.shape[0]), y_batch] = 1 # 前向传播和反向传播 self.forward(X_batch) self.backward() # 计算损失函数并输出当前迭代次数和损失函数值 loss = -np.sum(self.y * np.log(self.fc_out)) / self.batch_size print("epoch: {}, loss: {}".format(i+1, loss)) def predict(self, X_test): # 前向传播获取预测结果 pred = np.argmax(self.forward(X_test), axis=1) return pred # 加载sklearn库中的手写数字数据集 digits = load_digits() X = digits.images y = digits.target # 将手写数字图片转换为28*28的像素矩阵 X = X.reshape(-1, 8, 8, 1) X = np.pad(X, ((0,0),(1,1),(1,1),(0,0)), mode='constant') # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y) # 创建卷积神经网络对象并训练 cnn = ConvolutionalNeuralNetwork() cnn.train(X_train, y_train) # 在测试集上进行预测并输出准确率 pred = cnn.predict(X_test) accuracy = np.mean(pred == y_test) print("accuracy:", accuracy) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值