Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I try to obtain DeepIV.ate_inference, I get an error. #941

Closed
vekeywang opened this issue Dec 28, 2024 · 0 comments
Closed

When I try to obtain DeepIV.ate_inference, I get an error. #941

vekeywang opened this issue Dec 28, 2024 · 0 comments

Comments

@vekeywang
Copy link

vekeywang commented Dec 28, 2024

When attempting to access ate_inference from the DeepIV object, I consistently encounter the error 'DeepIV' object has no attribute '_inference'. However, I am able to obtain the ate value without any issues.I have defined my inference method using the string inference='bootstrap'. Additionally, I am using TensorFlow version 2.10.0, Keras version 2.10.0, and Python version 3.10.0.
Could you kindly assist me in understanding what might be causing this issue? Any guidance would be greatly appreciated.

import numpy as np
import keras
from keras import backend as K
import keras.layers as L
from keras.models import Model
from econml.iv.nnet import DeepIV
from econml.inference import BootstrapInference
np.random.seed(42)
n = 5000  
e = np.random.normal(size=(n,))  
x = np.random.uniform(low=0.0, high=10.0, size=(n,))  
z = np.random.uniform(low=0.0, high=10.0, size=(n,)) 
t = np.sqrt((x + 2) * z) + e  
y = t * t / 10 - x * t / 10 + e  
y = y.reshape(-1, 1)
t = t.reshape(-1, 1)
x = x.reshape(-1, 1)
z = z.reshape(-1, 1)
treatment_model = keras.Sequential([
    keras.layers.Dense(64, activation='relu', input_shape=(2,)),  
    keras.layers.Dense(32, activation='relu'),  
    keras.layers.Dense(1)  
])
response_model = keras.Sequential([
    keras.layers.Dense(64, activation='relu', input_shape=(2,)), 
    keras.layers.Dense(32, activation='relu'),  
    keras.layers.Dense(1)  
])
keras_fit_options = {
    "epochs": 30,  
    "validation_split": 0.1,  
    "callbacks": [keras.callbacks.EarlyStopping(patience=2, restore_best_weights=True)] 
}
deepIvEst = DeepIV(
    n_components=10,  
    m=lambda z, x: treatment_model(keras.layers.concatenate([z, x])),  
    h=lambda t, x: response_model(keras.layers.concatenate([t, x])),  
    n_samples=1,  
    use_upper_bound_loss=False,  
    n_gradient_samples=1,  
    optimizer='adam',  
    first_stage_options=keras_fit_options,  
    second_stage_options=keras_fit_options  
)
inference = BootstrapInference(n_bootstrap_samples=100, n_jobs=-1, bootstrap_type='pivot')
try:
    deepIvEst.fit(Y=y, T=t, X=x, Z=z, inference=inference)
    print("Model fitting completed successfully.")
    if hasattr(deepIvEst, '_inference'):
        print("Inference object was correctly set.")
    else:
        print("Inference object was not set.")
except Exception as e:
    print("Model fitting failed:", e)
T0 = np.zeros_like(t)
T1 = np.ones_like(t)
try:
    ate = deepIvEst.ate(X=x, T0=T0, T1=T1)
    ate_interval = deepIvEst.ate_interval(X=x, T0=T0, T1=T1, alpha=0.05)
    ate_inference = deepIvEst.ate_inference(X=x, T0=T0, T1=T1)
    print(ate_inference.summary())
except Exception as e:
    print("ATE inference failed:", e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant