├── .gitattributes ├── 9781484234525.jpg ├── Contributing.md ├── FCC.py ├── LICENSE.txt ├── README.md ├── errata.md ├── example1.py ├── sequence_2_sequence.py └── wide_and_deep.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /9781484234525.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/Intro-Deep-Learning-Biz-Applications-for-Devs/ed9ef0a7b75468c413744fd0c0c490b03bdfa243/9781484234525.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /FCC.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import os 3 | from PIL import Image 4 | import numpy as np 5 | from keras.layers import Input, Convolution2D, MaxPooling2D, UpSampling2D, 6 | Dropout 7 | from keras.models import Model 8 | from keras import backend as K 9 | from keras.callbacks import ModelCheckpoint 10 | smooth = 1. 11 | 12 | # define a weighted binary cross entropy function 13 | def binary_crossentropy_2d_w(alpha): 14 | def loss(y_true, y_pred): 15 | bce = K.binary_crossentropy(y_pred, y_true) 16 | bce *= 1 + alpha * y_true 17 | bce /= alpha 18 | return K.mean(K.batch_flatten(bce), axis=-1) 19 | return loss 20 | 21 | # define dice score to assess predictions 22 | def dice_coef(y_true, y_pred): 23 | y_true_f = K.flatten(y_true) 24 | y_pred_f = K.flatten(y_pred) 25 | intersection = K.sum(y_true_f * y_pred_f) 26 | return (2. * intersection + smooth) / (K.sum(y_true_f) + 27 | K.sum(y_pred_f) + smooth) 28 | 29 | def dice_coef_loss(y_true, y_pred): 30 | return 1 - dice_coef(y_true, y_pred) 31 | 32 | def load_data(dir, boundary=False): 33 | X = [] 34 | y = [] 35 | # load images 36 | for f in sorted(glob.glob(dir + '/image??.png')): 37 | img = np.array(Image.open(f).convert('RGB')) 38 | X.append(img) 39 | # load masks 40 | for i, f in enumerate(sorted(glob.glob(dir + '/image??_mask.txt'))): 41 | if boundary: 42 | a = get_boundary_mask(f) 43 | y.append(np.expand_dims(a, axis=0)) 44 | else: 45 | content = open(f).read().split('\n')[1:-1] 46 | a = np.array(content, 'i').reshape(X[i].shape[:2]) 47 | a = np.clip(a, 0, 1).astype('uint8') 48 | y.append(np.expand_dims(a, axis=0)) 49 | # stack data 50 | X = np.array(X) / 255. 51 | y = np.array(y) 52 | X = np.transpose(X, (0, 3, 1, 2)) 53 | return X, y 54 | 55 | # define the network model 56 | def net_2_outputs(input_shape): 57 | input_img = Input(input_shape, name='input') 58 | x = Convolution2D(8, 3, 3, activation='relu', 59 | border_mode='same')(input_img) 60 | x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x) 61 | x = Convolution2D(8, 3, 3, subsample=(1, 1), activation='relu', 62 | border_mode='same')(x) 63 | x = MaxPooling2D((2, 2), border_mode='same')(x) 64 | x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x) 65 | x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x) 66 | x = Convolution2D(16, 3, 3, subsample=(1, 1), activation='relu', 67 | border_mode='same')(x) 68 | x = MaxPooling2D((2, 2), border_mode='same')(x) 69 | x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(x) 70 | x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(x) 71 | x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(x) 72 | # up 73 | x = UpSampling2D((2, 2))(x) 74 | x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x) 75 | x = UpSampling2D((2, 2))(x) 76 | x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x) 77 | output = Convolution2D(1, 3, 3, activation='sigmoid', 78 | border_mode='same', name='output')(x) 79 | model = Model(input_img, output=[output]) 80 | model.compile(optimizer='adam', loss={'output': 81 | binary_crossentropy_2d_w(5)}) 82 | return model 83 | 84 | def train(): 85 | X, y = load_data(DATA_DIR_TRAIN.replace('c_type', c_type), 86 | boundary=False) # load the data 87 | print(X.shape, y.shape) # make sure its the right shape 88 | h = X.shape[2] 89 | w = X.shape[3] 90 | training_data = ShuffleBatchGenerator(input_data={'input': X}, 91 | output_data={'output': y, 'output_b': y_b}) # generate batches for 92 | training and testing 93 | training_data_aug = DataAugmentation(training_data, 94 | inplace_transfo=['mirror', 'transpose']) # apply some data 95 | augmentation 96 | net = net_2_outputs((X.shape[1], h, w)) 97 | net.summary() 98 | model = net 99 | model.fit(training_data_aug, 300, 1, callbacks=[ProgressBarCallback()]) 100 | net.save('model.hdf5' ) 101 | 102 | # save predictions to disk 103 | res = model.predict(training_data, training_data.nb_elements) 104 | if not os.path.isdir('res'): 105 | os.makedirs('res') 106 | for i, img in enumerate(res[0]): 107 | Image.fromarray(np.squeeze(img) * 108 | 255).convert('RGB').save('res/%s_res_%02d.jpg' % (c_type, i +1)) 109 | for i, img in enumerate(res[1]): 110 | Image.fromarray(np.squeeze(img) * 111 | 255).convert('RGB').save('res/%s_res_b%02d.jpg' % (c_type, i + 1)) 112 | 113 | 114 | if __name__ == '__main__': 115 | train() -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2018 Armando Vieira and Bernardete Ribeiro 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Introduction to Deep Learning Business Applications for Developers*](https://www.apress.com/9781484234525) by Armando Vieira and Bernardete Ribeiro (Apress, 2018). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484234525.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. -------------------------------------------------------------------------------- /errata.md: -------------------------------------------------------------------------------- 1 | # Errata for *Book Title* 2 | 3 | On **page xx** [Summary of error]: 4 | 5 | Details of error here. Highlight key pieces in **bold**. 6 | 7 | *** 8 | 9 | On **page xx** [Summary of error]: 10 | 11 | Details of error here. Highlight key pieces in **bold**. 12 | 13 | *** -------------------------------------------------------------------------------- /example1.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import Dense 3 | import numpy as np 4 | # load a dataset 5 | dataset = np.loadtxt("xxx.csv", delimiter=",") 6 | # split into input (X) and output (Y) variables 7 | X = dataset[:,0:X_dim] 8 | Y = dataset[:,X_dim] 9 | # create model 10 | model = Sequential() 11 | model.add(Dense(12, input_dim=X_dim, init='uniform', activation='relu')) 12 | model.add(Dense(5, init='uniform', activation='relu')) 13 | model.add(Dense(1, init='uniform', activation='sigmoid')) 14 | # Compile model 15 | model.compile(loss='binary_crossentropy', optimizer='adam', 16 | metrics=['accuracy']) 17 | # Fit the model 18 | model.fit(X, Y, epochs=150, batch_size=10, verbose=2) 19 | # calculate predictions 20 | predictions = model.predict(X) 21 | # round predictions 22 | rounded = [round(x[0]) for x in predictions] 23 | print(rounded) -------------------------------------------------------------------------------- /sequence_2_sequence.py: -------------------------------------------------------------------------------- 1 | from keras.models import Model 2 | from keras.layers import Input, LSTM, Dense 3 | 4 | encoder_inputs = Input(shape=(None, num_encoder_tokens)) 5 | encoder = LSTM(latent_dim, return_state=True) 6 | encoder_outputs, state_h, state_c = encoder(encoder_inputs) 7 | # We discard ‘encoder_outputs‘ and only keep the states. 8 | encoder_states = [state_h, state_c] 9 | # Set up the decoder, using ‘encoder_states‘ as initial state. 10 | decoder_inputs = Input(shape=(None, num_decoder_tokens)) 11 | # We set up our decoder to return full output sequences, 12 | # and to return internal states as well. We don't use the 13 | # return states in the training model, but we will use them in inference. 14 | decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) 15 | decoder_outputs, _, _ = decoder_lstm(decoder_inputs, 16 | initial_state=encoder_states) 17 | decoder_dense = Dense(num_decoder_tokens, activation='softmax') 18 | decoder_outputs = decoder_dense(decoder_outputs) 19 | 20 | 21 | # Define the model that will turn 22 | # ‘encoder_input_data‘ & ‘decoder_input_data‘ into ‘decoder_target_data‘ 23 | model = Model([encoder_inputs, decoder_inputs], decoder_outputs) 24 | # Run training 25 | model.compile(optimizer='rmsprop', loss='categorical_crossentropy') 26 | model.fit([encoder_input_data, decoder_input_data], decoder_target_data, 27 | batch_size=batch_size, epochs=epochs, validation_split=0.2) 28 | 29 | encoder_model = Model(encoder_inputs, encoder_states) 30 | decoder_state_input_h = Input(shape=(latent_dim,)) 31 | decoder_state_input_c = Input(shape=(latent_dim,)) 32 | decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c] 33 | decoder_outputs, state_h, state_c = decoder_lstm( 34 | decoder_inputs, initial_state=decoder_states_inputs) 35 | decoder_states = [state_h, state_c] 36 | decoder_outputs = decoder_dense(decoder_outputs) 37 | decoder_model = Model( 38 | [decoder_inputs] + decoder_states_inputs, 39 | [decoder_outputs] + decoder_states) 40 | 41 | def decode_sequence(input_seq): 42 | # Encode the input as state vectors. 43 | states_value = encoder_model.predict(input_seq) 44 | # Generate empty target sequence of length 1. 45 | target_seq = np.zeros((1, 1, num_decoder_tokens)) 46 | # Populate the first character of target sequence with the start 47 | character. 48 | target_seq[0, 0, target_token_index['\t']] = 1. 49 | # Sampling loop for a batch of sequences 50 | # (to simplify, here we assume a batch of size 1). 51 | stop_condition = False 52 | decoded_sentence = '' 53 | while not stop_condition: 54 | output_tokens, h, c = decoder_model.predict( 55 | [target_seq] + states_value) 56 | # Sample a token 57 | sampled_token_index = np.argmax(output_tokens[0, -1, :]) 58 | sampled_char = reverse_target_char_index[sampled_token_index] 59 | decoded_sentence += sampled_char 60 | # Exit condition: either hit max length 61 | # or find stop character. 62 | if (sampled_char == '\n' or len(decoded_sentence) > max_decoder_seq_length): 63 | stop_condition = True 64 | # Update the target sequence (of length 1). 65 | target_seq = np.zeros((1, 1, num_decoder_tokens)) 66 | target_seq[0, 0, sampled_token_index] = 1. 67 | # Update states 68 | states_value = [h, c] 69 | return decoded_sentence -------------------------------------------------------------------------------- /wide_and_deep.py: -------------------------------------------------------------------------------- 1 | # to run : python wide_and_deep.py --method method 2 | # example: python wide_and_deep.py --method deep 3 | import numpy as np 4 | import pandas as pd 5 | import argparse 6 | from sklearn.preprocessing import StandardScaler 7 | from copy import copy 8 | 9 | from keras.models import Sequential 10 | from keras.layers import Dense 11 | from keras.optimizers import Adam 12 | from keras.layers import Input, concatenate, Embedding, Reshape, Merge, 13 | Flatten, merge, Lambda 14 | from keras.layers.normalization import BatchNormalization 15 | from keras.models import Model 16 | from keras.regularizers import l2, l1_l2 17 | 18 | def cross_columns(x_cols): 19 | """simple helper to build the crossed columns in a pandas dataframe 20 | """ 21 | crossed_columns = dict() 22 | colnames = ['_'.join(x_c) for x_c in x_cols] 23 | for cname,x_c in zip(colnames,x_cols): 24 | crossed_columns[cname] = x_c 25 | return crossed_columns 26 | 27 | def val2idx(DF_deep,cols): 28 | """helper to index categorical columns before embeddings. 29 | """ 30 | DF_deep = pd.concat([df_train, df_test]) 31 | val_types = dict() 32 | for c in cols: 33 | val_types[c] = DF_deep[c].unique() 34 | val_to_idx = dict() 35 | for k, v in val_types.items(): 36 | val_to_idx[k] = {o: i for i, o in enumerate(val_types[k])} 37 | for k, v in val_to_idx.items(): 38 | DF_deep[k] = DF_deep[k].apply(lambda x: v[x]) 39 | unique_vals = dict() 40 | for c in cols: 41 | unique_vals[c] = DF_deep[c].nunique() 42 | return DF_deep,unique_vals 43 | 44 | def embedding_input(name, n_in, n_out, reg): 45 | inp = Input(shape=(1,), dtype='int64',name=name) 46 | return inp, Embedding(n_in, n_out, input_length=1, 47 | embeddings_regularizer=l2(reg))(inp) 48 | 49 | def continous_input(name): 50 | inp = Input(shape=(1,), name=name) 51 | return inp, Reshape((1, 1))(inp) 52 | 53 | def wide(): 54 | target = 'cr' 55 | wide_cols = ["gender", "xyz_campaign_id", "fb_campaign_id", "age", 56 | "interest"] 57 | x_cols = (['gender', 'age'],['age', 'interest']) 58 | DF_wide = pd.concat([df_train,df_test]) 59 | crossed_columns_d = cross_columns(x_cols) 60 | categorical_columns =list(DF_wide.select_dtypes(include=['object']).columns) 61 | wide_columns = wide_cols + crossed_columns_d.keys() 62 | 63 | for k, v in crossed_columns_d.iteritems(): 64 | DF_wide[k] = DF_wide[v].apply(lambda x: '-'.join(x), axis=1) 65 | DF_wide = DF_wide[wide_columns + [target] + ['IS_TRAIN']] 66 | dummy_cols = [ 67 | c for c in wide_columns if c in categorical_columns + 68 | crossed_columns_d.keys()] 69 | DF_wide = pd.get_dummies(DF_wide, columns=[x for x in dummy_cols]) 70 | train = DF_wide[DF_wide.IS_TRAIN == 1].drop('IS_TRAIN', axis=1) 71 | test = DF_wide[DF_wide.IS_TRAIN == 0].drop('IS_TRAIN', axis=1) 72 | 73 | # sanity check: make sure all columns are in the same order 74 | cols = ['cr'] + [c for c in train.columns if c != 'cr'] 75 | train = train[cols] 76 | test = test[cols] 77 | X_train = train.values[:, 1:] 78 | Y_train = train.values[:, 0] 79 | X_test = test.values[:, 1:] 80 | Y_test = test.values[:, 0] 81 | 82 | # WIDE MODEL 83 | wide_inp = Input(shape=(X_train.shape[1],), dtype='float32', 84 | name='wide_inp') 85 | w = Dense(1, activation="sigmoid", name = "wide_model")(wide_inp) 86 | wide = Model(wide_inp, w) 87 | wide.compile(Adam(0.01), loss='mse', metrics=['accuracy']) 88 | wide.fit(X_train,Y_train,nb_epoch=10,batch_size=64) 89 | results = wide.evaluate(X_test,Y_test) 90 | print "\n Results with wide model: %.3f" % results[1] 91 | 92 | 93 | def deep(): 94 | DF_deep = pd.concat([df_train,df_test]) 95 | target = 'cr' 96 | embedding_cols = ["gender", "xyz_campaign_id", "fb_campaign_id", 97 | "age", "interest"] 98 | deep_cols = embedding_cols + ['cpc','cpco','cpcoa'] 99 | DF_deep,unique_vals = val2idx(DF_deep, embedding_cols) 100 | train = DF_deep[DF_deep.IS_TRAIN == 1].drop('IS_TRAIN', axis=1) 101 | test = DF_deep[DF_deep.IS_TRAIN == 0].drop('IS_TRAIN', axis=1) 102 | n_factors = 5 103 | gender, gd = embedding_input('gender_in', unique_vals[ 104 | 'gender'], n_factors, 1e-3) 105 | xyz_campaign, xyz = embedding_input('xyz_campaign_id_in', unique_vals[ 106 | 'xyz_campaign_id'], n_factors, 1e-3) 107 | fb_campaign_id, fb = embedding_input('fb_campaign_id_in', unique_vals[ 108 | 'fb_campaign_id'], n_factors, 1e-3) 109 | age, ag = embedding_input('age_in', unique_vals[ 110 | 'age'], n_factors, 1e-3) 111 | interest, it = embedding_input('interest_in', unique_vals[ 112 | 'interest'], n_factors, 1e-3) 113 | # adding numerical columns to the deep model 114 | cpco, cp = continous_input('cpco_in') 115 | cpcoa, cpa = continous_input('cpcoa_in') 116 | X_train = [train[c] for c in deep_cols] 117 | Y_train = train[target] 118 | X_test = [test[c] for c in deep_cols] 119 | Y_test = test[target] 120 | # DEEP MODEL: input same order than in deep_cols: 121 | d = merge([gd, re, xyz, fb, ag, it], mode='concat') 122 | d = Flatten()(d) 123 | # layer to normalise continous columns with the embeddings 124 | d = BatchNormalization()(d) 125 | d = Dense(100, activation='relu', kernel_regularizer=l1_l2(l1=0.01, l2=0.01))(d) 126 | d = Dense(50, activation='relu',name='deep_inp')(d) 127 | d = Dense(1, activation="sigmoid")(d) 128 | deep = Model([gender, xyz_campaign, fb_campaign_id, age, interest, 129 | cpco, cpcoa], d) 130 | deep.compile(Adam(0.001), loss='mse', metrics=['accuracy']) 131 | deep.fit(X_train,Y_train, batch_size=64, nb_epoch=10) 132 | results = deep.evaluate(X_test,Y_test) 133 | print "\n Results with deep model: %.3f" % results[1] 134 | 135 | 136 | 137 | def wide_deep(): 138 | target = 'cr' 139 | wide_cols = ["gender", "xyz_campaign_id", "fb_campaign_id", "age", 140 | "interest"] 141 | x_cols = (['gender', 'xyz_campaign'],['age', 'interest']) 142 | DF_wide = pd.concat([df_train,df_test]) 143 | crossed_columns_d = cross_columns(x_cols) 144 | categorical_columns = 145 | list(DF_wide.select_dtypes(include=['object']).columns) 146 | wide_columns = wide_cols + crossed_columns_d.keys() 147 | for k, v in crossed_columns_d.items(): 148 | DF_wide[k] = DF_wide[v].apply(lambda x: '-'.join(x), axis=1) 149 | DF_wide = DF_wide[wide_columns + [target] + ['IS_TRAIN']] 150 | dummy_cols = [ 151 | c for c in wide_columns if c in categorical_columns + 152 | crossed_columns_d.keys()] 153 | DF_wide = pd.get_dummies(DF_wide, columns=[x for x in dummy_cols]) 154 | train = DF_wide[DF_wide.IS_TRAIN == 1].drop('IS_TRAIN', axis=1) 155 | test = DF_wide[DF_wide.IS_TRAIN == 0].drop('IS_TRAIN', axis=1) 156 | # sanity check: make sure all columns are in the same order 157 | cols = ['cr'] + [c for c in train.columns if c != 'cr'] 158 | train = train[cols] 159 | test = test[cols] 160 | X_train_wide = train.values[:, 1:] 161 | Y_train_wide = train.values[:, 0] 162 | X_test_wide = test.values[:, 1:] 163 | DF_deep = pd.concat([df_train,df_test]) 164 | embedding_cols = ['gender', 'xyz_campaign','fb_campaign_id', 'age', 165 | 'interest'] 166 | deep_cols = embedding_cols + ['cpco','cpcoa'] 167 | DF_deep,unique_vals = val2idx(DF_deep,embedding_cols) 168 | train = DF_deep[DF_deep.IS_TRAIN == 1].drop('IS_TRAIN', axis=1) 169 | test = DF_deep[DF_deep.IS_TRAIN == 0].drop('IS_TRAIN', axis=1) 170 | n_factors = 5 171 | gender, gd = embedding_input('gender_in', unique_vals[ 172 | 'gender'], n_factors, 1e-3) 173 | xyz_campaign, xyz = embedding_input('xyz_campaign_id_in', unique_vals[ 174 | 'xyz_campaign_id'], n_factors, 1e-3) 175 | fb_campaign_id, fb = embedding_input('fb_campaign_id_in', unique_vals[ 176 | 'fb_campaign_id'], n_factors, 1e-3) 177 | age, ag = embedding_input('age_in', unique_vals[ 178 | 'age'], n_factors, 1e-3) 179 | interest, it = embedding_input('interest_in', unique_vals[ 180 | 'interest'], n_factors, 1e-3) 181 | # adding numerical columns to the deep model 182 | cpco, cp = continous_input('cpco_in') 183 | cpcoa, cpa = continous_input('cpcoa_in') 184 | X_train_deep = [train[c] for c in deep_cols] 185 | Y_train_deep = train[target] 186 | X_test_deep = [test[c] for c in deep_cols] 187 | Y_test_deep = test[target] 188 | X_tr_wd = [X_train_wide] + X_train_deep 189 | Y_tr_wd = Y_train_deep # wide or deep is the same here 190 | X_te_wd = [X_test_wide] + X_test_deep 191 | Y_te_wd = Y_test_deep # wide or deep is the same here 192 | #WIDE 193 | wide_inp = Input(shape=(X_train_wide.shape[1],), dtype='float32', 194 | name='wide_inp') 195 | #DEEP 196 | deep_inp = merge([ge, xyz, ag, fb, it, cp, cpa], mode='concat') 197 | deep_inp = Flatten()(deep_inp) 198 | # layer to normalise continous columns with the embeddings 199 | deep_inp = BatchNormalization()(deep_inp) 200 | deep_inp = Dense(100, activation='relu', 201 | kernel_regularizer=l1_l2(l1=0.01, l2=0.01))(deep_inp) 202 | deep_inp = Dense(50, activation='relu',name='deep_inp')(deep_inp) 203 | #WIDE + DEEP 204 | wide_deep_inp = concatenate([wide_inp, deep_inp]) 205 | wide_deep_out = Dense(1, activation='sigmoid', 206 | name='wide_deep_out')(wide_deep_inp) 207 | wide_deep = Model(inputs=[wide_inp, gender, age, xyz_campaign, 208 | fb_campaign_id,cpco, cpcoa], 209 | outputs=wide_deep_out) 210 | wide_deep.compile(optimizer=Adam(lr=0.001),loss='mse', 211 | metrics=['accuracy']) 212 | wide_deep.fit(X_tr_wd, Y_tr_wd, nb_epoch=50, batch_size=80) 213 | # wide_deep.optimizer.lr = 0.001 214 | # wide_deep.fit(X_tr_wd, Y_tr_wd, nb_epoch=5, batch_size=64) 215 | results = wide_deep.evaluate(X_te_wd, Y_te_wd) 216 | print "\n Results with wide and deep model: %.3f" % results[1] 217 | 218 | 219 | if __name__ == '__main__': 220 | ap = argparse.ArgumentParser() 221 | ap.add_argument("--method", type=str, default="wide_deep", 222 | help="fitting method") 223 | args = vars(ap.parse_args()) 224 | method = args["method"] 225 | df_train = pd.read_csv("train.csv") 226 | df_test = pd.read_csv("test.csv") 227 | df_train['IS_TRAIN'] = 1 228 | df_test['IS_TRAIN'] = 0 229 | if method == 'wide': 230 | wide() 231 | elif method == 'deep': 232 | deep() 233 | else: 234 | wide_deep() --------------------------------------------------------------------------------