Example #1
0
def test_symmetric_tools_symmetric():
    rnd = np.random.RandomState(0)
    # generate random symmetric matrix
    for size in [4, 6, 11]:
        x = rnd.normal(size=(size, size))
        x = x + x.T

        compressed = compress_sym(x, make_symmetric=False)
        assert_equal(compressed.shape, (size * (size + 1) / 2, ))

        uncompressed = expand_sym(compressed)
        assert_array_equal(x, uncompressed)
Example #2
0
def test_symmetric_tools_symmetric():
    rnd = np.random.RandomState(0)
    # generate random symmetric matrix
    for size in [4, 6, 11]:
        x = rnd.normal(size=(size, size))
        x = x + x.T

        compressed = compress_sym(x, make_symmetric=False)
        assert_equal(compressed.shape, (size * (size + 1) / 2, ))

        uncompressed = expand_sym(compressed)
        assert_array_equal(x, uncompressed)
Example #3
0
def test_symmetric_tools_upper():
    rnd = np.random.RandomState(0)
    # generate random matrix with only upper triangle.
    # expected result is full symmetric matrix
    for size in [4, 6, 11]:
        x = rnd.normal(size=(size, size))
        x = x + x.T
        x_ = x.copy()
        x[np.tri(size, k=-1, dtype=np.bool)] = 0

        compressed = compress_sym(x, make_symmetric=True)
        assert_equal(compressed.shape, (size * (size + 1) / 2, ))

        uncompressed = expand_sym(compressed)
        assert_array_equal(x_, uncompressed)
Example #4
0
def test_symmetric_tools_upper():
    rnd = np.random.RandomState(0)
    # generate random matrix with only upper triangle.
    # expected result is full symmetric matrix
    for size in [4, 6, 11]:
        x = rnd.normal(size=(size, size))
        x = x + x.T
        x_ = x.copy()
        x[np.tri(size, k=-1, dtype=np.bool)] = 0

        compressed = compress_sym(x, make_symmetric=True)
        assert_equal(compressed.shape, (size * (size + 1) / 2, ))

        uncompressed = expand_sym(compressed)
        assert_array_equal(x_, uncompressed)
Example #5
0
y_pred = y_pred.reshape(x.shape[:2])
fig, plots = plt.subplots(1, 4, figsize=(12, 4))
plots[0].matshow(y)
plots[0].set_title("ground truth")
plots[1].matshow(np.argmax(x, axis=-1))
plots[1].set_title("input")
plots[2].matshow(y_pred)
plots[2].set_title("prediction")
loss_augmented = clf.model.loss_augmented_inference(x, y, clf.w)
loss_augmented = loss_augmented.reshape(y.shape)
plots[3].matshow(loss_augmented)
plots[3].set_title("loss augmented")
for p in plots:
    p.set_xticks(())
    p.set_yticks(())

# visualize weights
w_un = clf.w[:3 * 3].reshape(3, 3)
# decode the symmetric pairwise potential
w_pw = expand_sym(clf.w[3 * 3:])

fig, plots = plt.subplots(1, 2, figsize=(8, 4))
plots[0].matshow(w_un, cmap='gray', vmin=-5, vmax=5)
plots[0].set_title("Unary weights")
plots[1].matshow(w_pw, cmap='gray', vmin=-5, vmax=5)
plots[1].set_title("Pairwise weights")
for p in plots:
    p.set_xticks(())
    p.set_yticks(())
plt.show()
x, y, y_pred = X[0], Y[0], Y_pred[0]
y_pred = y_pred.reshape(x.shape[:2])
fig, plots = plt.subplots(1, 4, figsize=(12, 4))
plots[0].matshow(y)
plots[0].set_title("ground truth")
plots[1].matshow(np.argmax(x, axis=-1))
plots[1].set_title("input")
plots[2].matshow(y_pred)
plots[2].set_title("prediction")
loss_augmented = clf.model.loss_augmented_inference(x, y, clf.w)
loss_augmented = loss_augmented.reshape(y.shape)
plots[3].matshow(loss_augmented)
plots[3].set_title("loss augmented")
for p in plots:
    p.set_xticks(())
    p.set_yticks(())

# visualize weights
w_un = clf.w[:3 * 3].reshape(3, 3)
# decode the symmetric pairwise potential
w_pw = expand_sym(clf.w[3 * 3:])

fig, plots = plt.subplots(1, 2, figsize=(8, 4))
plots[0].matshow(w_un, cmap='gray', vmin=-5, vmax=5)
plots[0].set_title("Unary weights")
plots[1].matshow(w_pw, cmap='gray', vmin=-5, vmax=5)
plots[1].set_title("Pairwise weights")
for p in plots:
    p.set_xticks(())
    p.set_yticks(())
plt.show()