NeuralNetworkVerification / Marabou

Other
239 stars 86 forks source link

TypeError: setLowerBound(): incompatible function arguments #816

Open chenyi10 opened 5 days ago

chenyi10 commented 5 days ago

Error info :Traceback (most recent call last): File "/home/gpu/chenyi/mymaraboupy/uav/onnxexample1.py", line 117, in evaluateFile("data/zlpolicy_model/zlpolicy_model.onnx") File "/home/gpu/chenyi/mymaraboupy/uav/onnxexample1.py", line 112, in evaluateFile exitCode, vals, stats = network.solve(filename="", options=OPT) File "/etc/anaconda3/envs/cy-py3.8/lib/python3.8/site-packages/maraboupy/MarabouNetwork.py", line 70, in solve ipq = self.getInputQuery() File "/etc/anaconda3/envs/cy-py3.8/lib/python3.8/site-packages/maraboupy/parsers/InputQueryBuilder.py", line 344, in getInputQuery ipq.setLowerBound(l, self.lowerBounds[l]) TypeError: setLowerBound(): incompatible function arguments. The following argument types are supported:

  1. (self: maraboupy.MarabouCore.InputQuery, arg0: int, arg1: float) -> None

Invoked with: <maraboupy.MarabouCore.InputQuery object at 0x7f0df7b99670>, -1.0, -1

Code:# Global settings OPT = Marabou.createOptions(verbosity = 0) # Turn off printing TOL = 1e-4 # Set tolerance for checking Marabou evaluations NETWORK_FOLDER = "./" # Folder for test networks np.random.seed(123) # Seed random numbers for repeatability NUM_RAND = 10 # Default number of random test points per example model = tf.saved_model.load("tf2_model/my_model3")

def cosine_similarity(action, distance):

计算点积

dot_product = np.dot(action, distance)
# 计算范数(长度)
action_norm = np.linalg.norm(action)
distance_vector_norm = np.linalg.norm(distance)
# 计算余弦相似度
cosine_sim = dot_product / (action_norm * distance_vector_norm)

return round(cosine_sim, 8)

def evaluateFile(filename, inputNames = None, outputNames = None, testInputs = None, numPoints = NUM_RAND): """ Load network and evaluate testInputs with and without Marabou Args: filename (str): name of network file without path inputNames (list of str): name of input layers outputNames (list of str): name of output layers testInputs (list of arrays): test points to evaluate. Points created if none provided numPoints (int): Number of test points to create if testInputs is none """

# Load network relative to this file's location
filename = os.path.join(os.path.dirname(__file__), NETWORK_FOLDER, filename)
network = Marabou.read_onnx(filename, inputNames = inputNames, outputNames = outputNames)

# Create test points if none provided. This creates a list of test points.
# Each test point is itself a list, representing the values for each input array.
if not testInputs:
    testInputs = [[np.random.random(inVars.shape) for inVars in network.inputVars] for _ in range(numPoints)]

# Evaluate test points using both Marabou and ONNX, and assert that the error is less than TOL
for testInput in testInputs:
    err = network.findError(testInput, options=OPT, filename="")
    for i in range(len(err)):
        assert max(err[i].flatten()) < TOL

inputVars = network.inputVars[0][0]
outputVars = network.outputVars[0][0]

for inputVar in inputVars:
    for inputV in inputVar:
        network.setLowerBound(inputV, -100)
        network.setUpperBound(inputV, 100)

for outputVar in outputVars:
    for outputV in outputVar:
        network.setLowerBound(outputV, -100)
        network.setUpperBound(outputV, 100)

# print(inputVars)
# print(outputVars)

# 提取x, y, z坐标
positions = inputVars[:, :3]

# 计算每个智能体与其他所有智能体之间的欧氏距离
distances = np.zeros((16, 16))

for i in range(16):
    for j in range(16):
        if i != j:
            distances[i, j] = np.linalg.norm(positions[i] - positions[j])

# 找到每个智能体的最近邻智能体
nearest_neighbors = np.argmin(np.where(distances == 0, np.inf, distances), axis=1)

# 计算每个智能体到其最近邻智能体的距离向量
distance_vectors = np.zeros((16, 3))

for i in range(16):
    distance_vectors[i] = positions[nearest_neighbors[i]] - positions[i]

for i in range(len(distance_vectors)):
    value =cosine_similarity(distance_vectors[i],outputVars[i])

    network.setLowerBound(value,-1)
    network.setUpperBound(value,0.7)

exitCode, vals, stats = network.solve(filename="", options=OPT)

evaluateFile("data/zlpolicy_model/zlpolicy_model.onnx")

Is there a method to solve the problem with cosine similarity constraints?