Reputation: 139
I have a 3-dimensional tensor that I create outside of my python code and am able to encode in any human-readable format. I need to load the values of this tensor as a frozen layer to my pytorch
NN. I've tried to encode the tensor as a text file in the form [[[a,b],[c,d]], [[e,f], [g,h]], [[k,l],[m,n]]]
which seemed to be the most logical way for that. Then I tried to read its value via
tensor = torch.from_numpy(np.loadtxt("./arrays/tensor.txt"))
but got the exception in npyio.py
ValueError: could not convert string to float: '[[[-2.888356,'
Apparently, that's not how it works and the values are to be written as plain numbers separated by spaces and \n
, but then I don't see how to easily read the data of dimension >= 2 with numpy
.
What could be other simple methods to write down and read the tensor value into a pytorch
tensor?
Upvotes: 3
Views: 5203
Reputation: 2493
Did you try using Python's built-in eval
? In case you saved your tensor as a list in text file you may try something as follows:
with open("./arrays/tensor.txt","r") as f:
loaded_list = eval(f.read())
loaded_tensor = torch.tensor(loaded_list)
eval
will take care of converting your string to a list and then just cast the result to a Tensor
by using torch.tensor()
. The loaded tensor will then be in loaded_tensor
as required.
Upvotes: 3