Could i save weights in each layer to csv in pytorch?

Hello

I am a beginner in Deep Learning and doing research comparing keras backend tensorflow and pytorch. I just try writing model in pytorch and i had succeed print the weights. Is it possible to save those weights to csv file?

for reference this is my code

class MultiLayerPerceptron(nn.Module):
def init(self, input_size, hidden_size, num_classes):
super(MultiLayerPerceptron, self).init()
self.fc1 = nn.Linear(input_size, hidden_size)
self.Sigmoid = nn.Sigmoid()
self.fc2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
out = self.fc1(x)
out = self.Sigmoid(out)
out = self.fc2(out)
return out
model = MultiLayerPerceptron(input_size, hidden_size, num_classes)
weights = model.fc1.weight
print(weights)

import numpy as np
import pandas as pd

ā€¦
weights = weights.detach().numpy()
pd.DataFrame(weights).to_csv( ā€™ weights.csv ā€™ )

1 Like

thanks you it work well