Skip to content

Commit 16fd96e

Browse files
committed
init commit
1 parent 35cb925 commit 16fd96e

File tree

5 files changed

+323
-30
lines changed

5 files changed

+323
-30
lines changed

.codecov.yml

Lines changed: 0 additions & 1 deletion
This file was deleted.

.travis.yml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,5 @@ notifications:
1010
email: false
1111
git:
1212
depth: 99999999
13-
14-
## uncomment the following lines to allow failures on nightly julia
15-
## (tests will run but not make your overall status red)
16-
#matrix:
17-
# allow_failures:
18-
# - julia: nightly
19-
20-
## uncomment and modify the following lines to manually install system packages
21-
#addons:
22-
# apt: # apt-get for linux
23-
# packages:
24-
# - gfortran
25-
#before_script: # homebrew for mac
26-
# - if [ $TRAVIS_OS_NAME = osx ]; then brew install gcc; fi
27-
28-
## uncomment the following lines to override the default test script
29-
#script:
30-
# - julia -e 'Pkg.clone(pwd()); Pkg.build("DiffTests"); Pkg.test("DiffTests"; coverage=true)'
3113
after_success:
32-
# push coverage results to Coveralls
3314
- julia -e 'cd(Pkg.dir("DiffTests")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
34-
# push coverage results to Codecov
35-
- julia -e 'cd(Pkg.dir("DiffTests")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
# DiffTests
22

3-
[![Build Status](https://travis-ci.org/jrevels/DiffTests.jl.svg?branch=master)](https://travis-ci.org/jrevels/DiffTests.jl)
4-
5-
[![Coverage Status](https://coveralls.io/repos/jrevels/DiffTests.jl/badge.svg?branch=master&service=github)](https://coveralls.io/github/jrevels/DiffTests.jl?branch=master)
6-
7-
[![codecov.io](http://codecov.io/github/jrevels/DiffTests.jl/coverage.svg?branch=master)](http://codecov.io/github/jrevels/DiffTests.jl?branch=master)
3+
DiffTests contains a common suite of test functions for stressing the robustness of differentiation tools.

src/DiffTests.jl

Lines changed: 272 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,276 @@
11
module DiffTests
22

3-
# package code goes here
3+
#=
4+
These functions are organized in sets based on input/output type. They are unary and not
5+
in-place unless otherwised specified. These functions have been written with the following
6+
assumptions:
7+
8+
- Array input is of length >= 5
9+
- Matrix input is square
10+
- Matrix inputs for n-ary functions are of equal shape
11+
12+
Some of these functions have been modified from their original form to to allow for tunable
13+
input/output sizes, or to test certain programmatic behaviors. Thus, regardless of their
14+
names, one should not expect these functions to be "correct" for their original purpose.
15+
=#
16+
17+
########################
18+
# f(x::Number)::Number #
19+
########################
20+
21+
num2num_1(x) = sin(x)^2 / cos(x)^2
22+
num2num_2(x) = 2*x + sqrt(x*x*x)
23+
num2num_3(x) = 10.31^(x + x) - x
24+
num2num_4(x) = 1
25+
num2num_5(x) = 1. / (1. + exp(-x))
26+
27+
const NUMBER_TO_NUMBER_FUNCS = (num2num_1, num2num_2, num2num_3,
28+
num2num_4, num2num_5, identity)
29+
30+
#######################
31+
# f(x::Number)::Array #
32+
#######################
33+
34+
function num2arr_1(x)
35+
return reshape([num2num_1(x),
36+
num2num_2(x),
37+
num2num_3(x),
38+
num2num_1(x) - num2num_2(x),
39+
num2num_2(x),
40+
num2num_3(x),
41+
num2num_2(x),
42+
num2num_3(x)], 2, 2, 2)
43+
end
44+
45+
const NUMBER_TO_ARRAY_FUNCS = (num2arr_1,)
46+
47+
#################################
48+
# f!(y::Array, x::Number)::Void #
49+
#################################
50+
51+
function num2arr_1!(y, x)
52+
fill!(y, zero(x))
53+
for i in 2:length(y)
54+
y[i] = (sin(x) + y[i-1])^2
55+
end
56+
return nothing
57+
end
58+
59+
const INPLACE_NUMBER_TO_ARRAY_FUNCS = (num2arr_1!,)
60+
61+
########################
62+
# f(x::Vector)::Number #
63+
########################
64+
65+
vec2num_1(x) = (exp(x[1]) + log(x[3]) * x[4]) / x[5]
66+
vec2num_2(x) = x[1]*x[2] + sin(x[1])
67+
vec2num_3(x) = vecnorm(x' .* x)
68+
vec2num_4(x) = ((sum(x) + prod(x)); 1)
69+
vec2num_5(x) = sum((-x).^3)
70+
vec2num_6(x) = sum([ifelse(i > 0, i, 0) for i in x])
71+
72+
function rosenbrock_1(x)
73+
a = one(eltype(x))
74+
b = 100 * a
75+
result = zero(eltype(x))
76+
for i in 1:length(x)-1
77+
result += (a - x[i])^2 + b*(x[i+1] - x[i]^2)^2
78+
end
79+
return result
80+
end
81+
82+
function rosenbrock_2(x)
83+
a = x[1]
84+
b = 100 * a
85+
v = map((i, j) -> (a - j)^2 + b*(i - j^2)^2, x[2:end], x[1:end-1])
86+
return sum(v)
87+
end
88+
89+
rosenbrock_3(x) = sum(map((i, j) -> (1 - j)^2 + 100*(i - j^2)^2, x[2:end], x[1:end-1]))
90+
91+
function rosenbrock_4(x)
92+
t1 = (1 + x[1:end-1]).^2
93+
t2 = x[2:end] + (x[1:end-1]).^2
94+
return sum(t1 + 100 * (abs.(t2)).^2)
95+
end
96+
97+
function ackley(x)
98+
a, b, c = 20.0, -0.2, 2.0*π
99+
len_recip = inv(length(x))
100+
sum_sqrs = zero(eltype(x))
101+
sum_cos = sum_sqrs
102+
for i in x
103+
sum_cos += cos(c*i)
104+
sum_sqrs += i^2
105+
end
106+
return (-a * exp(b * sqrt(len_recip*sum_sqrs)) -
107+
exp(len_recip*sum_cos) + a + e)
108+
end
109+
110+
self_weighted_logit(x) = inv(1.0 + exp(-dot(x, x)))
111+
112+
const VECTOR_TO_NUMBER_FUNCS = (vec2num_1, vec2num_2, vec2num_3, vec2num_4, vec2num_5,
113+
vec2num_6, rosenbrock_1, rosenbrock_2, rosenbrock_3,
114+
rosenbrock_4, ackley, self_weighted_logit, first)
115+
116+
########################
117+
# f(x::Matrix)::Number #
118+
########################
119+
120+
mat2num_1(x) = det(first(x) * inv(x * x) + x)
121+
122+
function mat2num_2(x)
123+
a = reshape(x, length(x), 1)
124+
b = reshape(copy(x), 1, length(x))
125+
return trace(log.((1 .+ (a * b)) .+ a .- b))
126+
end
127+
128+
function mat2num_3(x)
129+
k = length(x)
130+
N = isqrt(k)
131+
A = reshape(x, N, N)
132+
return sum(map(n -> sqrt(abs(n) + n^2) * 0.5, A))
133+
end
134+
135+
mat2num_4(x) = mean(sum(sin.(x) * x, 2))
136+
137+
softmax(x) = sum(exp.(x) ./ sum(exp.(x), 2))
138+
139+
const MATRIX_TO_NUMBER_FUNCS = (det, mat2num_1, mat2num_2, mat2num_3, mat2num_4, softmax)
140+
141+
####################
142+
# binary broadcast #
143+
####################
144+
145+
if VERSION >= v"0.6.0-dev.1614"
146+
const BINARY_BROADCAST_OPS = ((a, b) -> broadcast(+, a, b),
147+
(a, b) -> broadcast(-, a, b),
148+
(a, b) -> broadcast(*, a, b),
149+
(a, b) -> broadcast(/, a, b),
150+
(a, b) -> broadcast(\, a, b),
151+
(a, b) -> broadcast(^, a, b))
152+
else
153+
const BINARY_BROADCAST_OPS = (.+, .-, .*, ./, .\, .^)
154+
end
155+
156+
#################################
157+
# f(::Matrix, ::Matrix)::Number #
158+
#################################
159+
160+
const BINARY_MATRIX_TO_MATRIX_FUNCS = (+, -, *, /, \,
161+
BINARY_BROADCAST_OPS...,
162+
A_mul_Bt, At_mul_B, At_mul_Bt,
163+
A_mul_Bc, Ac_mul_B, Ac_mul_Bc)
164+
165+
###########################################
166+
# f(::Matrix, ::Matrix, ::Matrix)::Number #
167+
###########################################
168+
169+
relu(x) = log.(1.0 .+ exp.(x))
170+
sigmoid(n) = 1. / (1. + exp.(-n))
171+
neural_step(x1, w1, w2) = sigmoid(dot(w2[1:size(w1, 2)], relu(w1 * x1[1:size(w1, 2)])))
172+
173+
const TERNARY_MATRIX_TO_NUMBER_FUNCS = (neural_step,)
174+
175+
################################
176+
# f!(y::Array, x::Array)::Void #
177+
################################
178+
# Credit for `chebyquad!`, `brown_almost_linear!`, and `trigonometric!` goes to
179+
# Kristoffer Carlsson (@KristofferC).
180+
181+
function chebyquad!(y, x)
182+
tk = 1/length(x)
183+
for j = 1:length(x)
184+
temp1 = 1.0
185+
temp2 = 2x[j]-1
186+
temp = 2temp2
187+
for i = 1:length(y)
188+
y[i] += temp2
189+
ti = temp*temp2 - temp1
190+
temp1 = temp2
191+
temp2 = ti
192+
end
193+
end
194+
iev = -1.0
195+
for k = 1:length(y)
196+
y[k] *= tk
197+
if iev > 0
198+
y[k] += 1/(k^2-1)
199+
end
200+
iev = -iev
201+
end
202+
return nothing
203+
end
204+
205+
function brown_almost_linear!(y, x)
206+
c = sum(x) - (length(x) + 1)
207+
for i = 1:(length(x)-1), j = 1:(length(y)-1)
208+
y[j] += x[i] + c
209+
end
210+
y[length(y)] = prod(x) - 1
211+
return nothing
212+
end
213+
214+
function trigonometric!(y, x)
215+
for i in x
216+
for j in eachindex(y)
217+
y[j] = cos(i)
218+
end
219+
end
220+
c = sum(y)
221+
n = length(x)
222+
for i in x
223+
for j in eachindex(y)
224+
y[j] = sin(i) * y[j] + n - c
225+
end
226+
end
227+
return nothing
228+
end
229+
230+
function mutation_test_1!(y, x)
231+
y[1] = x[1]
232+
y[1] = y[1] * x[2]
233+
y[2] = y[2] * x[3]
234+
y[3] = sum(y)
235+
return nothing
236+
end
237+
238+
function mutation_test_2!(y, x)
239+
y[1] *= x[1]
240+
y[2] *= x[1]
241+
y[1] *= x[2]
242+
y[2] *= x[2]
243+
return nothing
244+
end
245+
246+
const INPLACE_ARRAY_TO_ARRAY_FUNCS = (chebyquad!, brown_almost_linear!, trigonometric!,
247+
mutation_test_1!, mutation_test_2!)
248+
249+
######################
250+
# f(x::Array)::Array #
251+
######################
252+
253+
chebyquad(x) = (y = zeros(x); chebyquad!(y, x); return y)
254+
255+
brown_almost_linear(x) = (y = zeros(x); brown_almost_linear!(y, x); return y)
256+
257+
trigonometric(x) = (y = ones(x); trigonometric!(y, x); return y)
258+
259+
mutation_test_1(x) = (y = zeros(x); mutation_test_1!(y, x); return y)
260+
261+
mutation_test_2(x) = (y = ones(x); mutation_test_2!(y, x); return y)
262+
263+
arr2arr_1(x) = (sum(x .* x); zeros(x))
264+
265+
arr2arr_2(x) = x[1, :] .+ x[1, :] .+ first(x)
266+
267+
const ARRAY_TO_ARRAY_FUNCS = (-, chebyquad, brown_almost_linear, trigonometric, arr2arr_1,
268+
arr2arr_2, mutation_test_1, mutation_test_2, identity)
269+
270+
#######################
271+
# f(::Matrix)::Matrix #
272+
#######################
273+
274+
const MATRIX_TO_MATRIX_FUNCS = (inv,)
4275

5276
end # module

test/runtests.jl

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
using DiffTests
22
using Base.Test
33

4-
# write your own tests here
5-
@test 1 == 2
4+
n = rand()
5+
x, y = rand(5, 5), rand(26)
6+
A, B = rand(5, 5), rand(5, 5)
7+
8+
# f returns Number
9+
10+
for f in DiffTests.NUMBER_TO_NUMBER_FUNCS
11+
@test isa(f(n), Number)
12+
end
13+
14+
for f in DiffTests.VECTOR_TO_NUMBER_FUNCS
15+
@test isa(f(y), Number)
16+
end
17+
18+
for f in DiffTests.MATRIX_TO_NUMBER_FUNCS
19+
@test isa(f(x), Number)
20+
end
21+
22+
for f in DiffTests.TERNARY_MATRIX_TO_NUMBER_FUNCS
23+
@test isa(f(A, B, x), Number)
24+
end
25+
26+
# f returns Array
27+
28+
for f in DiffTests.NUMBER_TO_ARRAY_FUNCS
29+
@test isa(f(n), Array)
30+
end
31+
32+
for f in DiffTests.ARRAY_TO_ARRAY_FUNCS
33+
@test isa(f(A), Array)
34+
@test isa(f(y), Array)
35+
end
36+
37+
for f in DiffTests.MATRIX_TO_MATRIX_FUNCS
38+
@test isa(f(A), Array)
39+
end
40+
41+
for f in DiffTests.BINARY_MATRIX_TO_MATRIX_FUNCS
42+
@test isa(f(A, B), Array)
43+
end
44+
45+
# f! returns Void
46+
47+
for f! in DiffTests.INPLACE_ARRAY_TO_ARRAY_FUNCS
48+
@test isa(f!(y, x), Void)
49+
end
50+
51+
for f! in DiffTests.INPLACE_NUMBER_TO_ARRAY_FUNCS
52+
@test isa(f!(y, n), Void)
53+
end

0 commit comments

Comments
 (0)