how to solve this probolem ?i try it two days

username: yingchaoxue629
cooked: <p>D:\programdate\anaconda3\envs\pytorch\python.exe C:/Users/11067/Desktop/SAE_pytorch-master/AE_Train.py<br>
已归一化的训练集，大小为： (310, 404)<br>
217<br>
92<br>
Traceback (most recent call last):<br>
File “C:/Users/11067/Desktop/SAE_pytorch-master/AE_Train.py”, line 37, in <br>
encoded, decoded = ae(b_x)<br>
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py”, line 727, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “C:\Users\11067\Desktop\SAE_pytorch-master\AE_ModelConstruction.py”, line 55, in forward<br>
encoded=self.encoder(x)<br>
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py”, line 727, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\container.py”, line 117, in forward<br>
input = module(input)<br>
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py”, line 727, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\batchnorm.py”, line 136, in forward<br>
self.weight, self.bias, bn_training, exponential_average_factor, self.eps)<br>
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py”, line 2058, in batch_norm<br>
training, momentum, eps, torch.backends.cudnn.enabled<br>
RuntimeError: running_mean should contain 4 elements not 3</p>
<p>import torch<br>
from torch import nn<br>
from scipy import io<br>
import numpy as np<br>
import torch.utils.data as Data<br>
import pandas as pd<br>
ACTIVATION = torch.nn.LeakyReLU()<br>
import <a href="http://scipy.io" rel="noopener nofollow ugc">scipy.io</a> as scio</p>
<p>class autoencoder(nn.Module):<br>
def <strong>init</strong>(self,input_num,layer,batch_normalization=False):<br>
super(autoencoder,self).<strong>init</strong>()<br>
self.do_bn=batch_normalization<br>
self.encoder=nn.Sequential()<br>
for i in range(layer):<br>
# encoder第一层<br>
if i == 0:<br>
self.encoder.add_module(‘encoder_linear%i’ % (i + 1), nn.Linear(input_num, 2 ** (layer + 2)))<br>
if self.do_bn:<br>
self.encoder.add_module(‘encoder_BN%i’ % (i + 1),<br>
nn.BatchNorm1d(2 ** (layer + 2), momentum=0.5))<br>
self.encoder.add_module(‘encoder_actFun%i’ % (i + 1), ACTIVATION)<br>
# encoder最后一层<br>
elif i == (layer - 1):<br>
self.encoder.add_module(‘encoder_linear%i’ % (i + 1), nn.Linear(16, 4))<br>
if self.do_bn:<br>
self.encoder.add_module(‘encoder_BN%i’ % (i + 1),nn.BatchNorm1d(3,momentum=0.5))<br>
# encoder其它层<br>
else:<br>
self.encoder.add_module(‘encoder_linear%i’%(i+1),nn.Linear(2 ** (layer - i + 3),2 **(layer-i+2)))<br>
if self.do_bn:<br>
self.encoder.add_module(‘encoder_BN%i’ % (i + 1),<br>
nn.BatchNorm1d(2 ** (layer - i + 2), momentum=0.5))<br>
self.encoder.add_module(‘encoder_actFun%i’ % (i + 1), ACTIVATION)<br>
self.decoder = nn.Sequential()<br>
for i in range(layer):<br>
# decoder第一层<br>
if i == 0:<br>
self.decoder.add_module(‘decoder_linear%i’ % (i + 1), nn.Linear(4, 16))<br>
if self.do_bn:<br>
self.decoder.add_module(‘decoder_BN%i’ % (i + 1), nn.BatchNorm1d(16, momentum=0.5))<br>
self.decoder.add_module(‘decoder_actFun%i’ % (i + 1), ACTIVATION)<br>
<span class="hashtag">#decoder最后一层</span><br>
elif i == (layer - 1):<br>
self.decoder.add_module(‘decoder_linear%i’ % (i + 1), nn.Linear(2 ** (layer+ 2), input_num))<br>
self.decoder.add_module(‘decoder_actFun%i’ % (i + 1), nn.Sigmoid())<br>
# 其它层<br>
else:<br>
self.decoder.add_module(‘encoder_linear%i’ % (i + 1), nn.Linear(2 ** (3+i),2 ** (4+i)))<br>
if self.do_bn:<br>
self.decoder.add_module(‘encoder_BN%i’ % (i + 1),nn.BatchNorm1d(2 ** (4+i), momentum=0.5))<br>
self.decoder.add_module(‘encoder_actFun%i’ % (i + 1), ACTIVATION)<br>
def forward(self, x):<br>
encoded=self.encoder(x)<br>
decoded=self.decoder(encoded)<br>
return encoded,decoded<br>
<span class="hashtag">#导入训练集并归一化</span>,训练集可以为包含多个文件的列表<br>
def Traindata(name_list,if_nor=True):<br>
filepath = ‘C:/Users/11067/Desktop/SAE_pytorch-master/Matrix1.mat’<br>
dict_data = scio.loadmat(filepath)<br>
np_D = dict_data[‘Matrix’]<br>
np_D = np_D[:, 3:]  # 去掉不需要的前四列<br>
np_Dmax, np_Dmin = np_D.max(axis=0), np_D.min(axis=0)</p>
<pre><code># #name_list:文件名字列表；if_nor:是否归一化
# column_num = 407 #文件中总列数为18，但是前四列没有用到，后面会删除。
# np_D = np.zeros((1, column_num))#构造一个1行18列的全零向量
# # for i in range(len(name_list)):#循环读取各个文件数据
# #     dict_obj = io.loadmat(name_list[i])
# #     temp = dict_obj['Matrix']
# #     np_D = np.vstack((np_D, temp))
# # #np_D = np.delete(np_D, 0, axis=0)
#
# np_D = np_D[:,3:]#去掉不需要的前四列
# #index = np.where(np_D[:,3]&lt; 0)[0]#将磨煤机电流低于10的值删去
# #np_D=np.delete(np_D,axis=0)#删去开始构造的全零向量
# #np_D = np.delete(np_D, index,axis=0)  # 删去开始构造的全零向量
# np_Dmax, np_Dmin = np_D.max(axis=0), np_D.min(axis=0)
if if_nor:
    np_D = (np_D - np_Dmin) / (np_Dmax - np_Dmin)
    print('已归一化的训练集，大小为：', np_D.shape)
    return np_D,np_Dmax,np_Dmin
else:
    print('未归一化的训练集，大小为：', np_D.shape)
    return np_D, np_Dmax, np_Dmin
</code></pre>
<p>if <strong>name</strong>==“<strong>main</strong>”:<br>
ae=autoencoder(404,4,batch_normalization=False)<br>
print(ae)</p>
<p>from AE_ModelConstruction import *</p>
<p>percent=0.7#测试比例<br>
patience=8#比对次数<br>
LR=0.005<br>
BATCH_SIZE=32<br>
layer=3<br>
input_num=404</p>
<p>ae=autoencoder(input_num,layer,batch_normalization=True)<br>
ae2=autoencoder(input_num,layer,batch_normalization=True)</p>
<p>name_list=[‘Matrix1’]<span class="hashtag">#训练数据文件名</span><br>
Trainset,Trainset_max,Trainset_min=Traindata(name_list)<span class="hashtag">#加载训练集</span><br>
subTrainset=Trainset[0:int(percent<em>Trainset.shape[0])]<span class="hashtag">#将训练数据分成两批</span>，训练模型<br>
vaildset=Trainset[int(percent</em>Trainset.shape[0]):-1]<br>
print(len(subTrainset))<br>
print(len(vaildset))</p>
<p>torch_sTs=torch.from_numpy(subTrainset).float()<br>
train_loader = Data.DataLoader(dataset=torch_sTs, batch_size=BATCH_SIZE, shuffle=True)<br>
torch_vs=torch.from_numpy(vaildset).float()</p>
<p><span class="hashtag">#第一轮训练</span>，将训练集分为子训练集和验证集，运行算法1并返回最佳训练轮数<br>
i=0<br>
j=0<br>
Loss_min=10<br>
i_best=i<br>
optimizer = torch.optim.Adam(ae.parameters(), lr=LR)<br>
loss_func = nn.MSELoss()<br>
while j&lt;patience:<br>
i=i+1<br>
ae.train()  # 训练状态<br>
for step, x in enumerate(train_loader):  # Dataloader<br>
b_x = x.view(-1, 1 * 404)  # batch x, shape (batch, 1<em>6)<br>
b_y = x.view(-1, 1 * 404)  # batch y, shape (batch, 1</em>6)<br>
encoded, decoded = ae(b_x)<br>
loss = loss_func(decoded, b_y)  # mean square error 求误差<br>
optimizer.zero_grad()  # clear gradients for this training step 在这轮的step里清除梯度<br>
loss.backward()  # backpropagation, compute gradients 误差反向传播，计算梯度d(loss)/dx<br>
optimizer.step()  # apply gradients 根据梯度更新参数<br>
ae.eval()  # 测试状态<br>
vs_enc, vs_dec=ae(torch_vs)<br>
vs_loss=loss_func(vs_dec,torch_vs)<br>
print(‘验证集误差：’,vs_loss.data.numpy())<br>
if vs_loss.data.numpy()&lt;Loss_min:<br>
j=0<br>
i_best=i<br>
Loss_min=vs_loss.data.numpy()<br>
else:<br>
j=j+1<br>
print(‘best epoch:’,i_best)<br>
<span class="hashtag">#第二轮训练</span><br>
train_loss=[]<br>
optimizer2 = torch.optim.Adam(ae2.parameters(), lr=LR)<br>
loss_func2 = nn.MSELoss()<br>
torch_Ts=torch.from_numpy(Trainset).float()<br>
train_loader2 = Data.DataLoader(dataset=torch_Ts, batch_size=BATCH_SIZE, shuffle=True)<br>
ae2.train()<span class="hashtag">#训练状态</span><br>
for epoch in range(i_best):<br>
for step, x in enumerate(train_loader2):<span class="hashtag">#Dataloader</span><br>
b_x = x.view(-1, 1<em>404)   # batch x, shape (batch, 1</em>14)<br>
b_y = x.view(-1, 1<em>404)   # batch y, shape (batch, 1</em>14)<br>
encoded, decoded = ae2(b_x)<br>
loss = loss_func2(decoded, b_y)      # mean square error<br>
optimizer2.zero_grad()               # clear gradients for this training step<br>
loss.backward()                     # backpropagation, compute gradients<br>
optimizer2.step()                    # apply gradients<br>
train_loss.append(loss.data.numpy())<br>
if step % 50 == 0:<br>
print(‘Epoch: ‘, epoch,’| train loss: %.6f’ % loss.data.numpy())<br>
torch.save(ae2, ‘ae_withBN_xyc_3.pkl’)<span class="hashtag">#保存神经网络模型</span><br>
np.save(‘Loss_withBN_xyc_3.npy’, train_loss)<span class="hashtag">#保存训练误差</span></p>

username: InnovArul
cooked: <aside class="quote no-group" data-username="yingchaoxue629" data-post="1" data-topic="149176">
<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/yingchaoxue629/48/48645_2.png" class="avatar"> yingchaoxue629:</div>
<blockquote>
<p>if self.do_bn:<br>
self.encoder.add_module(‘encoder_BN%i’ % (i + 1),nn.BatchNorm1d(3,momentum=0.5))</p>
</blockquote>
</aside>
<p>Probably in this line, <code>BatchNorm1d</code> gets 4 input channels?</p>
<p>P.S: Pl. enclose the code between three backticks (```) for readability.</p>