When I’m studying the GNN,my training code can’t train the module and the parameter keep still
def create_node_emb(num_node=34, embedding_dim=16):
# TODO: Implement this function that will create the node embedding matrix.
# A torch.nn.Embedding layer will be returned. You do not need to change
# the values of num_node and embedding_dim. The weight matrix of returned
# layer should be initialized under uniform distribution.
emb = None
############# Your code here ############
emb = torch.nn.Embedding(num_embeddings=num_node,embedding_dim=embedding_dim)
shape = emb.weight.data.shape
emb.weight.data = torch.rand(shape)
#########################################
return emb
this is the correct code
def train(emb, loss_fn, sigmoid, train_label, train_edge):
# TODO: Train the embedding layer here. You can also change epochs and
# learning rate. In general, you need to implement:
# (1) Get the embeddings of the nodes in train_edge
# (2) Dot product the embeddings between each node pair
# (3) Feed the dot product result into sigmoid
# (4) Feed the sigmoid output into the loss_fn
# (5) Print both loss and accuracy of each epoch
# (6) Update the embeddings using the loss and optimizer
# (as a sanity check, the loss should decrease during training)
epochs = 500
learning_rate = 0.1
optimizer = SGD(emb.parameters(), lr=learning_rate, momentum=0.9)
for i in range(epochs):
emb.train()
############# Your code here ############
optimizer.zero_grad()
dot_pro = torch.sum(output[0]*output[1],dim=1)
pred = sigmoid(dot_pro)
loss = loss_fn(pred,train_label)
loss.backward()
optimizer.step()
acc = accuracy(pred,train_label)
if i%50 == 0:
print("accracy ={},loss={}".format(acc,loss))
params = list(emb.parameters())
for i, param in enumerate(params):
print("Parameter {} shape: {}".format(i, param.shape))
print(param)
yet if I change the train code like the following code,
def train(emb, loss_fn, sigmoid, train_label, train_edge):
# TODO: Train the embedding layer here. You can also change epochs and
# learning rate. In general, you need to implement:
# (1) Get the embeddings of the nodes in train_edge
# (2) Dot product the embeddings between each node pair
# (3) Feed the dot product result into sigmoid
# (4) Feed the sigmoid output into the loss_fn
# (5) Print both loss and accuracy of each epoch
# (6) Update the embeddings using the loss and optimizer
# (as a sanity check, the loss should decrease during training)
epochs = 500
learning_rate = 0.1
optimizer = SGD(emb.parameters(), lr=learning_rate, momentum=0.9)
for i in range(epochs):
emb.train()
############# Your code here ############
optimizer.zero_grad()
node_list_fir,node_list_sec = (train_edge[0]),(train_edge[1])
emb_list_fir,emb_list_sec = emb(node_list_fir),emb(node_list_sec)
emb_torch = torch.sum(emb_list_fir*emb_list_sec,dim=1)
emb_torch = sigmoid(emb_torch)
emb_res = torch.round(emb_torch)
loss = loss_fn(emb_res,train_label)
loss.backward()
optimizer.step()
acc = accuracy(emb_res,train_label)
if i%50 == 0:
print("accracy ={},loss={}".format(acc,loss))
params = list(emb.parameters())
for i, param in enumerate(params):
print("Parameter {} shape: {}".format(i, param.shape))
print(param)
the parameters don’t change in the log
what’s the reason about the phenomenon, I’m new in DL,why I separate the train_edge into two part and then input into the model the training can’t work well.I don’t know the theory behind it