赞
踩
import torch import torch.nn as nn class ConvModel(nn.Module): def __init__(self): super(ConvModel, self).__init__() # 定义卷积层 self.conv = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3, stride=1) self.pool = nn.MaxPool1d(kernel_size=2, stride=2) # 定义全连接层 self.fc1 = nn.Linear(in_features=3168, out_features=64) self.fc2 = nn.Linear(in_features=64, out_features=2) def forward(self, x): # 卷积操作 x = x.unsqueeze(1) x = self.conv(x) x = nn.functional.relu(x) x = self.pool(x) # 展平 x = torch.flatten(x, start_dim=1) # 全连接层 x = self.fc1(x) x = nn.functional.relu(x) x = self.fc2(x) return x input = torch.randn(64,200) model = ConvModel() output = model(input) for param in model.parameters(): print(param) for mods in model.modules(): print(mods) print(model.state_dict())
model.modules()
是一个PyTorch中的方法,它会返回一个包含模型所有组成层(Module)的迭代器,包括模型中嵌套的子层级。通过调用model.modules()方法可以遍历整个模型,并访问到每一层的名称、参数等信息。
具体来说,model.modules()返回的是一个生成器(generator),其中包括了整个模型的所有层级,包括父层级和子层级,但是不包括网络中的连接层和节点。这个生成器按照树形结构遍历整个模型,并按照层级有序输出每个层级。
ConvModel(
(conv): Conv1d(1, 32, kernel_size=(3,), stride=(1,))
(pool): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(fc1): Linear(in_features=3168, out_features=64, bias=True)
(fc2): Linear(in_features=64, out_features=2, bias=True)
)
Conv1d(1, 32, kernel_size=(3,), stride=(1,))
MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
Linear(in_features=3168, out_features=64, bias=True)
Linear(in_features=64, out_features=2, bias=True)
model.parameters()
是一个PyTorch中的方法,它会返回一个包含模型所有可训练参数的迭代器,这些参数可以被用于优化器的更新或者保存与加载模型。具体来说,model.parameters()返回的是一个生成器(generator),里面只有模型的参数,没有对应的网络层名称。
Parameter containing: tensor([[[-0.5126, 0.5331, 0.2893]], [[ 0.4555, -0.1294, 0.0125]], [[ 0.1551, 0.4624, 0.2719]], [[-0.4037, -0.5695, 0.2529]], [[ 0.0677, -0.5520, -0.4953]], [[ 0.0964, -0.5395, 0.0329]], [[-0.3732, 0.2867, 0.3513]], [[-0.2955, -0.0611, -0.5704]], [[-0.5759, 0.5438, 0.2083]], [[ 0.1635, -0.1977, 0.1897]], [[-0.1315, 0.0727, 0.4758]], [[ 0.5563, -0.1573, -0.0435]], [[ 0.4147, 0.3957, -0.0221]], [[-0.4292, 0.5694, 0.3400]], [[ 0.2282, -0.2961, -0.0415]], [[ 0.2947, 0.1851, 0.4878]], [[ 0.3008, 0.2580, 0.3571]], [[ 0.0821, 0.2338, -0.3678]], [[-0.1976, 0.1668, -0.1952]], [[ 0.4718, 0.2912, 0.0277]], [[ 0.0925, -0.5663, 0.4554]], [[-0.3790, 0.5210, 0.4064]], [[ 0.0432, 0.2155, -0.3406]], [[-0.2695, 0.4273, -0.2091]], [[ 0.5320, -0.2330, 0.3117]], [[-0.2894, 0.1364, 0.5271]], [[ 0.5254, 0.2866, 0.0733]], [[ 0.4851, -0.1229, -0.4983]], [[ 0.1804, 0.4486, -0.0619]], [[-0.5312, 0.5690, -0.3322]], [[ 0.0271, 0.2029, 0.0384]], [[-0.0130, -0.2701, 0.3163]]], requires_grad=True) Parameter containing: tensor([ 0.0342, 0.3380, 0.0396, 0.0872, 0.5354, 0.4468, -0.4234, -0.4960, 0.4873, -0.4876, 0.5049, -0.4638, -0.0434, -0.3543, -0.0406, -0.4049, 0.4760, -0.3240, -0.2755, -0.0046, 0.1204, -0.1654, 0.2997, 0.0625, -0.1786, -0.0292, 0.3281, -0.4937, -0.3622, -0.3028, 0.2650, 0.0713], requires_grad=True) Parameter containing: tensor([[ 0.0160, -0.0104, -0.0111, ..., 0.0129, 0.0091, 0.0010], [ 0.0101, 0.0083, 0.0145, ..., 0.0079, 0.0081, 0.0054], [-0.0059, -0.0004, 0.0028, ..., -0.0139, -0.0017, -0.0005], ..., [-0.0160, -0.0081, -0.0147, ..., 0.0150, -0.0108, 0.0030], [-0.0066, -0.0151, 0.0050, ..., -0.0126, -0.0041, 0.0100], [ 0.0141, -0.0058, -0.0081, ..., 0.0026, 0.0083, 0.0022]], requires_grad=True) Parameter containing: tensor([-1.4286e-02, -4.8758e-03, -9.6254e-03, 3.8352e-03, 1.7488e-02, 9.5369e-03, -1.2196e-02, -4.0154e-03, -1.5040e-02, -1.7715e-02, 1.4567e-02, -1.7208e-02, -1.7108e-02, -1.4707e-02, -1.2141e-03, 1.0333e-02, 1.3588e-02, 1.4712e-02, -1.3024e-02, -1.6053e-02, -1.0188e-02, -1.5649e-02, -7.5805e-03, -1.3627e-02, 1.4144e-02, 5.0014e-03, -1.0649e-02, -1.3394e-02, 4.9695e-03, 4.9918e-03, 1.1173e-02, -1.5761e-02, 1.4373e-02, 8.3734e-04, 2.2160e-05, -1.5278e-02, -1.6162e-02, 3.3545e-03, -7.0366e-03, 1.2571e-02, 1.0598e-02, 8.7718e-03, -5.5483e-03, 1.2729e-02, -1.5751e-02, 9.5723e-03, -1.7305e-02, -1.3221e-02, -6.7073e-03, -1.6645e-03, -1.2975e-02, 1.1121e-02, 1.4769e-02, 1.3430e-03, 6.8799e-03, 1.5452e-02, 1.4529e-02, 1.0451e-02, -7.1500e-03, -1.2924e-02, -7.7933e-03, -2.8951e-03, 8.1708e-03, -8.0176e-03], requires_grad=True) Parameter containing: tensor([[-0.0286, -0.1145, -0.0556, -0.0326, -0.1048, -0.1136, -0.1209, 0.0143, -0.0974, -0.1066, -0.0110, -0.1067, 0.0064, 0.0431, 0.0437, -0.0485, 0.0760, 0.0398, -0.0526, 0.0138, -0.0278, 0.0861, -0.0675, 0.1166, 0.0165, 0.0697, -0.0966, -0.0170, -0.1015, -0.1014, -0.0581, 0.1092, 0.1147, 0.0013, 0.0246, -0.0419, 0.1228, 0.0919, -0.0277, -0.0781, -0.0209, 0.0984, -0.0507, -0.0988, -0.0766, -0.1127, -0.0171, -0.0945, 0.0552, -0.1151, 0.1186, 0.0960, 0.0265, 0.1094, 0.1215, -0.0884, 0.0510, 0.0182, 0.0710, -0.0617, -0.0658, 0.0838, -0.0267, 0.0384], [ 0.0812, -0.0191, -0.0470, 0.0184, -0.0612, 0.0393, 0.1248, 0.0249, -0.1226, 0.0047, 0.0882, 0.0487, 0.1045, 0.0559, 0.0201, 0.0271, -0.0657, 0.0610, 0.0036, 0.1152, 0.0320, 0.0100, 0.0171, -0.0890, 0.0910, -0.0298, -0.0979, 0.0838, 0.0191, -0.0210, 0.0581, 0.0309, 0.1167, -0.0727, 0.0471, -0.0216, 0.0969, 0.0827, -0.0934, 0.0181, -0.0922, -0.0406, 0.0906, -0.0490, -0.0792, -0.0736, -0.0735, 0.1206, -0.0137, -0.0138, -0.1037, -0.0516, -0.1138, 0.0146, -0.0104, 0.0561, 0.0404, 0.0993, 0.0587, 0.0061, -0.0620, -0.0294, 0.0185, -0.0011]], requires_grad=True) Parameter containing: tensor([-0.0527, 0.1059], requires_grad=True)
model.state_dict()
是一个 PyTorch 中的方法,用于返回模型中可训练参数以及其对应值的字典(dictionary)。即将每个层的权重和偏差等参数保存到一个 Python 字典中,字典的 key 为参数的名称,value 为该参数的值,可以将这个字典用于后面的优化器、断点续训或者模型的保存和加载。state_dict() 方法的返回结果是一个 OrderedDict 类型的对象,其中包括了模型中所有可学习参数的名称和对应的张量值。由于 state_dict() 的输出仅仅是模型中可学习参数的值,因此在加载模型状态时,必须已经实例化相应的网络结构,并且网络结构必须与之前保存的模型相同才能进行恢复。
OrderedDict([('conv.weight', tensor([[[-0.5126, 0.5331, 0.2893]], [[ 0.4555, -0.1294, 0.0125]], [[ 0.1551, 0.4624, 0.2719]], [[-0.4037, -0.5695, 0.2529]], [[ 0.0677, -0.5520, -0.4953]], [[ 0.0964, -0.5395, 0.0329]], [[-0.3732, 0.2867, 0.3513]], [[-0.2955, -0.0611, -0.5704]], [[-0.5759, 0.5438, 0.2083]], [[ 0.1635, -0.1977, 0.1897]], [[-0.1315, 0.0727, 0.4758]], [[ 0.5563, -0.1573, -0.0435]], [[ 0.4147, 0.3957, -0.0221]], [[-0.4292, 0.5694, 0.3400]], [[ 0.2282, -0.2961, -0.0415]], [[ 0.2947, 0.1851, 0.4878]], [[ 0.3008, 0.2580, 0.3571]], [[ 0.0821, 0.2338, -0.3678]], [[-0.1976, 0.1668, -0.1952]], [[ 0.4718, 0.2912, 0.0277]], [[ 0.0925, -0.5663, 0.4554]], [[-0.3790, 0.5210, 0.4064]], [[ 0.0432, 0.2155, -0.3406]], [[-0.2695, 0.4273, -0.2091]], [[ 0.5320, -0.2330, 0.3117]], [[-0.2894, 0.1364, 0.5271]], [[ 0.5254, 0.2866, 0.0733]], [[ 0.4851, -0.1229, -0.4983]], [[ 0.1804, 0.4486, -0.0619]], [[-0.5312, 0.5690, -0.3322]], [[ 0.0271, 0.2029, 0.0384]], [[-0.0130, -0.2701, 0.3163]]])), ('conv.bias', tensor([ 0.0342, 0.3380, 0.0396, 0.0872, 0.5354, 0.4468, -0.4234, -0.4960, 0.4873, -0.4876, 0.5049, -0.4638, -0.0434, -0.3543, -0.0406, -0.4049, 0.4760, -0.3240, -0.2755, -0.0046, 0.1204, -0.1654, 0.2997, 0.0625, -0.1786, -0.0292, 0.3281, -0.4937, -0.3622, -0.3028, 0.2650, 0.0713])), ('fc1.weight', tensor([[ 0.0160, -0.0104, -0.0111, ..., 0.0129, 0.0091, 0.0010], [ 0.0101, 0.0083, 0.0145, ..., 0.0079, 0.0081, 0.0054], [-0.0059, -0.0004, 0.0028, ..., -0.0139, -0.0017, -0.0005], ..., [-0.0160, -0.0081, -0.0147, ..., 0.0150, -0.0108, 0.0030], [-0.0066, -0.0151, 0.0050, ..., -0.0126, -0.0041, 0.0100], [ 0.0141, -0.0058, -0.0081, ..., 0.0026, 0.0083, 0.0022]])), ('fc1.bias', tensor([-1.4286e-02, -4.8758e-03, -9.6254e-03, 3.8352e-03, 1.7488e-02, 9.5369e-03, -1.2196e-02, -4.0154e-03, -1.5040e-02, -1.7715e-02, 1.4567e-02, -1.7208e-02, -1.7108e-02, -1.4707e-02, -1.2141e-03, 1.0333e-02, 1.3588e-02, 1.4712e-02, -1.3024e-02, -1.6053e-02, -1.0188e-02, -1.5649e-02, -7.5805e-03, -1.3627e-02, 1.4144e-02, 5.0014e-03, -1.0649e-02, -1.3394e-02, 4.9695e-03, 4.9918e-03, 1.1173e-02, -1.5761e-02, 1.4373e-02, 8.3734e-04, 2.2160e-05, -1.5278e-02, -1.6162e-02, 3.3545e-03, -7.0366e-03, 1.2571e-02, 1.0598e-02, 8.7718e-03, -5.5483e-03, 1.2729e-02, -1.5751e-02, 9.5723e-03, -1.7305e-02, -1.3221e-02, -6.7073e-03, -1.6645e-03, -1.2975e-02, 1.1121e-02, 1.4769e-02, 1.3430e-03, 6.8799e-03, 1.5452e-02, 1.4529e-02, 1.0451e-02, -7.1500e-03, -1.2924e-02, -7.7933e-03, -2.8951e-03, 8.1708e-03, -8.0176e-03])), ('fc2.weight', tensor([[-0.0286, -0.1145, -0.0556, -0.0326, -0.1048, -0.1136, -0.1209, 0.0143, -0.0974, -0.1066, -0.0110, -0.1067, 0.0064, 0.0431, 0.0437, -0.0485, 0.0760, 0.0398, -0.0526, 0.0138, -0.0278, 0.0861, -0.0675, 0.1166, 0.0165, 0.0697, -0.0966, -0.0170, -0.1015, -0.1014, -0.0581, 0.1092, 0.1147, 0.0013, 0.0246, -0.0419, 0.1228, 0.0919, -0.0277, -0.0781, -0.0209, 0.0984, -0.0507, -0.0988, -0.0766, -0.1127, -0.0171, -0.0945, 0.0552, -0.1151, 0.1186, 0.0960, 0.0265, 0.1094, 0.1215, -0.0884, 0.0510, 0.0182, 0.0710, -0.0617, -0.0658, 0.0838, -0.0267, 0.0384], [ 0.0812, -0.0191, -0.0470, 0.0184, -0.0612, 0.0393, 0.1248, 0.0249, -0.1226, 0.0047, 0.0882, 0.0487, 0.1045, 0.0559, 0.0201, 0.0271, -0.0657, 0.0610, 0.0036, 0.1152, 0.0320, 0.0100, 0.0171, -0.0890, 0.0910, -0.0298, -0.0979, 0.0838, 0.0191, -0.0210, 0.0581, 0.0309, 0.1167, -0.0727, 0.0471, -0.0216, 0.0969, 0.0827, -0.0934, 0.0181, -0.0922, -0.0406, 0.0906, -0.0490, -0.0792, -0.0736, -0.0735, 0.1206, -0.0137, -0.0138, -0.1037, -0.0516, -0.1138, 0.0146, -0.0104, 0.0561, 0.0404, 0.0993, 0.0587, 0.0061, -0.0620, -0.0294, 0.0185, -0.0011]])), ('fc2.bias', tensor([-0.0527, 0.1059]))])
model.state_dict()的迭代输出: conv.weight tensor([[[-0.5126, 0.5331, 0.2893]], [[ 0.4555, -0.1294, 0.0125]], [[ 0.1551, 0.4624, 0.2719]], [[-0.4037, -0.5695, 0.2529]], [[ 0.0677, -0.5520, -0.4953]], [[ 0.0964, -0.5395, 0.0329]], [[-0.3732, 0.2867, 0.3513]], [[-0.2955, -0.0611, -0.5704]], [[-0.5759, 0.5438, 0.2083]], [[ 0.1635, -0.1977, 0.1897]], [[-0.1315, 0.0727, 0.4758]], [[ 0.5563, -0.1573, -0.0435]], [[ 0.4147, 0.3957, -0.0221]], [[-0.4292, 0.5694, 0.3400]], [[ 0.2282, -0.2961, -0.0415]], [[ 0.2947, 0.1851, 0.4878]], [[ 0.3008, 0.2580, 0.3571]], [[ 0.0821, 0.2338, -0.3678]], [[-0.1976, 0.1668, -0.1952]], [[ 0.4718, 0.2912, 0.0277]], [[ 0.0925, -0.5663, 0.4554]], [[-0.3790, 0.5210, 0.4064]], [[ 0.0432, 0.2155, -0.3406]], [[-0.2695, 0.4273, -0.2091]], [[ 0.5320, -0.2330, 0.3117]], [[-0.2894, 0.1364, 0.5271]], [[ 0.5254, 0.2866, 0.0733]], [[ 0.4851, -0.1229, -0.4983]], [[ 0.1804, 0.4486, -0.0619]], [[-0.5312, 0.5690, -0.3322]], [[ 0.0271, 0.2029, 0.0384]], [[-0.0130, -0.2701, 0.3163]]]) conv.bias tensor([ 0.0342, 0.3380, 0.0396, 0.0872, 0.5354, 0.4468, -0.4234, -0.4960, 0.4873, -0.4876, 0.5049, -0.4638, -0.0434, -0.3543, -0.0406, -0.4049, 0.4760, -0.3240, -0.2755, -0.0046, 0.1204, -0.1654, 0.2997, 0.0625, -0.1786, -0.0292, 0.3281, -0.4937, -0.3622, -0.3028, 0.2650, 0.0713]) fc1.weight tensor([[ 0.0160, -0.0104, -0.0111, ..., 0.0129, 0.0091, 0.0010], [ 0.0101, 0.0083, 0.0145, ..., 0.0079, 0.0081, 0.0054], [-0.0059, -0.0004, 0.0028, ..., -0.0139, -0.0017, -0.0005], ..., [-0.0160, -0.0081, -0.0147, ..., 0.0150, -0.0108, 0.0030], [-0.0066, -0.0151, 0.0050, ..., -0.0126, -0.0041, 0.0100], [ 0.0141, -0.0058, -0.0081, ..., 0.0026, 0.0083, 0.0022]]) fc1.bias tensor([-1.4286e-02, -4.8758e-03, -9.6254e-03, 3.8352e-03, 1.7488e-02, 9.5369e-03, -1.2196e-02, -4.0154e-03, -1.5040e-02, -1.7715e-02, 1.4567e-02, -1.7208e-02, -1.7108e-02, -1.4707e-02, -1.2141e-03, 1.0333e-02, 1.3588e-02, 1.4712e-02, -1.3024e-02, -1.6053e-02, -1.0188e-02, -1.5649e-02, -7.5805e-03, -1.3627e-02, 1.4144e-02, 5.0014e-03, -1.0649e-02, -1.3394e-02, 4.9695e-03, 4.9918e-03, 1.1173e-02, -1.5761e-02, 1.4373e-02, 8.3734e-04, 2.2160e-05, -1.5278e-02, -1.6162e-02, 3.3545e-03, -7.0366e-03, 1.2571e-02, 1.0598e-02, 8.7718e-03, -5.5483e-03, 1.2729e-02, -1.5751e-02, 9.5723e-03, -1.7305e-02, -1.3221e-02, -6.7073e-03, -1.6645e-03, -1.2975e-02, 1.1121e-02, 1.4769e-02, 1.3430e-03, 6.8799e-03, 1.5452e-02, 1.4529e-02, 1.0451e-02, -7.1500e-03, -1.2924e-02, -7.7933e-03, -2.8951e-03, 8.1708e-03, -8.0176e-03]) fc2.weight tensor([[-0.0286, -0.1145, -0.0556, -0.0326, -0.1048, -0.1136, -0.1209, 0.0143, -0.0974, -0.1066, -0.0110, -0.1067, 0.0064, 0.0431, 0.0437, -0.0485, 0.0760, 0.0398, -0.0526, 0.0138, -0.0278, 0.0861, -0.0675, 0.1166, 0.0165, 0.0697, -0.0966, -0.0170, -0.1015, -0.1014, -0.0581, 0.1092, 0.1147, 0.0013, 0.0246, -0.0419, 0.1228, 0.0919, -0.0277, -0.0781, -0.0209, 0.0984, -0.0507, -0.0988, -0.0766, -0.1127, -0.0171, -0.0945, 0.0552, -0.1151, 0.1186, 0.0960, 0.0265, 0.1094, 0.1215, -0.0884, 0.0510, 0.0182, 0.0710, -0.0617, -0.0658, 0.0838, -0.0267, 0.0384], [ 0.0812, -0.0191, -0.0470, 0.0184, -0.0612, 0.0393, 0.1248, 0.0249, -0.1226, 0.0047, 0.0882, 0.0487, 0.1045, 0.0559, 0.0201, 0.0271, -0.0657, 0.0610, 0.0036, 0.1152, 0.0320, 0.0100, 0.0171, -0.0890, 0.0910, -0.0298, -0.0979, 0.0838, 0.0191, -0.0210, 0.0581, 0.0309, 0.1167, -0.0727, 0.0471, -0.0216, 0.0969, 0.0827, -0.0934, 0.0181, -0.0922, -0.0406, 0.0906, -0.0490, -0.0792, -0.0736, -0.0735, 0.1206, -0.0137, -0.0138, -0.1037, -0.0516, -0.1138, 0.0146, -0.0104, 0.0561, 0.0404, 0.0993, 0.0587, 0.0061, -0.0620, -0.0294, 0.0185, -0.0011]]) fc2.bias tensor([-0.0527, 0.1059])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。