马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请大神赐教,我现在有一个n维样本X,它的方差为4点几,我用一个简单的网络,不进行任何训练(即不计算任何损失函数,不做反馈),相当于对X简单做一个函数变换F(X),得到与X等维的Y。
但是不管我这个网络怎么设计,得到的Y的方差总是接近于0. 有什么办法让Y的方差接近X的方差?
以下是我的相关代码:def ConvertTensor(vec,grad_flag=True):
return torch.tensor(vec,dtype=torch.float32,requires_grad=grad_flag)
def Normalize(flat_geom):
Nsample, ThreeN = flat_geom.shape
mean_list = []
std_list = []
norm_geom = copy.deepcopy(flat_geom)
for i in range(Nsample):
each_geom = norm_geom[i]
mean = np.mean(each_geom)
std = np.std(each_geom)
norm_geom[i] = (each_geom-mean)/std
mean_list.append(mean)
std_list.append(std)
return mean_list, std_list, norm_geom
class Generator(nn.Module):
""" Architecture of the Generator, uses res-blocks """
def __init__(self):
super().__init__()
self.activation_func = nn.ReLU()
self.lr = 2e-3
self.inp_dim = 186
self.latent_dim = 100
self.Nlayer = 3
self.model = self.G_model()
def G_model(self):
module_list = []
inp_dim = self.inp_dim
latent_dim = self.latent_dim
activation_func = self.activation_func
Nlayer = self.Nlayer
module_list.append(nn.Linear(inp_dim,latent_dim))
module_list.append(activation_func)
for i in range(Nlayer):
module_list.append(nn.Linear(latent_dim,latent_dim))
module_list.append(activation_func)
module_list.append(nn.Linear (latent_dim, inp_dim) )
module_list.append(activation_func)
model = nn.Sequential(*module_list)
return model
def forward(self,inp_dat):
input_tensor = ConvertTensor(inp_dat,True) #include grad
out=self.model(input_tensor)
return out
def train(self,inp_data):
gen_tensor = self.forward(inp_data)
return gen_tensor
if __name__=="__main__":
filename="LessConfig.dat"
flag=2
inp_data = np.loadtxt(filename)
norm_flag=1
if norm_flag==1:
inp_tensor = ConvertTensor(inp_data,False)
if norm_flag==2:
mean_list, std_list, norm_geom = Normalize(inp_data)
inp_tensor = ConvertTensor(norm_geom,False)
generator = Generator()
for i in range(50):
mean = torch.mean(inp_tensor[0]).detach().numpy()
std = torch.std(inp_tensor[0]).detach().numpy()
print(" input mean and std:" , np.round(mean,4), np.round(std,4))
gen_out = generator.train(inp_tensor[0])
mean = torch.mean(gen_out).detach().numpy()
std = torch.std(gen_out).detach().numpy()
print("output mean and std:" , np.round(mean,4), np.round(std,4))
|