从for循环转向矩阵计算:大规模条件验证的向量化实现咨询
I've written code to validate if a list of objects meets certain requirements. First, here's my object class:
class S: def __init__(self, f, t, tf, timeline): self.f = f self.t = t self.tf = tf self.timeline = timeline
I have a validation function that takes a list of N S objects and returns True/False based on whether they meet a specific condition:
def c1(input1): # input1 is a list like [S_1, ..., S_N] if condition_c1_valid: return True else: return False
Right now, I'm generating all possible combinations of these objects and checking them one by one:
import itertools # Generate all possible individual S objects possible_objects = [S(f, t, tf, timeline) for f in [...] for t in [...] # ... other parameter loops ... ] # Generate combinations (here, 5 elements per input) inputs_to_check = list(itertools.combinations_with_replacement(possible_objects, 5)) results = [] for inp in inputs_to_check: if c1(inp): results.append(inp)
The problem is that the scale of inputs to check is enormous—even with generator optimizations, it's handling large sets with 6/7 nested loops, leading to huge computational overhead.
I'm wondering if I can vectorize this entire validation process using a matrix/array approach to run it on a GPU with TensorFlow. My rough pseudocode idea looks like this:
# Hypothetical structure Data[input, c1, ..., cN] return all(inputs where (c1 AND ... AND cN) is True)
Is this feasible? Could you provide a concrete example of how to implement this? My goal is to port this computation to GPU to speed things up significantly.
内容的提问来源于stack exchange,提问作者Mathieu

