Hello all, I am new in this method, when I try to run it, I got an size mismatch error message.
Could someone help me for these two codes below please
main:
train = pd.read_csv("dataset/Train+.csv")
trainx, trainy = np.array(train[train.columns[train.columns != "class"]]), np.array(pd.get_dummies(train["class"]))
batch_size = 512
max_epoch = 100
train_N = len(train)
gpu = False
device = "cuda" if gpu else "cpu"
model = CVAE()
if gpu:
model = model.cuda()
opt = optim.Adadelta(model.parameters(),lr = 1e-3)
def Loss_function(x_hat,x, mu,logsimga):
reconstraction_loss = F.binary_cross_entropy(x_hat,x,size_average = False)
KL_div = -0.5 * th.sum(1+logsimga-mu.pow(2) - logsimga.exp())
return reconstraction_loss+KL_div
def create_batch(x,y):
a = list(range(len(x)))
np.random.shuffle(a)
x = x[a]
y = y[a]
batch_x = [x[batch_size * i : (i+1)*batch_size,:].tolist() for i in range(len(x)//batch_size)]
batch_y = [y[batch_size * i : (i+1)*batch_size].tolist() for i in range(len(x)//batch_size)]
return batch_x, batch_y
def train():
model.train()
tr_loss = 0
batch_x,batch_y = create_batch(trainx,trainy)
for x,y in zip(batch_x,batch_y):
opt.zero_grad()
if gpu:
x,y = V(th.Tensor(x).cuda()),V(th.Tensor(y).cuda())
else:
x,y = V(th.Tensor(x)),V(th.Tensor(y))
x_hat,mu,logsigma = model(x,y)
loss = Loss_function(x_hat,x,mu,logsigma)
loss.backward()
tr_loss += loss.item()
opt.step()
return tr_loss/train_N
tr_loss = []
for epoch in range(max_epoch):
trl = train()
tr_loss.append(trl)
if epoch % 1 == 0:
print(epoch,trl)
save = th.save(model.state_dict(), f"save_model/vae_adadelta_{max_epoch}.pth")
plt.plot(tr_loss)
plt.show()
model:
class CVAE(nn.Module):
def __init__(self):
super().__init__()
self.fc1=nn.Linear(116,500)
self.hidden = nn.Linear(512,505)
self.mu = nn.Linear(505,25)
self.sigma = nn.Linear(505,25)
self.fc2 = nn.Linear(25,495)
self.fc3 = nn.Linear(500,116)
self.sigmoid = nn.Sigmoid()
self.relu = nn.ReLU()
def encoder(self,x, y):
h = self.relu(self.fc1(x))
h1 = th.cat((h, y), dim = 1)
h2 = self.hidden(self.relu(self.hidden(h1)))
return self.mu(h2),self.sigma(h2)
def revize_parameter(self,mu,logsigma):
sigma = th.exp(0.5*logsigma)
eps = V(th.randn(sigma.size()))
return sigma.mul(eps) + mu
def decoder(self,z,y):
h = self.relu(self.fc2(z))
h = th.cat((h,y),dim = 1)
h = self.fc3(self.relu(self.hidden(h)))
return self.sigmoid(h)
def forward(self,x,y):
mu,sigma = self.encoder(x, y)
z = self.revize_parameter(mu,sigma)
output = self.decoder(z,y)
return output,mu,sigma
error:
builtins.RuntimeError: size mismatch, m1: [512 x 505], m2: [512 x 505]