コード例 #1
0
def test_save(tf_graph, mocker, tmpdir):
    save_weights_path = str(tmpdir.dirpath("save_weights.npz"))
    params = get_params()
    rnn = RNN(params)
    with pytest.raises(UserWarning) as excinfo:
        rnn.save(save_weights_path)
    assert "No weights to return" in str(excinfo.value)
    mocker.patch.object(RNN, 'forward_pass')
    RNN.forward_pass.return_value = tf.fill(
        [params['N_batch'], params['N_steps'], params['N_out']],
        float('nan')), tf.fill(
            [params['N_batch'], params['N_steps'], params['N_rec']],
            float('nan'))
    rnn.build()
    rdm1 = rd.RDM(dt=params['dt'],
                  tau=params['tau'],
                  T=2000,
                  N_batch=params['N_batch'])
    gen1 = rdm1.batch_generator()
    rnn.train(gen1)

    assert not tmpdir.dirpath("save_weights.npz").check(exists=1)
    rnn.save(save_weights_path)
    assert tmpdir.dirpath("save_weights.npz").check(exists=1)

    tmpdir.dirpath("save_weights.npz").remove()
コード例 #2
0
def test_train(tf_graph, mocker, capfd):
    rdm = rd.RDM(dt=10, tau=100, T=2000, N_batch=128)
    gen = rdm.batch_generator()

    params = get_params()
    rnn = RNN(params)
    with pytest.raises(UserWarning) as excinfo:
        rnn.train(gen)
    assert 'build' in str(excinfo.value)

    mocker.patch.object(RNN, 'forward_pass')
    RNN.forward_pass.return_value = tf.fill(
        [params['N_batch'], params['N_steps'], params['N_out']],
        float('nan')), tf.fill(
            [params['N_batch'], params['N_steps'], params['N_rec']],
            float('nan'))
    rnn.build()

    rdm1 = rd.RDM(dt=params['dt'],
                  tau=params['tau'],
                  T=2000,
                  N_batch=params['N_batch'])
    gen1 = rdm1.batch_generator()
    assert rnn.is_initialized is False
    rnn.train(gen1)
    assert rnn.is_initialized is True
    out, _ = capfd.readouterr()
    assert out != ""
コード例 #3
0
def test_get_weights(tf_graph, mocker):
    params = get_params()
    rnn = RNN(params)
    with pytest.raises(UserWarning) as excinfo:
        rnn.get_weights()
    assert 'No weights to return yet -- model has not yet been initialized.' in str(
        excinfo.value)
    mocker.patch.object(RNN, 'forward_pass')
    RNN.forward_pass.return_value = tf.fill(
        [params['N_batch'], params['N_steps'], params['N_out']],
        float('nan')), tf.fill(
            [params['N_batch'], params['N_steps'], params['N_rec']],
            float('nan'))
    rnn.build()
    with pytest.raises(UserWarning) as excinfo:
        rnn.get_weights()
    assert 'No weights to return yet -- model has not yet been initialized.' in str(
        excinfo.value)
    rdm1 = rd.RDM(dt=params['dt'],
                  tau=params['tau'],
                  T=2000,
                  N_batch=params['N_batch'])
    gen1 = rdm1.batch_generator()
    rnn.train(gen1)
    assert type(rnn.get_weights()) is dict
コード例 #4
0
def test_custom_loss(tf_graph, mocker):

    params = get_params()
    params['loss_function'] = 'my_mean_squared_error'
    rnn = RNN(params)

    mocker.patch.object(RNN, 'forward_pass')
    RNN.forward_pass.return_value = tf.fill(
        [params['N_batch'], params['N_steps'], params['N_out']],
        float('nan')), tf.fill(
            [params['N_batch'], params['N_steps'], params['N_rec']],
            float('nan'))

    with pytest.raises(UserWarning) as excinfo:
        rnn.build()
    assert 'my_mean_squared_error' in str(excinfo.value)
    rnn.destruct()

    params['my_mean_squared_error'] = mean_squared_error
    rnn = RNN(params)

    mocker.patch.object(RNN, 'forward_pass')
    RNN.forward_pass.return_value = tf.fill(
        [params['N_batch'], params['N_steps'], params['N_out']],
        float('nan')), tf.fill(
            [params['N_batch'], params['N_steps'], params['N_rec']],
            float('nan'))

    rnn.build()
コード例 #5
0
ファイル: test_rnn.py プロジェクト: yulkang/PsychRNN
def test_build(tf_graph, mocker):
	pd = PerceptualDiscrimination(dt = 10, tau = 100, T = 2000, N_batch = 128)  
	gen = pd.batch_generator()

	params = get_params()
	rnn = RNN(params)
	mocker.patch.object(RNN, 'forward_pass')
	RNN.forward_pass.return_value = tf.fill([params['N_batch'], params['N_steps'], params['N_out']], float('nan')), tf.fill([params['N_batch'], params['N_steps'], params['N_rec']], float('nan'))
	rnn.build()
コード例 #6
0
ファイル: test_rnn.py プロジェクト: yulkang/PsychRNN
def test_destruct(tf_graph, mocker):
	params = get_params()
	rnn1 = RNN(params)
	rnn1.destruct()
	rnn2 = RNN(params)
	mocker.patch.object(RNN, 'forward_pass')
	RNN.forward_pass.return_value = tf.fill([params['N_batch'], params['N_steps'], params['N_out']], float('nan')), tf.fill([params['N_batch'], params['N_steps'], params['N_rec']], float('nan'))
	rnn2.build()
	rnn2.destruct()
	rnn3 = RNN(params)
コード例 #7
0
def test_custom_loss(tf_graph, mocker):

    params = get_params()
    params['custom_regularization'] = my_custom_regularization
    rnn = RNN(params)

    mocker.patch.object(RNN, 'forward_pass')
    RNN.forward_pass.return_value = tf.fill(
        [params['N_batch'], params['N_steps'], params['N_out']],
        float('nan')), tf.fill(
            [params['N_batch'], params['N_steps'], params['N_rec']],
            float('nan'))
    rnn.build()
コード例 #8
0
ファイル: test_rnn.py プロジェクト: plhijk/PsychRNN
def test_load_weights_path_rnn(tf_graph, mocker, tmpdir, capfd):
    params = get_params()

    pd1 = PerceptualDiscrimination(dt=params['dt'],
                                   tau=params['tau'],
                                   T=2000,
                                   N_batch=params['N_batch'])
    gen1 = pd1.batch_generator()

    mocker.patch.object(RNN, 'forward_pass')
    RNN.forward_pass.return_value = tf.fill(
        [params['N_batch'], params['N_steps'], params['N_out']],
        float('nan')), tf.fill(
            [params['N_batch'], params['N_steps'], params['N_rec']],
            float('nan'))

    rnn = RNN(params)
    rnn.build()

    train_params = {}
    train_params['save_weights_path'] = str(
        tmpdir.dirpath("save_weights.npz")
    )  # Where to save the model after training. Default: None
    train_params['verbosity'] = False

    ### save out some weights to test with and destroy the rnn that created them
    assert not tmpdir.dirpath("save_weights.npz").check(exists=1)
    rnn.train(gen1, train_params)

    assert rnn.is_initialized is True
    out, _ = capfd.readouterr()
    print(out)
    assert out == ""
    assert tmpdir.dirpath("save_weights.npz").check(exists=1)
    rnn.destruct()

    ### Make sure loading weights fails on nonexistent file
    params['load_weights_path'] = "nonexistent"
    with pytest.raises(EnvironmentError) as excinfo:
        rnn = RNN(params)
    assert "No such file" in str(excinfo.value)
    rnn.destruct()

    ### Ensure success when loading weights created previously
    params['load_weights_path'] = str(tmpdir.dirpath("save_weights.npz"))
    rnn = RNN(params)

    tmpdir.dirpath("save_weights.npz").remove()
コード例 #9
0
def test_test(mocker):
    rdm = rd.RDM(dt=10, tau=100, T=2000, N_batch=128)
    gen = rdm.batch_generator()
    x, y, m, p = next(gen)

    params = get_params()
    rnn = RNN(params)
    with pytest.raises(UserWarning) as excinfo:
        rnn.test(x)
    assert 'build' in str(excinfo.value)

    mocker.patch.object(RNN, 'forward_pass')
    RNN.forward_pass.return_value = tf.fill(
        [params['N_batch'], params['N_steps'], params['N_out']],
        float('nan')), tf.fill(
            [params['N_batch'], params['N_steps'], params['N_rec']],
            float('nan'))
    rnn.build()

    rnn.test(x)
コード例 #10
0
ファイル: test_rnn.py プロジェクト: yulkang/PsychRNN
def test_train_train_params_file_creation(tf_graph, mocker, tmpdir, capfd):
	params = get_params()

	pd1 = PerceptualDiscrimination(dt = params['dt'], tau = params['tau'], T = 2000, N_batch = params['N_batch'])  
	gen1 = pd1.batch_generator()
	pd2 = PerceptualDiscrimination(dt = params['dt'], tau = params['tau'], T = 1000, N_batch = params['N_batch'])
	gen2 = pd2.batch_generator()

	mocker.patch.object(RNN, 'forward_pass')
	RNN.forward_pass.return_value = tf.fill([params['N_batch'], params['N_steps'], params['N_out']], float('nan')), tf.fill([params['N_batch'], params['N_steps'], params['N_rec']], float('nan'))
	
	rnn =RNN(params)
	rnn.build()

	train_params = {}
	train_params['save_weights_path'] =  str(tmpdir.dirpath("save_weights.npz")) # Where to save the model after training. Default: None
	train_params['training_iters'] = 1000 # number of iterations to train for Default: 10000
	train_params['learning_rate'] = .01 # Sets learning rate if use default optimizer Default: .001
	train_params['loss_epoch'] = 20 # Compute and recopd loss every 'loss_epoch' epochs. Default: 10
	train_params['verbosity'] = False
	train_params['save_training_weights_epoch'] = 10 # save training weights every 'save_training_weights_epoch' epochs. Default: 100
	train_params['training_weights_path'] = str(tmpdir.dirpath("training_weights")) # where to save training weights as training progresses. Default: None
	train_params['generator_function'] = gen2 # replaces trial_batch_generator with the generator_function when not none. Default: None
	train_params['optimizer'] = tf.compat.v1.train.AdamOptimizer(learning_rate=train_params['learning_rate']) # What optimizer to use to compute gradients. Default: tf.train.AdamOptimizer(learning_rate=train_params['learning_rate'])
	train_params['clip_grads'] = False # If true, clip gradients by norm 1. Default: True
	
	assert not tmpdir.dirpath("save_weights.npz").check(exists=1)
	assert not tmpdir.dirpath("training_weights" + str(train_params['save_training_weights_epoch'])).check(exists=1)
	rnn.train(gen1, train_params)


	assert rnn.is_initialized is True
	out, _ = capfd.readouterr()
	print(out)
	assert out==""
	assert tmpdir.dirpath("save_weights.npz").check(exists=1)
	assert tmpdir.dirpath("training_weights" + str(train_params['save_training_weights_epoch'])+ ".npz").check(exists=1)