python-张量:访问一个值

时间:2019-02-28 21:36:33

标签: python-3.x pytorch

下面给出的是VGG16模型的输出。命令<head> <title> Student Assignment Grades </title> </head> <body> <h1> Student Assignment Grades </h1> <table id="gradesTable" class="gradesTable" contenteditable> <caption> <STRONG>Student Grades </STRONG> </caption> <tr> <th id="StudentNames" contenteditable="false">Student Names</th> <th id="StudentID" contenteditable="false"> Student ID</th> <th id="StudentResults" contenteditable="false"> Assignment 1</th> <th id="StudentResults" contenteditable="false"> Assignment 2</th> <th id="StudentResults" contenteditable="false"> Assignment 3</th> <th id="StudentResults" contenteditable="false"> Assignment 4</th> <th id="StudentResults" contenteditable="false"> Assignment 5</th> <th id="Average" contenteditable="false"> Final Grade</th> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> <tr> <td contenteditable class="StudentName">"Your Name"</td> <td contenteditable class="StudentID">"Your Student Number"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable class="Assignment">"-"</td> <td contenteditable="false" class="Final Grade"> </td> </tr> </table> <!-- button to invoke the calculateAverage Function, styled with a coloured background --> <button class="btnCenter" onclick="getAverage()"> Calculate Final Grades</button> </body>的输出显示VGG16.classifier[6],我无法理解其工作原理。另外,如何打印线性值

Linear(in_features=25088, out_features=4096, bias=True)

1 个答案:

答案 0 :(得分:0)

in C++模型分为两组,分别称为featuresclassifier。您可以以VGG16.featuresVGG16.classifier的身份访问它们:

>>> VGG16 = torchvision.models.vgg16(pretrained=True)
>>> VGG16.classifier
Sequential(
  (0): Linear(in_features=25088, out_features=4096, bias=True)
  (1): ReLU(inplace)
  (2): Dropout(p=0.5)
  (3): Linear(in_features=4096, out_features=4096, bias=True)
  (4): ReLU(inplace)
  (5): Dropout(p=0.5)
  (6): Linear(in_features=4096, out_features=1000, bias=True)
)

此外,您可以使用索引访问这些图层组中的每个图层。例如,要访问模型的classifier部分的第一层,您可以执行以下操作:

>>> VGG16.classifier[0]  # first layer of classifier portion
Linear(in_features=25088, out_features=4096, bias=True)
# and so on...
>>> VGG16.classifier[3]  # fourth layer of classifier portion
Linear(in_features=4096, out_features=4096, bias=True)