Skip to content

Commit f6cac43

Browse files
committed
Relax tensorflow version limit
1 parent 2cc9f62 commit f6cac43

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

econml/iv/nnet/_deepiv.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ def mog_loss_model(n_components, d_t):
9393
# LL = C - log(sum(pi_i/sig^d * exp(-d2/(2*sig^2))))
9494
# Use logsumexp for numeric stability:
9595
# LL = C - log(sum(exp(-d2/(2*sig^2) + log(pi_i/sig^d))))
96-
# TODO: does the numeric stability actually make any difference?
9796
def make_logloss(d2, sig, pi):
98-
return -K.logsumexp(-d2 / (2 * K.square(sig)) + K.log(pi / K.pow(sig, d_t)), axis=-1)
97+
# logsumexp doesn't exist in keras 2.4; simulate it
98+
values = - d2 / (2 * K.square(sig)) + K.log(pi / K.pow(sig, d_t))
99+
# logsumexp(a,b,c) = log(exp(a)+exp(b)+exp(c)) = log((exp(a-k)+exp(b-k)+exp(c-k))*exp(k))
100+
# = log((exp(a-k)+exp(b-k)+exp(c-k))) + k
101+
mx = K.max(values, axis=-1)
102+
return -K.log(K.sum(K.exp(values - L.Reshape((-1, 1))(mx)), axis=-1)) - mx
99103

100104
ll = L.Lambda(lambda dsp: make_logloss(*dsp), output_shape=(1,))([d2, sig, pi])
101105

@@ -350,7 +354,7 @@ def fit(self, Y, T, X, Z, *, inference=None):
350354
model.add_loss(L.Lambda(K.mean)(ll))
351355
model.compile(self._optimizer)
352356
# TODO: do we need to give the user more control over other arguments to fit?
353-
model.fit([Z, X, T], [], **self._first_stage_options)
357+
model.fit([Z, X, T], **self._first_stage_options)
354358

355359
lm = response_loss_model(lambda t, x: self._h(t, x),
356360
lambda z, x: Model([z_in, x_in],
@@ -365,7 +369,7 @@ def fit(self, Y, T, X, Z, *, inference=None):
365369
response_model.add_loss(L.Lambda(K.mean)(rl))
366370
response_model.compile(self._optimizer)
367371
# TODO: do we need to give the user more control over other arguments to fit?
368-
response_model.fit([Z, X, Y], [], **self._second_stage_options)
372+
response_model.fit([Z, X, Y], **self._second_stage_options)
369373

370374
self._effect_model = Model([t_in, x_in], [self._h(t_in, x_in)])
371375

econml/tests/test_deepiv.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,27 @@ def test_stop_grad(self):
3232
model = keras.Model([x_input, y_input, z_input], [loss])
3333
model.add_loss(K.mean(loss))
3434
model.compile('nadam')
35-
model.fit([np.array([[1]]), np.array([[2]]), np.array([[0]])], [])
35+
model.fit([np.array([[1]]), np.array([[2]]), np.array([[0]])])
36+
37+
def test_mog_loss(self):
38+
inputs = [keras.layers.Input(shape=s) for s in [(3,), (3, 2), (3,), (2,)]]
39+
ll_model = keras.engine.Model(inputs, mog_loss_model(3, 2)(inputs))
40+
41+
for n in range(10):
42+
ps = -np.log(np.random.uniform(size=(3,)))
43+
pi = ps / np.sum(ps)
44+
mu = np.random.normal(size=(3, 2))
45+
sig = np.exp(np.random.normal(size=3,))
46+
t = np.random.normal(size=(2,))
47+
48+
pred = ll_model.predict([pi.reshape(1, 3), mu.reshape(1, 3, 2), sig.reshape(1, 3), t.reshape(1, 2)])
49+
50+
# LL = C - log(sum(pi_i/sig^d * exp(-d2/(2*sig^2))))
51+
d = mu - t.reshape(-1, 2)
52+
d2 = np.sum(d * d, axis=-1)
53+
ll = -np.log(np.sum(pi / (sig * sig) * np.exp(-d2 / (2 * sig * sig)), axis=0))
54+
55+
assert np.allclose(ll, pred[0])
3656

3757
@pytest.mark.slow
3858
def test_deepiv_shape(self):
@@ -500,7 +520,7 @@ def norm(lr):
500520
model = keras.engine.Model([x_input, t_input], [ll])
501521
model.add_loss(K.mean(ll))
502522
model.compile('nadam')
503-
model.fit([x, t], [], epochs=5)
523+
model.fit([x, t], epochs=5)
504524

505525
# For some reason this doesn't work at all when run against the CNTK backend...
506526
# model.compile('nadam', loss=lambda _,l:l)
@@ -559,7 +579,7 @@ def sample(n):
559579
model = keras.engine.Model([x_input, t_input], [ll])
560580
model.add_loss(K.mean(ll))
561581
model.compile('nadam')
562-
model.fit([x, t], [], epochs=100)
582+
model.fit([x, t], epochs=100)
563583

564584
model2 = keras.engine.Model([x_input], [pi, mu, sig])
565585
import matplotlib

setup.cfg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ automl =
6060
; azureml-sdk[explain,automl] == 1.0.83
6161
azure-cli
6262
tf =
63-
keras < 2.4
64-
tensorflow > 1.10, < 2.3
63+
keras
64+
tensorflow > 1.10, < 2.4
6565
plt =
6666
matplotlib
6767
all =
6868
azure-cli
69-
keras < 2.4
70-
tensorflow > 1.10, < 2.3
69+
keras
70+
tensorflow > 1.10, < 2.4
7171
matplotlib
7272

7373
[options.packages.find]

0 commit comments

Comments
 (0)