Esempio n. 1
0
def test_run():
    ###### Test updating F, G, S in that order
    # Test whether a single iteration gives the correct behaviour, updating F, G, S in that order
    R = numpy.array([[1,2],[3,4]],dtype='f')
    M = numpy.array([[1,1],[0,1]])
    K = 3
    L = 1
    
    F = numpy.array([[1,2,3],[4,5,6]],dtype='f')
    S = numpy.array([[7],[8],[9]],dtype='f')
    G = numpy.array([[10],[11]],dtype='f')
    #FSG = numpy.array([[500,550],[1220,1342]],dtype='f')
    #FS = numpy.array([[50],[122]],dtype='f')
    #SG = numpy.array([[70,77],[80,88],[90,99]],dtype='f')
    
    nmtf = NMTF(R,M,K,L)
    
    # Check we get an Exception if W, H are undefined
    with pytest.raises(AssertionError) as error:
        nmtf.run(0)
    assert str(error.value) == "F, S and G have not been initialised - please run NMTF.initialise() first."       
    
    nmtf.F = numpy.copy(F)
    nmtf.S = numpy.copy(S)
    nmtf.G = numpy.copy(G) 
    
    nmtf.run(1)
Esempio n. 2
0
R = numpy.loadtxt(input_folder + "R.txt")
M = numpy.ones((I, J))

# Run the VB algorithm, <repeats> times
times_repeats = []
performances_repeats = []
for i in range(0, repeats):
    # Set all the seeds
    numpy.random.seed(3)
    random.seed(4)
    scipy.random.seed(5)

    # Run the classifier
    nmtf = NMTF(R, M, K, L)
    nmtf.initialise(init_S, init_FG, expo_prior)
    nmtf.run(iterations)

    # Extract the performances and timestamps across all iterations
    times_repeats.append(nmtf.all_times)
    performances_repeats.append(nmtf.all_performances)

# Check whether seed worked: all performances should be the same
assert all([numpy.array_equal(performances, performances_repeats[0]) for performances in performances_repeats]), \
    "Seed went wrong - performances not the same across repeats!"

# Print out the performances, and the average times
all_times_average = list(numpy.average(times_repeats, axis=0))
all_performances = performances_repeats[0]
print "np_all_times_average = %s" % all_times_average
print "np_all_performances = %s" % all_performances