コード例 #1
0
def test_sparse_components_nonzero_ground_state():
    expected_dense = np.array([[[1.432, 2.654], [0.5, 7.1], [0.5, 7.1]],
                               [[0.5, 7.1], [5.327, 6.777], [6.112, 7.123]],
                               [[0.5, 7.1], [0.5, 7.1], [0.5, 7.1]],
                               [[0.5, 7.1], [0.5, 7.1],
                                [16.853, 17.352]]]).astype(np.float32)

    H_in = np.array([[0, 0], [1, 1], [1, 2], [3, 2]])
    M_in = np.array([[1.432, 2.654], [5.327, 6.777], [6.112, 7.123],
                     [16.853, 17.352]])
    dense_shape = (4, 3, 2)
    ground_state = np.array([0.5, 7.1])

    sparse = SparseDataValue(H=H_in,
                             M=M_in,
                             dense_shape=dense_shape,
                             ground_state=ground_state)

    np.testing.assert_equal(sparse.H, H_in)
    np.testing.assert_almost_equal(sparse.M, M_in)
    np.testing.assert_equal(sparse.ground_state, ground_state)
    assert sparse.dense_shape == dense_shape

    np.testing.assert_almost_equal(expected_dense,
                                   sparse.to_dense(),
                                   decimal=5)
コード例 #2
0
def test_simple_sparse_to_dense():
    dense = np.array([[[1.432, 2.654], [0, 0], [0, 0]],
                      [[0, 0], [5.327, 6.777], [6.112, 7.123]],
                      [[0, 0], [0, 0], [0, 0]],
                      [[0, 0], [0, 0], [16.853, 17.352]]]).astype(np.float32)
    sparse = SparseDataValue(dense)
    np.testing.assert_almost_equal(dense, sparse.to_dense())
コード例 #3
0
def test_h_q_n_in_2_nonzero_ground_state():
    x_dense = np.array([[[1.7, 0.7], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]],
                        [[0.0, 0.0], [0.0, 4.1], [0.0, 0.0], [0.0, 0.0]],
                        [[0.0, 0.0], [0.0, 0.0], [7.9, 0.9], [4.8, 0.8]],
                        [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]],
                       dtype=np.float32)

    np.random.seed(102)

    x_s = SparseDataValue(x_dense,
                          ground_state=np.array([0.5,
                                                 1.78]).astype(np.float32))

    n_in = 2
    f = 2

    H_out, Q = build_h_out_and_Q(x_s.H, x_s.M, x_s.dense_shape, f, n_in,
                                 x_s.ground_state)

    expected_H = np.array([(0, 0), (0, 1), (1, 0), (1, 1), (1, 2), (2, 1),
                           (2, 2)])
    expected_Q = np.array([
        (1.7, 0.7, 0.5, 1.78, 0.5, 1.78, 0.0, 4.1),
        (0.5, 1.78, 0.5, 1.78, 0.0, 4.1, 0.5, 1.78),
        (0.5, 1.78, 0.0, 4.1, 0.5, 1.78, 0.5, 1.78),
        (0.0, 4.1, 0.5, 1.78, 0.5, 1.78, 7.9, 0.9),
        (0.5, 1.78, 0.5, 1.78, 7.9, 0.9, 4.8, 0.8),
        (0.5, 1.78, 7.9, 0.9, 0.5, 1.78, 0.5, 1.78),
        (7.9, 0.9, 4.8, 0.8, 0.5, 1.78, 0.5, 1.78),
    ])

    np.testing.assert_array_equal(H_out, expected_H)
    np.testing.assert_array_almost_equal(Q, expected_Q, decimal=6)
コード例 #4
0
def run_sparse_and_dense(sparse: SparseDataValue, n_out, f, np_seed: int = None, b: np.ndarray = None):
    dense = sparse.to_dense()

    if np_seed:
        np.random.seed(np_seed)

    n_in = sparse.dense_shape[2]

    sparse_tensor = SparseDataTensor(
        tf.constant(sparse.H),
        tf.constant(sparse.M),
        sparse.dense_shape,
        tf.constant(sparse.ground_state))

    W_np = np.random.rand(f * f * n_in, n_out)
    W = tf.constant(W_np, dtype=tf.float32)
    if b is None:
        b = tf.constant(np.random.rand(n_out), dtype=tf.float32)

    output_sparse_tensor = sparse_conv_2d(sparse_tensor, W, f, n_out, b)

    # calculate output of sparse_conv_2d and convert to dense representation
    with tf.Session() as sess:
        scnn_output_dense = \
            SparseDataValue(
                H=sess.run(output_sparse_tensor.H), M=sess.run(output_sparse_tensor.M),
                dense_shape=output_sparse_tensor.dense_shape, ground_state=sess.run(output_sparse_tensor.ground_state)
            ).to_dense()

    # now perform the same convolution with tf.nn.conv2d

    X_4d = tf.reshape(tf.constant(dense, dtype=tf.float32),
                      (1, dense.shape[0], dense.shape[1], dense.shape[2]))
    W_4d = tf.reshape(W, (f, f, n_in, n_out))

    conv_1_tf = tf.nn.conv2d(X_4d, W_4d, strides=[1, 1, 1, 1], padding="VALID")
    dense_cnn_output_tf = tf.nn.bias_add(conv_1_tf, b)

    with tf.Session() as sess:
        dense_cnn_output = sess.run(dense_cnn_output_tf)

    return scnn_output_dense.reshape(dense_cnn_output.shape), dense_cnn_output
コード例 #5
0
def test_simple_dense_int_array():
    dense = np.array([[[1, 2], [0, 0], [0, 0]], [[0, 0], [5, 6], [6, 7]],
                      [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [16, 17]]])
    sparse = SparseDataValue(dense)
    assert sparse.dense_shape == (4, 3, 2)
    np.testing.assert_array_equal(sparse.ground_state,
                                  np.zeros(2, dtype=np.int64))
    np.testing.assert_array_equal(sparse.H,
                                  np.array([[0, 0], [1, 1], [1, 2], [3, 2]]))
    np.testing.assert_array_equal(sparse.M,
                                  np.array([[1, 2], [5, 6], [6, 7], [16, 17]]))
コード例 #6
0
def test_sparse_conv_2d_128x128x3():

    np.random.seed(123)
    x_dense = np.random.rand(128, 128, 3).astype(np.float32)
    # make a sparse region
    x_dense[range(51, 74), range(85, 108), :] = 0.0

    x_sparse = SparseDataValue(x_dense, ground_state=np.array([4.23, 2.5355, 13.322]).astype(np.float32))

    scnn_output, tf_cnn_output = run_sparse_and_dense(x_sparse, 8, 5, np_seed=2)

    np.testing.assert_almost_equal(scnn_output, tf_cnn_output, decimal=7)
コード例 #7
0
def test_sparse_conv_2d_very_large_very_sparse():

    x_dense = np.zeros((1280, 1280, 3)).astype(np.float32)
    # set a few values
    x_dense[4, 1] = [13.52, 3.1, 0.44]
    x_dense[9, 2] = [0.0, 158.97, 12.58]
    x_dense[3, 5] = [45.7, 93.1, 456.14]

    x_sparse = SparseDataValue(x_dense, ground_state=np.array([0.5, 0.5, 0.5]).astype(np.float32))

    scnn_output, tf_cnn_output = run_sparse_and_dense(x_sparse, 1, 7, np_seed=123)

    np.testing.assert_almost_equal(scnn_output, tf_cnn_output, decimal=4)
コード例 #8
0
def test_sparse_conv_2d_n_in_2_nonzero_ground_state():
    x_dense = np.array([
        [[1.7, 0.7], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]],
        [[0.0, 0.0], [0.0, 4.1], [0.0, 0.0], [0.0, 0.0]],
        [[0.0, 0.0], [0.0, 0.0], [7.9, 0.9], [4.8, 0.8]],
        [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]
    ], dtype=np.float32)

    x_sparse = SparseDataValue(x_dense, ground_state=np.array([0.5, 1.78]).astype(np.float32))

    scnn_output, tf_cnn_output = run_sparse_and_dense(x_sparse, 13, 2, np_seed=1)

    np.testing.assert_almost_equal(scnn_output, tf_cnn_output, decimal=6)
コード例 #9
0
def test_simple_dense_float32_array():
    dense = np.array([[[1.432, 2.654], [0, 0], [0, 0]],
                      [[0, 0], [5.327, 6.777], [6.112, 7.123]],
                      [[0, 0], [0, 0], [0, 0]],
                      [[0, 0], [0, 0], [16.853, 17.352]]]).astype(np.float32)
    sparse = SparseDataValue(dense)
    assert sparse.dense_shape == (4, 3, 2)
    np.testing.assert_array_equal(sparse.ground_state,
                                  np.zeros(2, dtype=np.int64))
    np.testing.assert_array_equal(sparse.H,
                                  np.array([[0, 0], [1, 1], [1, 2], [3, 2]]))
    np.testing.assert_array_almost_equal(
        sparse.M,
        np.array([[1.432, 2.654], [5.327, 6.777], [6.112, 7.123],
                  [16.853, 17.352]]))
コード例 #10
0
def test_large_dense_to_dense():
    np.random.seed(1)
    dense = np.random.rand(128, 128, 3).astype(np.float32)
    sparse = SparseDataValue(dense)
    np.testing.assert_almost_equal(dense, sparse.to_dense())
コード例 #11
0
 def to_value(self, sess: tf.Session):
     H, M, gs = sess.run([self.H, self.M, self.ground_state])
     return SparseDataValue(H=H,
                            M=M,
                            dense_shape=self.dense_shape,
                            ground_state=gs)