Skip to content

Instantly share code, notes, and snippets.

@Nucs
Created July 13, 2019 19:11
Show Gist options
  • Select an option

  • Save Nucs/c327d55ed33ca5d4d3f25202a0df9b41 to your computer and use it in GitHub Desktop.

Select an option

Save Nucs/c327d55ed33ca5d4d3f25202a0df9b41 to your computer and use it in GitHub Desktop.
import numpy as np
errors = 0
rights = 0
outputShapes = list()
def operator_(left, right, msg):
global errors, rights, outputShapes
print("")
print(msg + " --------------------------------")
print("---------------------------------------------------")
print("")
print("@Left: ------------")
print("shape: " + str(left.shape if isinstance(left, np.ndarray) else left) + " size: " + str(left.size if isinstance(left, np.ndarray) else left))
print(left)
print("")
print("@Right: -----------")
print("shape: " + str(right.shape if isinstance(right, np.ndarray) else right) + " size: " + str(right.size if isinstance(right, np.ndarray) else right))
print(right)
print("")
try:
ret = (left + right)
print("@Result: ----------")
print("shape: " + str(ret.shape if isinstance(ret, np.ndarray) else ret) + " size: " + str(ret.size if isinstance(ret, np.ndarray) else ret))
print(ret)
rights += 1
outputShapes.append(str(left.shape if isinstance(left, np.ndarray) else left) + " | " + str(right.shape if isinstance(right, np.ndarray) else right) + " -> " + str(ret.shape if isinstance(ret, np.ndarray) else ret))
except Exception as e:
print(e)
errors += 1
print("------------------")
# operator_((np.random.randint(0, 2, (5, 5)) == 1),
# (np.random.randint(0, 2, (5, 5)) == 1), "Random left and right")
# operator_(np.arange(25).reshape((5, 5)), np.arange(25).reshape((5, 5)), "test")
left: np.ndarray = np.arange(25).reshape((5, 5))
operator_(left, 1, "Mat(5,5) vs Scalar 1")
operator_(left, np.arange(5), "Mat(5,5) vs Vec(5,)")
operator_(left, np.arange(3), "Mat(5,5) vs Vec(3,) missmatch")
operator_(left, np.arange(1), "Mat(5,5) vs Vec(1,)")
operator_(left, left, "Mat(5,5) vs Mat(5,5)")
operator_(np.arange(5).reshape((1, 5)), left, "Mat(1,5) vs Mat(5,5)")
operator_(left, left.reshape(1, 5, 5), "Mat(5,5) vs Mat(1,5,5)")
operator_(left, np.arange(10).reshape((1, 2, 5)), "Mat(5,5) vs Mat(1,2,5)")
operator_(left, np.arange(5).reshape((1, 1, 1, 5)), "Mat(5,5) vs Mat(1,1,1,5)")
operator_(np.arange(5), np.arange(5), "Vec(5,) vs Vec(5,)")
# A (4d array): 2 x 1 x 3 x 1
# B (3d array): 2 x 1 x 2
# Result (4d array): 2 x 2 x 3 x 2
operator_(np.arange(6).reshape((2, 1, 3, 1)), np.arange(4).reshape(2, 1, 2), "ND(2, 1, 3, 1) vs ND(2, 1, 2)")
print("\n\n\n")
print(str(errors) + "/" + str(rights))
for m in outputShapes:
print(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment