memory

How to get a flattened view of PyTorch model parameters?

How to get a flattened view of PyTorch model parameters? Question: I want to have a flattened view of the parameters belonging to my Pytorch model. Note it should be a view and not a copy of the parameters. In other words, when I modify the parameters in the view it should also modify the …

Total answers: 1

Why does Python's functools.partial definition use function attributes?

Why does Python's functools.partial definition use function attributes? Question: In the functools.partial example definition, function attributes are used in the returned partial function object: def partial(func, /, *args, **keywords): def newfunc(*fargs, **fkeywords): newkeywords = {**keywords, **fkeywords} return func(*args, *fargs, **newkeywords) newfunc.func = func newfunc.args = args newfunc.keywords = keywords return newfunc What would be the …

Total answers: 3

Limiting Python file space in memory

Limiting Python file space in memory Question: When you open a file in python (e.g., open(filename, ‘r’) does it load the entire file into memory? More importantly, is there a way to partially load a file into memory to save memory space (for larger systems) or am I overthinking this? Particularly, I’m trying to optimize …

Total answers: 1

How to store data in seperate memory mdoules

How to store data in seperate memory mdoules Question: I’m working on an image processing pipeline in Python and I’m using Cython for the main computation so that it can run really fast. From early benchmarks, I found a memory bottleneck where the code would not scale at all using multiple threads. I revised the …

Total answers: 1

How do I use a file like a memory buffer in Python?

How do I use a file like a memory buffer in Python? Question: I don’t know the correct terminology, maybe it’s called page file, but I’m not sure. I need a way to use an on-disk file as a buffer, like bytearray. It should be able to do things like a = buffer[100:200] and buffer[33] …

Total answers: 1

How are small sets stored in memory?

How are small sets stored in memory? Question: If we look at the resize behavior for sets under 50k elements: >>> import sys >>> s = set() >>> seen = {} >>> for i in range(50_000): … size = sys.getsizeof(s) … if size not in seen: … seen[size] = len(s) … print(f"{size=} {len(s)=}") … s.add(i) …

Total answers: 1

Lists vs tuple in Python

Lists vs tuple in Python Question: I create my first python student project. I need save data about orders in online shop in books python. But I can’t understandwhat will be better: save informations in list with tuple (last mean in data must be changeable) or I can make just list. I see, that list …

Total answers: 1

how to create a list of new objects in python

how to create a list of new objects in python Question: I am trying to create a list of new objects in python (as in separate memory locations, not references) class example: _name = "" _words = [] def __init__(self,name): _name = name def add_words(self,new_words): for i in new_words: self._words.append(i) examples = [example("example_1"),example("example_2"),example("example_3")] examples[0].add_words(["test1","test2"]) print(examples[2]._words) …

Total answers: 2

Optimal way of storing stream content on disk using Python

Optimal way of storing stream content on disk using Python Question: I’d like to stream data directly to disk. One way of doing that is simply to read data and write to file, but I also want to minimalize RAM usage. with open("dummy.source", "br") as out, open("dummy.copy", "bw") as in_: in_.write(out.read()) # this causes reading …

Total answers: 1