how to solve this probolem ?i try it two days

D:\programdate\anaconda3\envs\pytorch\python.exe C:/Users/11067/Desktop/SAE_pytorch-master/AE_Train.py
已归一化的训练集,大小为: (310, 404)
217
92
Traceback (most recent call last):
File “C:/Users/11067/Desktop/SAE_pytorch-master/AE_Train.py”, line 37, in
encoded, decoded = ae(b_x)
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py”, line 727, in _call_impl
result = self.forward(*input, **kwargs)
File “C:\Users\11067\Desktop\SAE_pytorch-master\AE_ModelConstruction.py”, line 55, in forward
encoded=self.encoder(x)
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py”, line 727, in _call_impl
result = self.forward(*input, **kwargs)
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\container.py”, line 117, in forward
input = module(input)
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py”, line 727, in _call_impl
result = self.forward(*input, **kwargs)
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\batchnorm.py”, line 136, in forward
self.weight, self.bias, bn_training, exponential_average_factor, self.eps)
File “D:\programdate\anaconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py”, line 2058, in batch_norm
training, momentum, eps, torch.backends.cudnn.enabled
RuntimeError: running_mean should contain 4 elements not 3

import torch
from torch import nn
from scipy import io
import numpy as np
import torch.utils.data as Data
import pandas as pd
ACTIVATION = torch.nn.LeakyReLU()
import scipy.io as scio

class autoencoder(nn.Module):
def init(self,input_num,layer,batch_normalization=False):
super(autoencoder,self).init()
self.do_bn=batch_normalization
self.encoder=nn.Sequential()
for i in range(layer):
# encoder第一层
if i == 0:
self.encoder.add_module(‘encoder_linear%i’ % (i + 1), nn.Linear(input_num, 2 ** (layer + 2)))
if self.do_bn:
self.encoder.add_module(‘encoder_BN%i’ % (i + 1),
nn.BatchNorm1d(2 ** (layer + 2), momentum=0.5))
self.encoder.add_module(‘encoder_actFun%i’ % (i + 1), ACTIVATION)
# encoder最后一层
elif i == (layer - 1):
self.encoder.add_module(‘encoder_linear%i’ % (i + 1), nn.Linear(16, 4))
if self.do_bn:
self.encoder.add_module(‘encoder_BN%i’ % (i + 1),nn.BatchNorm1d(3,momentum=0.5))
# encoder其它层
else:
self.encoder.add_module(‘encoder_linear%i’%(i+1),nn.Linear(2 ** (layer - i + 3),2 **(layer-i+2)))
if self.do_bn:
self.encoder.add_module(‘encoder_BN%i’ % (i + 1),
nn.BatchNorm1d(2 ** (layer - i + 2), momentum=0.5))
self.encoder.add_module(‘encoder_actFun%i’ % (i + 1), ACTIVATION)
self.decoder = nn.Sequential()
for i in range(layer):
# decoder第一层
if i == 0:
self.decoder.add_module(‘decoder_linear%i’ % (i + 1), nn.Linear(4, 16))
if self.do_bn:
self.decoder.add_module(‘decoder_BN%i’ % (i + 1), nn.BatchNorm1d(16, momentum=0.5))
self.decoder.add_module(‘decoder_actFun%i’ % (i + 1), ACTIVATION)
#decoder最后一层
elif i == (layer - 1):
self.decoder.add_module(‘decoder_linear%i’ % (i + 1), nn.Linear(2 ** (layer+ 2), input_num))
self.decoder.add_module(‘decoder_actFun%i’ % (i + 1), nn.Sigmoid())
# 其它层
else:
self.decoder.add_module(‘encoder_linear%i’ % (i + 1), nn.Linear(2 ** (3+i),2 ** (4+i)))
if self.do_bn:
self.decoder.add_module(‘encoder_BN%i’ % (i + 1),nn.BatchNorm1d(2 ** (4+i), momentum=0.5))
self.decoder.add_module(‘encoder_actFun%i’ % (i + 1), ACTIVATION)
def forward(self, x):
encoded=self.encoder(x)
decoded=self.decoder(encoded)
return encoded,decoded
#导入训练集并归一化,训练集可以为包含多个文件的列表
def Traindata(name_list,if_nor=True):
filepath = ‘C:/Users/11067/Desktop/SAE_pytorch-master/Matrix1.mat’
dict_data = scio.loadmat(filepath)
np_D = dict_data[‘Matrix’]
np_D = np_D[:, 3:] # 去掉不需要的前四列
np_Dmax, np_Dmin = np_D.max(axis=0), np_D.min(axis=0)

# #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]< 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

if name==“main”:
ae=autoencoder(404,4,batch_normalization=False)
print(ae)

from AE_ModelConstruction import *

percent=0.7#测试比例
patience=8#比对次数
LR=0.005
BATCH_SIZE=32
layer=3
input_num=404

ae=autoencoder(input_num,layer,batch_normalization=True)
ae2=autoencoder(input_num,layer,batch_normalization=True)

name_list=[‘Matrix1’]#训练数据文件名
Trainset,Trainset_max,Trainset_min=Traindata(name_list)#加载训练集
subTrainset=Trainset[0:int(percentTrainset.shape[0])]#将训练数据分成两批,训练模型
vaildset=Trainset[int(percent
Trainset.shape[0]):-1]
print(len(subTrainset))
print(len(vaildset))

torch_sTs=torch.from_numpy(subTrainset).float()
train_loader = Data.DataLoader(dataset=torch_sTs, batch_size=BATCH_SIZE, shuffle=True)
torch_vs=torch.from_numpy(vaildset).float()

#第一轮训练,将训练集分为子训练集和验证集,运行算法1并返回最佳训练轮数
i=0
j=0
Loss_min=10
i_best=i
optimizer = torch.optim.Adam(ae.parameters(), lr=LR)
loss_func = nn.MSELoss()
while j<patience:
i=i+1
ae.train() # 训练状态
for step, x in enumerate(train_loader): # Dataloader
b_x = x.view(-1, 1 * 404) # batch x, shape (batch, 16)
b_y = x.view(-1, 1 * 404) # batch y, shape (batch, 1
6)
encoded, decoded = ae(b_x)
loss = loss_func(decoded, b_y) # mean square error 求误差
optimizer.zero_grad() # clear gradients for this training step 在这轮的step里清除梯度
loss.backward() # backpropagation, compute gradients 误差反向传播,计算梯度d(loss)/dx
optimizer.step() # apply gradients 根据梯度更新参数
ae.eval() # 测试状态
vs_enc, vs_dec=ae(torch_vs)
vs_loss=loss_func(vs_dec,torch_vs)
print(‘验证集误差:’,vs_loss.data.numpy())
if vs_loss.data.numpy()<Loss_min:
j=0
i_best=i
Loss_min=vs_loss.data.numpy()
else:
j=j+1
print(‘best epoch:’,i_best)
#第二轮训练
train_loss=[]
optimizer2 = torch.optim.Adam(ae2.parameters(), lr=LR)
loss_func2 = nn.MSELoss()
torch_Ts=torch.from_numpy(Trainset).float()
train_loader2 = Data.DataLoader(dataset=torch_Ts, batch_size=BATCH_SIZE, shuffle=True)
ae2.train()#训练状态
for epoch in range(i_best):
for step, x in enumerate(train_loader2):#Dataloader
b_x = x.view(-1, 1404) # batch x, shape (batch, 114)
b_y = x.view(-1, 1404) # batch y, shape (batch, 114)
encoded, decoded = ae2(b_x)
loss = loss_func2(decoded, b_y) # mean square error
optimizer2.zero_grad() # clear gradients for this training step
loss.backward() # backpropagation, compute gradients
optimizer2.step() # apply gradients
train_loss.append(loss.data.numpy())
if step % 50 == 0:
print(‘Epoch: ‘, epoch,’| train loss: %.6f’ % loss.data.numpy())
torch.save(ae2, ‘ae_withBN_xyc_3.pkl’)#保存神经网络模型
np.save(‘Loss_withBN_xyc_3.npy’, train_loss)#保存训练误差

Probably in this line, BatchNorm1d gets 4 input channels?

P.S: Pl. enclose the code between three backticks (```) for readability.