コード例 #1
0
def deep_iv_fit(x, z, t, y, epochs=100, hidden=[128, 64, 32]):
    from deepiv.models import Treatment, Response
    import deepiv.architectures as architectures
    import deepiv.densities as densities
    from keras.layers import Input, Dense
    from keras.models import Model
    from keras.layers.merge import Concatenate
    n = z.shape[0]
    dropout_rate = min(1000. / (1000. + n), 0.5)
    batch_size = 100
    images = False
    act = "relu"
    n_components = 10
    instruments = Input(shape=(z.shape[1], ), name="instruments")
    features = Input(shape=(x.shape[1], ), name="features")
    treatment_input = Concatenate(axis=1)([instruments, features])
    est_treat = architectures.feed_forward_net(
        treatment_input,
        lambda x: densities.mixture_of_gaussian_output(x, n_components),
        hidden_layers=hidden,
        dropout_rate=dropout_rate,
        l2=0.0001,
        activations=act)

    treatment_model = Treatment(inputs=[instruments, features],
                                outputs=est_treat)
    treatment_model.compile('adam',
                            loss="mixture_of_gaussians",
                            n_components=n_components)

    treatment_model.fit([z, x], t, epochs=epochs, batch_size=batch_size)

    # Build and fit response model
    treatment = Input(shape=(t.shape[1], ), name="treatment")
    response_input = Concatenate(axis=1)([features, treatment])
    est_response = architectures.feed_forward_net(response_input,
                                                  Dense(1),
                                                  activations=act,
                                                  hidden_layers=hidden,
                                                  l2=0.001,
                                                  dropout_rate=dropout_rate)
    response_model = Response(treatment=treatment_model,
                              inputs=[features, treatment],
                              outputs=est_response)
    response_model.compile('adam', loss='mse')
    response_model.fit([z, x],
                       y,
                       epochs=epochs,
                       verbose=1,
                       batch_size=batch_size,
                       samples_per_batch=2)

    return response_model
コード例 #2
0
    't': t.shape,
    'y': y.shape
}))

# Build and fit treatment model
if K.image_data_format() == "channels_first":
    image_shape = (1, 28, 28)
else:
    image_shape = (28, 28, 1)

images = Input(shape=(28 * 28, ), name='treat_images')
image_reshaped = Reshape(image_shape)(images)  # reshape
time = Input(shape=(1, ), name='treat_time')
instruments = Input(shape=(z.shape[1], ), name='treat_instruments')

mix_gaussian_output = lambda x: densities.mixture_of_gaussian_output(x, 10)

treatment_output = conv_embedding(image_reshaped,
                                  mix_gaussian_output, [time, instruments],
                                  dropout_rate=dropout_rate,
                                  embedding_dropout=embedding_dropout,
                                  embedding_l2=embedding_l2)

treatment_model = Treatment(inputs=[instruments, time, images],
                            outputs=treatment_output)
treatment_model.compile('adam', loss="mixture_of_gaussians", n_components=10)

treatment_model.fit([z, x[:, 0:1], x[:, 1:]],
                    t,
                    epochs=epochs,
                    batch_size=batch_size)
コード例 #3
0
Treament:{t},\n\
Response:{y}".format(**{'x':x.shape, 'z':z.shape,
                        't':t.shape, 'y':y.shape}))

# Build and fit treatment model
instruments = Input(shape=(z.shape[1],), name="instruments")
features = Input(shape=(x.shape[1],), name="features")
treatment_input = Concatenate(axis=1)([instruments, features])

hidden = [128, 64, 32]

act = "relu"

n_components = 10

est_treat = architectures.feed_forward_net(treatment_input, lambda x: densities.mixture_of_gaussian_output(x, n_components),
                                           hidden_layers=hidden,
                                           dropout_rate=dropout_rate, l2=0.0001,
                                           activations=act)

treatment_model = Treatment(inputs=[instruments, features], outputs=est_treat)
treatment_model.compile('adam',
                        loss="mixture_of_gaussians",
                        n_components=n_components)

treatment_model.fit([z, x], t, epochs=epochs, batch_size=batch_size)

# Build and fit response model

treatment = Input(shape=(t.shape[1],), name="treatment")
response_input = Concatenate(axis=1)([features, treatment])
コード例 #4
0
    for j in range(n_trials):
        x, z, t, y, g_true = datafunction(n, 1)
        print('N={}, DeepIV Trial: %{}\n'.format(n, j / n_trials * 100))
        #         print("Data shapes:\n\
        #         Features:{x},\n\
        #         Instruments:{z},\n\
        #         Treament:{t},\n\
        #         Response:{y}".format(**{'x':x.shape, 'z':z.shape,
        #                                 't':t.shape, 'y':y.shape}))
        # Build and fit treatment model
        instruments = Input(shape=(z.shape[1], ), name="instruments")
        features = Input(shape=(x.shape[1], ), name="features")
        treatment_input = Concatenate(axis=1)([instruments, features])
        est_treat = architectures.feed_forward_net(
            treatment_input,
            lambda x: densities.mixture_of_gaussian_output(x, n_components),
            hidden_layers=hidden,
            dropout_rate=dropout_rate,
            l2=0.0001,
            activations=act)
        treatment_model = Treatment(inputs=[instruments, features],
                                    outputs=est_treat)
        treatment_model.compile('adam',
                                loss="mixture_of_gaussians",
                                n_components=n_components)

        treatment_model.fit([z, x],
                            t,
                            epochs=epochs,
                            batch_size=batch_size,
                            verbose=False)