├── LICENSE ├── README.md └── wide_deep_keras.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Jonathan Rahn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # keras-wide-n-deep 2 | Reimplementation of [Google's Wide & Deep Network](https://arxiv.org/abs/1606.07792) in Keras 3 | 4 | Based on a [TF Tutorial](https://www.tensorflow.org/tutorials/wide_and_deep/) and [Liu Sida's Blog Post](https://liusida.github.io/2016/10/31/translate-from-tf-2-keras/). 5 | 6 | work in progress 7 | -------------------------------------------------------------------------------- /wide_deep_keras.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | from keras.models import Sequential 3 | from keras.layers import Dense, Merge 4 | from sklearn.preprocessing import MinMaxScaler 5 | 6 | COLUMNS = [ 7 | "age", "workclass", "fnlwgt", "education", "education_num", "marital_status", 8 | "occupation", "relationship", "race", "gender", "capital_gain", "capital_loss", 9 | "hours_per_week", "native_country", "income_bracket" 10 | ] 11 | 12 | LABEL_COLUMN = "label" 13 | 14 | CATEGORICAL_COLUMNS = [ 15 | "workclass", "education", "marital_status", "occupation", "relationship", 16 | "race", "gender", "native_country" 17 | ] 18 | 19 | CONTINUOUS_COLUMNS = [ 20 | "age", "education_num", "capital_gain", "capital_loss", "hours_per_week" 21 | ] 22 | 23 | def load(filename): 24 | with open(filename, 'r') as f: 25 | skiprows = 1 if 'test' in filename else 0 26 | df = pd.read_csv( 27 | f, names=COLUMNS, skipinitialspace=True, skiprows=skiprows, engine='python' 28 | ) 29 | df = df.dropna(how='any', axis=0) 30 | return df 31 | 32 | def preprocess(df): 33 | df[LABEL_COLUMN] = df['income_bracket'].apply(lambda x: ">50K" in x).astype(int) 34 | df.pop("income_bracket") 35 | y = df[LABEL_COLUMN].values 36 | df.pop(LABEL_COLUMN) 37 | 38 | df = pd.get_dummies(df, columns=[x for x in CATEGORICAL_COLUMNS]) 39 | 40 | # TODO: select features for wide & deep parts 41 | 42 | # TODO: transformations (cross-products) 43 | # from sklearn.preprocessing import PolynomialFeatures 44 | # X = PolynomialFeatures(degree=2, interaction_only=True, include_bias=False).fit_transform(X) 45 | 46 | df = pd.DataFrame(MinMaxScaler().fit_transform(df), columns=df.columns) 47 | 48 | X = df.values 49 | return X, y 50 | 51 | def main(): 52 | df_train = load('adult.data') 53 | df_test = load('adult.test') 54 | df = pd.concat([df_train, df_test]) 55 | train_len = len(df_train) 56 | 57 | X, y = preprocess(df) 58 | X_train = X[:train_len] 59 | y_train = y[:train_len] 60 | X_test = X[train_len:] 61 | y_test = y[train_len:] 62 | 63 | wide = Sequential() 64 | wide.add(Dense(1, input_dim=X_train.shape[1])) 65 | 66 | deep = Sequential() 67 | # TODO: add embedding 68 | deep.add(Dense(input_dim=X_train.shape[1], output_dim=100, activation='relu')) 69 | deep.add(Dense(100, activation='relu')) 70 | deep.add(Dense(50, activation='relu')) 71 | deep.add(Dense(1, activation='sigmoid')) 72 | 73 | model = Sequential() 74 | model.add(Merge([wide, deep], mode='concat', concat_axis=1)) 75 | model.add(Dense(1, activation='sigmoid')) 76 | 77 | model.compile( 78 | optimizer='rmsprop', 79 | loss='binary_crossentropy', 80 | metrics=['accuracy'] 81 | ) 82 | 83 | model.fit([X_train, X_train], y_train, nb_epoch=10, batch_size=32) 84 | 85 | loss, accuracy = model.evaluate([X_test, X_test], y_test) 86 | print('\n', 'test accuracy:', accuracy) 87 | 88 | if __name__ == '__main__': 89 | main() 90 | --------------------------------------------------------------------------------