示例#1
0
def test_sharing():
    data = Frame(create_test_data())
    f1 = FrameRef(data, index=toindex([0, 1, 2, 3]))
    f2 = FrameRef(data, index=toindex([2, 3, 4, 5, 6]))
    # test read
    for k, v in f1.items():
        assert U.allclose(data[k].data[0:4], v)
    for k, v in f2.items():
        assert U.allclose(data[k].data[2:7], v)
    f2_a1 = f2['a1'].data
    # test write
    # update own ref should not been seen by the other.
    f1[Index(th.tensor([0, 1]))] = {
        'a1': th.zeros([2, D]),
        'a2': th.zeros([2, D]),
        'a3': th.zeros([2, D]),
    }
    assert U.allclose(f2['a1'], f2_a1)
    # update shared space should been seen by the other.
    f1[Index(th.tensor([2, 3]))] = {
        'a1': th.ones([2, D]),
        'a2': th.ones([2, D]),
        'a3': th.ones([2, D]),
    }
    f2_a1[0:2] = th.ones([2, D])
    assert U.allclose(f2['a1'], f2_a1)
示例#2
0
def test_sharing():
    data = Frame(create_test_data())
    f1 = FrameRef(data, index=toindex([0, 1, 2, 3]))
    f2 = FrameRef(data, index=toindex([2, 3, 4, 5, 6]))
    # test read
    for k, v in f1.items():
        assert F.allclose(F.narrow_row(data[k].data, 0, 4), v)
    for k, v in f2.items():
        assert F.allclose(F.narrow_row(data[k].data, 2, 7), v)
    f2_a1 = f2['a1']
    # test write
    # update own ref should not been seen by the other.
    f1[Index(F.tensor([0, 1]))] = {
        'a1': F.zeros([2, D]),
        'a2': F.zeros([2, D]),
        'a3': F.zeros([2, D]),
    }
    assert F.allclose(f2['a1'], f2_a1)
    # update shared space should been seen by the other.
    f1[Index(F.tensor([2, 3]))] = {
        'a1': F.ones([2, D]),
        'a2': F.ones([2, D]),
        'a3': F.ones([2, D]),
    }
    F.narrow_row_set(f2_a1, 0, 2, F.ones([2, D]))
    assert F.allclose(f2['a1'], f2_a1)
示例#3
0
def test_slicing():
    data = Frame(create_test_data(grad=True))
    f1 = FrameRef(data, index=toindex(slice(1, 5)))
    f2 = FrameRef(data, index=toindex(slice(3, 8)))
    # test read
    for k, v in f1.items():
        assert U.allclose(data[k].data[1:5], v)
    f2_a1 = f2['a1'].data
    # test write
    f1[Index(th.tensor([0, 1]))] = {
        'a1': th.zeros([2, D]),
        'a2': th.zeros([2, D]),
        'a3': th.zeros([2, D]),
    }
    assert U.allclose(f2['a1'], f2_a1)

    f1[Index(th.tensor([2, 3]))] = {
        'a1': th.ones([2, D]),
        'a2': th.ones([2, D]),
        'a3': th.ones([2, D]),
    }
    f2_a1[toindex(slice(0, 2))] = 1
    assert U.allclose(f2['a1'], f2_a1)

    f1[toindex(slice(2, 4))] = {
        'a1': th.zeros([2, D]),
        'a2': th.zeros([2, D]),
        'a3': th.zeros([2, D]),
    }
    f2_a1[toindex(slice(0, 2))] = 0
    assert U.allclose(f2['a1'], f2_a1)
示例#4
0
def test_slicing():
    data = Frame(create_test_data(grad=True))
    f1 = FrameRef(data, index=toindex(slice(1, 5)))
    f2 = FrameRef(data, index=toindex(slice(3, 8)))
    # test read
    for k, v in f1.items():
        assert F.allclose(F.narrow_row(data[k].data, 1, 5), v)
    f2_a1 = f2['a1']  # is a tensor
    # test write
    f1[Index(F.tensor([0, 1]))] = {
        'a1': F.zeros([2, D]),
        'a2': F.zeros([2, D]),
        'a3': F.zeros([2, D]),
    }
    assert F.allclose(f2['a1'], f2_a1)

    f1[Index(F.tensor([2, 3]))] = {
        'a1': F.ones([2, D]),
        'a2': F.ones([2, D]),
        'a3': F.ones([2, D]),
    }
    F.narrow_row_set(f2_a1, 0, 2, 1)
    assert F.allclose(f2['a1'], f2_a1)

    f1[toindex(slice(2, 4))] = {
        'a1': F.zeros([2, D]),
        'a2': F.zeros([2, D]),
        'a3': F.zeros([2, D]),
    }
    F.narrow_row_set(f2_a1, 0, 2, 0)
    assert F.allclose(f2['a1'], f2_a1)
示例#5
0
def test_row3():
    # test row delete
    data = Frame(create_test_data())
    f = FrameRef(data)
    assert f.is_contiguous()
    assert f.is_span_whole_column()
    assert f.num_rows == N
    del f[toindex(th.tensor([2, 3]))]
    assert not f.is_contiguous()
    assert not f.is_span_whole_column()
    # delete is lazy: only reflect on the ref while the
    # underlying storage should not be touched
    assert f.num_rows == N - 2
    assert data.num_rows == N
    newidx = list(range(N))
    newidx.pop(2)
    newidx.pop(2)
    newidx = toindex(newidx)
    for k, v in f.items():
        assert U.allclose(v, data[k][newidx])