help in example

111391: <p>class NeuralNet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(NeuralNet, self).<strong>init</strong>()<br>
self.flatten = nn.Flatten(1, -1)<br>
self.layer1= nn.Linear(28*28, 128)<br>
self.drop = nn.Dropout(0.2)<br>
self.layer2 = nn.Linear(128, 10)<br>
self.relu = nn.ReLU()<br>
self.softmax=nn.Softmax()</p>
<pre><code>def forward(self, x):
    x = self.flatten(x)
    x = self.relu(self.layer1(x)) 
    x = self.drop(x)
    x = self.layer2(x) 
    x=self.softmax(x)
    return x
</code></pre>
<p>model = NeuralNet().to(‘cpu’)</p>
<p>height, width = 28, 128<br>
x = Variable(torch.FloatTensor(1,height, width), requires_grad = True)<br>
y = Variable(torch.FloatTensor(height,10), requires_grad = False)</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.Adam(model.parameters(), lr=0.001)</p>
<p>for epoch in range(20):<br>
running_loss = 0.0</p>
<pre><code>optimizer.zero_grad()
y_pred = model(x)
print(y_pred)
print(y)
loss = criterion(y_pred, y)
loss.backward()
optimizer.step()

# выводим статистику о процессе обучения
running_loss += loss.item()

print('[%d] loss: %.3f' %(epoch + 1, running_loss / 2000))
</code></pre>
<p>print(‘Training is finished!’)<br>
Ukladnikov_Andrey_Lesson1 - Jupyter Notebook .MathJax_Hover_Frame {border-radius: .25em; -webkit-border-radius: .25em; -moz-border-radius: .25em; -khtml-border-radius: .25em; box-shadow: 0px 0px 15px <span class="hashtag">#83A</span>; -webkit-box-shadow: 0px 0px 15px <span class="hashtag">#83A</span>; -moz-box-shadow: 0px 0px 15px <span class="hashtag">#83A</span>; -khtml-box-shadow: 0px 0px 15px <span class="hashtag">#83A</span>; border: 1px solid <span class="hashtag">#A6D</span> ! important; display: inline-block; position: absolute} .MathJax_Menu_Button .MathJax_Hover_Arrow {position: absolute; cursor: pointer; display: inline-block; border: 2px solid <span class="hashtag">#AAA</span>; border-radius: 4px; -webkit-border-radius: 4px; -moz-border-radius: 4px; -khtml-border-radius: 4px; font-family: ‘Courier New’,Courier; font-size: 9px; color: <span class="hashtag">#F0F0F0</span>} .MathJax_Menu_Button .MathJax_Hover_Arrow span {display: block; background-color: <span class="hashtag">#AAA</span>; border: 1px solid; border-radius: 3px; line-height: 0; padding: 4px} .MathJax_Hover_Arrow:hover {color: white!important; border: 2px solid <span class="hashtag">#CCC</span>!important} .MathJax_Hover_Arrow:hover span {background-color: <span class="hashtag">#CCC</span>!important} <span class="hashtag">#MathJax_About</span> {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: <span class="hashtag">#DDDDDD</span>; color: black; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px <span class="hashtag">#808080</span>; -webkit-box-shadow: 0px 10px 20px <span class="hashtag">#808080</span>; -moz-box-shadow: 0px 10px 20px <span class="hashtag">#808080</span>; -khtml-box-shadow: 0px 10px 20px <span class="hashtag">#808080</span>} <span class="hashtag">#MathJax_About</span>.MathJax_MousePost {outline: none} .MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 2px; border: 1px solid <span class="hashtag">#CCCCCC</span>; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; box-shadow: 0px 10px 20px <span class="hashtag">#808080</span>; -webkit-box-shadow: 0px 10px 20px <span class="hashtag">#808080</span>; -moz-box-shadow: 0px 10px 20px <span class="hashtag">#808080</span>; -khtml-box-shadow: 0px 10px 20px <span class="hashtag">#808080</span>} .MathJax_MenuItem {padding: 2px 2em; background: transparent} .MathJax_MenuArrow {position: absolute; right: .5em; padding-top: .25em; color: <span class="hashtag">#666666</span>; font-family: ‘Arial unicode MS’; font-size: .75em} .MathJax_MenuActive .MathJax_MenuArrow {color: white} .MathJax_MenuArrow.RTL {left: .5em; right: auto} .MathJax_MenuCheck {position: absolute; left: .7em; font-family: ‘Arial unicode MS’} .MathJax_MenuCheck.RTL {right: .7em; left: auto} .MathJax_MenuRadioCheck {position: absolute; left: 1em} .MathJax_MenuRadioCheck.RTL {right: 1em; left: auto} .MathJax_MenuLabel {padding: 2px 2em 4px 1.33em; font-style: italic} .MathJax_MenuRule {border-top: 1px solid <span class="hashtag">#CCCCCC</span>; margin: 4px 1px 0px} .MathJax_MenuDisabled {color: GrayText} .MathJax_MenuActive {background-color: Highlight; color: HighlightText} .MathJax_MenuDisabled:focus, .MathJax_MenuLabel:focus {background-color: <span class="hashtag">#E8E8E8</span>} .MathJax_ContextMenu:focus {outline: none} .MathJax_ContextMenu .MathJax_MenuItem:focus {outline: none} <span class="hashtag">#MathJax_AboutClose</span> {top: .2em; right: .2em} .MathJax_Menu .MathJax_MenuClose {top: -10px; left: -10px} .MathJax_MenuClose {position: absolute; cursor: pointer; display: inline-block; border: 2px solid <span class="hashtag">#AAA</span>; border-radius: 18px; -webkit-border-radius: 18px; -moz-border-radius: 18px; -khtml-border-radius: 18px; font-family: ‘Courier New’,Courier; font-size: 24px; color: <span class="hashtag">#F0F0F0</span>} .MathJax_MenuClose span {display: block; background-color: <span class="hashtag">#AAA</span>; border: 1.5px solid; border-radius: 18px; -webkit-border-radius: 18px; -moz-border-radius: 18px; -khtml-border-radius: 18px; line-height: 0; padding: 8px 0 6px} .MathJax_MenuClose:hover {color: white!important; border: 2px solid <span class="hashtag">#CCC</span>!important} .MathJax_MenuClose:hover span {background-color: <span class="hashtag">#CCC</span>!important} .MathJax_MenuClose:hover:focus {outline: none} .MathJax_Preview .MJXf-math {color: inherit!important} .MJX_Assistive_MathML {position: absolute!important; top: 0; left: 0; clip: rect(1px, 1px, 1px, 1px); padding: 1px 0 0 0!important; border: 0!important; height: 1px!important; width: 1px!important; overflow: hidden!important; display: block!important; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none} .MJX_Assistive_MathML.MJX_Assistive_MathML_Block {width: 100%!important} <span class="hashtag">#MathJax_Zoom</span> {position: absolute; background-color: <span class="hashtag">#F0F0F0</span>; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; -webkit-box-sizing: content-box; -moz-box-sizing: content-box; box-sizing: content-box; box-shadow: 5px 5px 15px <span class="hashtag">#AAAAAA</span>; -webkit-box-shadow: 5px 5px 15px <span class="hashtag">#AAAAAA</span>; -moz-box-shadow: 5px 5px 15px <span class="hashtag">#AAAAAA</span>; -khtml-box-shadow: 5px 5px 15px <span class="hashtag">#AAAAAA</span>} <span class="hashtag">#MathJax_ZoomOverlay</span> {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)} <span class="hashtag">#MathJax_ZoomFrame</span> {position: relative; display: inline-block; height: 0; width: 0} <span class="hashtag">#MathJax_ZoomEventTrap</span> {position: absolute; left: 0; top: 0; z-index: 302; display: inline-block; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)} .MathJax_Preview {color: <span class="hashtag">#888</span>; display: contents} <span class="hashtag">#MathJax_Message</span> {position: fixed; left: 1px; bottom: 2px; background-color: <span class="hashtag">#E6E6E6</span>; border: 1px solid <span class="hashtag">#959595</span>; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap} <span class="hashtag">#MathJax_MSIE_Frame</span> {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px} .MathJax_Error {color: <span class="hashtag">#CC0000</span>; font-style: italic} div.MathJax_MathML {text-align: center; margin: .75em 0px; display: block!important} .MathJax_MathML {font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} span.MathJax_MathML {display: inline!important} .MathJax_mmlExBox {display: block!important; overflow: hidden; height: 1px; width: 60ex; min-height: 0; max-height: none; padding: 0; border: 0; margin: 0} .MJXp-script {font-size: .8em} .MJXp-right {-webkit-transform-origin: right; -moz-transform-origin: right; -ms-transform-origin: right; -o-transform-origin: right; transform-origin: right} .MJXp-bold {font-weight: bold} .MJXp-italic {font-style: italic} .MJXp-scr {font-family: MathJax_Script,‘Times New Roman’,Times,STIXGeneral,serif} .MJXp-frak {font-family: MathJax_Fraktur,‘Times New Roman’,Times,STIXGeneral,serif} .MJXp-sf {font-family: MathJax_SansSerif,‘Times New Roman’,Times,STIXGeneral,serif} .MJXp-cal {font-family: MathJax_Caligraphic,‘Times New Roman’,Times,STIXGeneral,serif} .MJXp-mono {font-family: MathJax_Typewriter,‘Times New Roman’,Times,STIXGeneral,serif} .MJXp-largeop {font-size: 150%} .MJXp-largeop.MJXp-int {vertical-align: -.2em} .MJXp-math {display: inline-block; line-height: 1.2; text-indent: 0; font-family: ‘Times New Roman’,Times,STIXGeneral,serif; white-space: nowrap; border-collapse: collapse} .MJXp-display {display: block; text-align: center; margin: 1em 0} .MJXp-math span {display: inline-block} .MJXp-box {display: block!important; text-align: center} .MJXp-box:after {content: " "} .MJXp-rule {display: block!important; margin-top: .1em} .MJXp-char {display: block!important} .MJXp-mo {margin: 0 .15em} .MJXp-mfrac {margin: 0 .125em; vertical-align: .25em} .MJXp-denom {display: inline-table!important; width: 100%} .MJXp-denom &gt; * {display: table-row!important} .MJXp-surd {vertical-align: top} .MJXp-surd &gt; * {display: block!important} .MJXp-script-box &gt; * {display: table!important; height: 50%} .MJXp-script-box &gt; * &gt; * {display: table-cell!important; vertical-align: top} .MJXp-script-box &gt; *:last-child &gt; * {vertical-align: bottom} .MJXp-script-box &gt; * &gt; * &gt; * {display: block!important} .MJXp-mphantom {visibility: hidden} .MJXp-munderover, .MJXp-munder {display: inline-table!important} .MJXp-over {display: inline-block!important; text-align: center} .MJXp-over &gt; * {display: block!important} .MJXp-munderover &gt; *, .MJXp-munder &gt; * {display: table-row!important} .MJXp-mtable {vertical-align: .25em; margin: 0 .125em} .MJXp-mtable &gt; * {display: inline-table!important; vertical-align: middle} .MJXp-mtr {display: table-row!important} .MJXp-mtd {display: table-cell!important; text-align: center; padding: .5em 0 0 .5em} .MJXp-mtr &gt; .MJXp-mtd:first-child {padding-left: 0} .MJXp-mtr:first-child &gt; .MJXp-mtd {padding-top: 0} .MJXp-mlabeledtr {display: table-row!important} .MJXp-mlabeledtr &gt; .MJXp-mtd:first-child {padding-left: 0} .MJXp-mlabeledtr:first-child &gt; .MJXp-mtd {padding-top: 0} .MJXp-merror {background-color: <span class="hashtag">#FFFF88</span>; color: <span class="hashtag">#CC0000</span>; border: 1px solid <span class="hashtag">#CC0000</span>; padding: 1px 3px; font-style: normal; font-size: 90%} .MJXp-scale0 {-webkit-transform: scaleX(.0); -moz-transform: scaleX(.0); -ms-transform: scaleX(.0); -o-transform: scaleX(.0); transform: scaleX(.0)} .MJXp-scale1 {-webkit-transform: scaleX(.1); -moz-transform: scaleX(.1); -ms-transform: scaleX(.1); -o-transform: scaleX(.1); transform: scaleX(.1)} .MJXp-scale2 {-webkit-transform: scaleX(.2); -moz-transform: scaleX(.2); -ms-transform: scaleX(.2); -o-transform: scaleX(.2); transform: scaleX(.2)} .MJXp-scale3 {-webkit-transform: scaleX(.3); -moz-transform: scaleX(.3); -ms-transform: scaleX(.3); -o-transform: scaleX(.3); transform: scaleX(.3)} .MJXp-scale4 {-webkit-transform: scaleX(.4); -moz-transform: scaleX(.4); -ms-transform: scaleX(.4); -o-transform: scaleX(.4); transform: scaleX(.4)} .MJXp-scale5 {-webkit-transform: scaleX(.5); -moz-transform: scaleX(.5); -ms-transform: scaleX(.5); -o-transform: scaleX(.5); transform: scaleX(.5)} .MJXp-scale6 {-webkit-transform: scaleX(.6); -moz-transform: scaleX(.6); -ms-transform: scaleX(.6); -o-transform: scaleX(.6); transform: scaleX(.6)} .MJXp-scale7 {-webkit-transform: scaleX(.7); -moz-transform: scaleX(.7); -ms-transform: scaleX(.7); -o-transform: scaleX(.7); transform: scaleX(.7)} .MJXp-scale8 {-webkit-transform: scaleX(.8); -moz-transform: scaleX(.8); -ms-transform: scaleX(.8); -o-transform: scaleX(.8); transform: scaleX(.8)} .MJXp-scale9 {-webkit-transform: scaleX(.9); -moz-transform: scaleX(.9); -ms-transform: scaleX(.9); -o-transform: scaleX(.9); transform: scaleX(.9)} .MathJax_PHTML .noError {vertical-align: -2px; font-size: 90%; text-align: left; color: black; padding: 1px 3px; border: 1px solid} .MathJax_Display {text-align: center; margin: 0; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax .merror {background-color: <span class="hashtag">#FFFF88</span>; color: <span class="hashtag">#CC0000</span>; border: 1px solid <span class="hashtag">#CC0000</span>; padding: 1px 3px; font-style: normal; font-size: 90%} .MathJax .MJX-monospace {font-family: monospace} .MathJax .MJX-sans-serif {font-family: sans-serif} <span class="hashtag">#MathJax_Tooltip</span> {background-color: InfoBackground; color: InfoText; border: 1px solid black; box-shadow: 2px 2px 5px <span class="hashtag">#AAAAAA</span>; -webkit-box-shadow: 2px 2px 5px <span class="hashtag">#AAAAAA</span>; -moz-box-shadow: 2px 2px 5px <span class="hashtag">#AAAAAA</span>; -khtml-box-shadow: 2px 2px 5px <span class="hashtag">#AAAAAA</span>; padding: 3px 4px; z-index: 401; position: absolute; left: 0; top: 0; width: auto; height: auto; display: none} .MathJax {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax:focus, body :focus .MathJax {display: inline-table} .MathJax.MathJax_FullWidth {text-align: center; display: table-cell!important; width: 10000em!important} .MathJax img, .MathJax nobr, .MathJax a {border: 0; padding: 0; margin: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; vertical-align: 0; line-height: normal; text-decoration: none} img.MathJax_strut {border: 0!important; padding: 0!important; margin: 0!important; vertical-align: 0!important} .MathJax span {display: inline; position: static; border: 0; padding: 0; margin: 0; vertical-align: 0; line-height: normal; text-decoration: none; box-sizing: content-box} .MathJax nobr {white-space: nowrap!important} .MathJax img {display: inline!important; float: none!important} .MathJax * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .MathJax_Processing {visibility: hidden; position: fixed; width: 0; height: 0; overflow: hidden} .MathJax_Processed {display: none!important} .MathJax_test {font-style: normal; font-weight: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-transform: none; letter-spacing: normal; word-spacing: normal; overflow: hidden; height: 1px} .MathJax_test.mjx-test-display {display: table!important} .MathJax_test.mjx-test-inline {display: inline!important; margin-right: -1px} .MathJax_test.mjx-test-default {display: block!important; clear: both} .MathJax_ex_box {display: inline-block!important; position: absolute; overflow: hidden; min-height: 0; max-height: none; padding: 0; border: 0; margin: 0; width: 1px; height: 60ex} .MathJax_em_box {display: inline-block!important; position: absolute; overflow: hidden; min-height: 0; max-height: none; padding: 0; border: 0; margin: 0; width: 1px; height: 60em} .mjx-test-inline .MathJax_left_box {display: inline-block; width: 0; float: left} .mjx-test-inline .MathJax_right_box {display: inline-block; width: 0; float: right} .mjx-test-display .MathJax_right_box {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} .MathJax .MathJax_HitBox {cursor: text; background: white; opacity: 0; filter: alpha(opacity=0)} .MathJax .MathJax_HitBox * {filter: none; opacity: 1; background: transparent} <span class="hashtag">#MathJax_Tooltip</span> * {filter: none; opacity: 1; background: transparent} <span class="mention">@font-face</span> {font-family: MathJax_Blank; src: url(‘about:blank’)} .MathJax .noError {vertical-align: -2px; font-size: 90%; text-align: left; color: black; padding: 1px 3px; border: 1px solid}</p>
<p>RuntimeError: size mismatch, m1: [1 x 3584], m2: [784 x 128] at C:\Users\builder\AppData\Local\Temp\pip-req-build-e5c8dddg\aten\src\TH/generic/THTensorMath.cpp:136</p>

pchandrasekaran: <aside class="quote no-group quote-modified" data-username="111391" data-post="1" data-topic="98836">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/111391/48/29620_2.png" class="avatar"> 111391:</div>
<blockquote>
<p>self.layer1= nn.Linear(28*28, 128)<br>
height, width = 28, 128</p>
</blockquote>
</aside>
<p>You’ve provided the wrong height and width values. When flattened, 28x128 gives a tensor of length 3584, but your nn.Linear layer expects a 28x28 (784) value.</p>