コード例 #1
0
    def encoding_func(features):
        """
        Take in 'n' features as a numpy array, and output a 'dim' dimensional SSP
        """

        vec = power(axis_vectors[0], features[0])
        for i in range(1, n):
            vec *= power(axis_vectors[i], features[i])

        return vec.v
コード例 #2
0
    def encoding_func(features):
        """
        Take in 'n' features as a numpy array, and output a 'dim' dimensional SSP
        """
        # TODO: any scaling required?
        # TODO: make sure matrix multiply order is correct
        tranformed_features = transform_axes @ features

        vec = power(axis_vectors[0], tranformed_features[0])
        for i in range(1, n + 1):
            vec *= power(axis_vectors[i], tranformed_features[i])

        return vec.v
コード例 #3
0
def encode_point_n(x, y, axis_sps, x_axis, y_axis):
    """
    Encodes a given 2D point as a ND SSP
    """

    # 2D point represented as an N dimensional vector in the plane spanned by 'x_axis' and 'y_axis'

    vec = (x_axis * x + y_axis * y)

    # Generate the SSP from the high dimensional vector, by convolving all of the axis vector components together
    ret = power(axis_sps[0], vec[0])
    for i in range(1, len(axis_sps)):
        ret *= power(axis_sps[i], vec[i])

    return ret
コード例 #4
0
def encode_dataset(data, dim=256, seed=13, scale=1.0):
    """
    :param data: the data to be encoded
    :param dim: dimensionality of the SSP
    :param seed: seed for the single axis vector
    :param scale: scaling of the data for the encoding
    :return:
    """
    rng = np.random.RandomState(seed=seed)
    # TODO: have option to normalize everything first, for consistent relative scale
    axis_vec = make_good_unitary(dim, rng=rng)

    n_samples = data.shape[0]
    n_features = data.shape[1]

    n_out_features = n_features * dim

    data_out = np.zeros((n_samples, n_out_features))

    for s in range(n_samples):
        for f in range(n_features):
            data_out[s, f * dim:(f + 1) * dim] = power(axis_vec,
                                                       data[s, f] * scale).v

    return data_out
コード例 #5
0
def get_axes_and_scale(dim=256, n=3, seed=13, apply_scaling=True):
    """
    Get X and Y axis vectors based on an n dimensional projection.
    Also return the correct scaling so the size of the bump in the heatmap is consistent
    """
    rng = np.random.RandomState(seed=seed)

    points_nd = np.eye(n) * np.sqrt(n)
    # points in 2D that will correspond to each axis, plus one at zero
    points_2d = np.zeros((n, 2))
    thetas = np.linspace(0, 2 * np.pi, n + 1)[:-1]
    # TODO: will want a scaling here, or along the high dim axes
    for i, theta in enumerate(thetas):
        points_2d[i, 0] = np.cos(theta)
        points_2d[i, 1] = np.sin(theta)

    transform_mat = np.linalg.lstsq(points_2d, points_nd)

    x_axis = transform_mat[0][0, :]
    y_axis = transform_mat[0][1, :]

    if apply_scaling:
        x_axis /= transform_mat[3][0]
        y_axis /= transform_mat[3][1]

    axis_sps = []
    for i in range(n):
        # random unitary vector
        axis_sps.append(make_good_unitary(dim, rng=rng))

    X = power(axis_sps[0], x_axis[0])
    Y = power(axis_sps[0], y_axis[0])
    for i in range(1, n):
        X *= power(axis_sps[i], x_axis[i])
        Y *= power(axis_sps[i], y_axis[i])

    return X, Y, transform_mat[3][0]
コード例 #6
0
    X_vec = circulant_matrix_to_vec(covariance)
    X = spa.SemanticPointer(data=X_vec)

    # X.make_unitary()
    X_circ = circulant(X.v)
else:
    raise NotImplementedError

plt.figure()
plt.imshow(X_circ)

similarity = np.zeros((args.res, ))
zero_vec = np.zeros((dim, ))
zero_vec[0] = 1

for i, x in enumerate(xs):
    p = power(X, x)
    similarity[i] = np.dot(p.v, zero_vec)

plt.figure()
plt.plot(similarity)

plt.show()

# def compute_circulant_matrix(vec):
#
#     mat = np.zeros((len(vec), len(vec)))
#
#     for i in range(len(vec)):
#         mat[i, :]
コード例 #7
0
fig, ax = plt.subplots(1, 3, figsize=(8, 4))

sigma_normal = 0.5
sigma_hex = 0.5
sigma_hex_c = 0.5

sim_hex = np.zeros((res, ))
sim_hex_c = np.zeros((res, ))  # this version has axes generated together and then converted to 2D
sim_normal = np.zeros((res, ))
gauss_hex = gaussian_1d(0, sigma_hex, xs)
gauss_hex_c = gaussian_1d(0, sigma_hex_c, xs)
gauss_normal = gaussian_1d(0, sigma_normal, xs)

for i, x, in enumerate(xs):
    sim_normal[i] = power(X, x).v[0]
    sim_hex_c[i] = power(Xh, x).v[0]
    sim_hex[i] = encode_point_hex(0, x, X, Y, Z).v[0]

ax[0].plot(xs, sim_normal)
ax[0].plot(xs, gauss_normal)
ax[1].plot(xs, sim_hex)
ax[1].plot(xs, gauss_hex)
ax[2].plot(xs, sim_hex_c)
ax[2].plot(xs, gauss_hex_c)

for i in range(len(ax)):
    ax[i].set_ylim([-.2, 1.2])

plt.show()
コード例 #8
0
def orthogonal_hex_dir_7dim(phi=np.pi / 2., angle=0):
    dim = 7
    xf = np.zeros((dim, ), dtype='Complex64')
    xf[0] = 1
    xf[1] = np.exp(1.j * phi)
    xf[2] = 1
    xf[3] = 1
    xf[4] = np.conj(xf[3])
    xf[5] = np.conj(xf[2])
    xf[6] = np.conj(xf[1])

    yf = np.zeros((dim, ), dtype='Complex64')
    yf[0] = 1
    yf[1] = 1
    yf[2] = np.exp(1.j * phi)
    yf[3] = 1
    yf[4] = np.conj(yf[3])
    yf[5] = np.conj(yf[2])
    yf[6] = np.conj(yf[1])

    zf = np.zeros((dim, ), dtype='Complex64')
    zf[0] = 1
    zf[1] = 1
    zf[2] = 1
    zf[3] = np.exp(1.j * phi)
    zf[4] = np.conj(zf[3])
    zf[5] = np.conj(zf[2])
    zf[6] = np.conj(zf[1])

    Xh = np.fft.ifft(xf).real
    Yh = np.fft.ifft(yf).real
    Zh = np.fft.ifft(zf).real

    # checks to make sure everything worked correctly
    assert np.allclose(np.abs(xf), 1)
    assert np.allclose(np.abs(yf), 1)
    assert np.allclose(np.fft.fft(Xh), xf)
    assert np.allclose(np.fft.fft(Yh), yf)
    assert np.allclose(np.linalg.norm(Xh), 1)
    assert np.allclose(np.linalg.norm(Yh), 1)

    axis_sps = [
        spa.SemanticPointer(data=Xh),
        spa.SemanticPointer(data=Yh),
        spa.SemanticPointer(data=Zh),
    ]

    n = 3
    points_nd = np.eye(n) * np.sqrt(n)
    # points in 2D that will correspond to each axis, plus one at zero
    points_2d = np.zeros((n, 2))
    thetas = np.linspace(0, 2 * np.pi, n + 1)[:-1] + angle
    # TODO: will want a scaling here, or along the high dim axes
    for i, theta in enumerate(thetas):
        points_2d[i, 0] = np.cos(theta)
        points_2d[i, 1] = np.sin(theta)

    transform_mat = np.linalg.lstsq(points_2d, points_nd)

    x_axis = transform_mat[0][0, :] / transform_mat[3][0]
    y_axis = transform_mat[0][1, :] / transform_mat[3][1]

    X = power(axis_sps[0], x_axis[0])
    Y = power(axis_sps[0], y_axis[0])
    for i in range(1, n):
        X *= power(axis_sps[i], x_axis[i])
        Y *= power(axis_sps[i], y_axis[i])

    sv = transform_mat[3][0]
    return X, Y, sv, transform_mat[0]
コード例 #9
0
 def encoding_func(feature):
     return power(axis_vec, feature * scale * axis_scaling).v
コード例 #10
0
ファイル: utils.py プロジェクト: bjkomer/ssp-experiments
def encode_point_3d(x, y, z, x_axis_sp, y_axis_sp, z_axis_sp):

    return power(x_axis_sp, x) * power(y_axis_sp, y) * power(z_axis_sp, z)
コード例 #11
0
locs = [
    0,
    2.2,
    -3.4,
]
locs = [
    0,
    3.2,
]
n_locs = len(locs)

vecs = np.zeros((n_locs, dim))

for i, loc in enumerate(locs):
    vecs[i, :] = power(X, loc).v

sims = np.zeros((res, n_locs))

for i, x in enumerate(xs):
    hmv[i, :] = power(X, x).v
    for j in range(n_locs):
        sims[i, j] = np.dot(vecs[j], hmv[i, :])

plt.plot(xs, sims, linewidth=2.5)

plt.legend(["k = {}".format(k) for k in locs], fontsize=16)

plt.xlabel("Exponent", fontsize=18)
plt.ylabel("Dot Product", fontsize=18)