3

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?

1

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

My pleasure! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.