wide_and_deep_model_keras學習(有錯誤

來源:互聯網
上載者:User

標籤:lse   載入   orm   max   status   連續   concat   pandas   .data   

#coding: utf-8‘‘‘用keras寫的google Wide&&Deep model‘‘‘import pandas as pdfrom keras.models import Sequentialfrom keras.layers import Dense, mergefrom sklearn.preprocessing import MinMaxScaler#所有的資料列COLUMNS = [    "age", "workclass", "fnlwgt", "education", "education_num", "marital_status",     "occupation", "relationship", "race", "gender", "capital_gain", "capital_loss",     "hours_per_week", "native_country", "income_bracket"]#標籤列LABEL_COLUMN = "label"#類型特徵變數CATEGORICAL_COLUMNS = [    "workclass", "education", "marital_status", "occupation", "relationship",     "race", "gender", "native_country"]#連續值特徵變數CONTINUOUS_COLUMNS = [    "age", "education_num", "capital_gain", "capital_loss", "hours_per_week"]#負載檔案def load(filename):    with open(filename, ‘r‘) as f:        skiprows = 1 if ‘test‘ in filename else 0        df = pd.read_csv(            f, names=COLUMNS, skipinitialspace=True, skiprows=skiprows, engine=‘python‘        )        #預設值處理        df = df.dropna(how=‘any‘, axis=0)    return df#預先處理def preprocess(df):    df[LABEL_COLUMN] = df[‘income_bracket‘].apply(lambda x: ">50K" in x).astype(int)    df.pop("income_bracket")    y = df[LABEL_COLUMN].values    df.pop(LABEL_COLUMN)        df = pd.get_dummies(df, columns=[x for x in CATEGORICAL_COLUMNS])    # TODO: 對特徵進行選擇,使得網路更高效        # TODO: 特徵工程,比如加入交叉與組合特徵    # from sklearn.preprocessing import PolynomialFeatures    # X = PolynomialFeatures(degree=2, interaction_only=True, include_bias=False).fit_transform(X)        df = pd.DataFrame(MinMaxScaler().fit_transform(df), columns=df.columns)    X = df.values    return X, ydef main():    df_train = load(‘adult.data‘)    df_test = load(‘adult.test‘)    df = pd.concat([df_train, df_test])#拼接    train_len = len(df_train)        X, y = preprocess(df)    X_train = X[:train_len]    y_train = y[:train_len]    X_test = X[train_len:]    y_test = y[train_len:]        #Wide部分    wide = Sequential()    wide.add(Dense(1, input_dim=X_train.shape[1]))        #Deep部分    deep = Sequential()    # TODO: 添加embedding層    deep.add(Dense(input_dim=X_train.shape[1], output_dim=100, activation=‘relu‘))    #deep.add(Dense(100, activation=‘relu‘))    deep.add(Dense(input_dim=100, output_dim=32, activation=‘relu‘))    #deep.add(Dense(50, activation=‘relu‘))    deep.add(Dense(input_dim=32, output_dim=8))    deep.add(Dense(1, activation=‘sigmoid‘))        #Wide和Deep拼接 :兩邊搭出來,一拼接    model = Sequential()    model.add(merge([wide, deep], mode=‘concat‘, concat_axis=1))    model.add(Dense(1, activation=‘sigmoid‘))        #編譯模型    model.compile(        optimizer=‘rmsprop‘,        loss=‘binary_crossentropy‘,        metrics=[‘accuracy‘]    )        #模型訓練    model.fit([X_train, X_train], y_train, nb_epoch=10, batch_size=32)        #loss與準確率評估    loss, accuracy = model.evaluate([X_test, X_test], y_test)    print(‘\n‘, ‘test accuracy:‘, accuracy)    if __name__ == ‘__main__‘:    main()
#錯誤為:model.add(merge([wide, deep], mode=‘concat‘, concat_axis=1))

#TypeError: ‘module‘ object is not callable

 

wide_and_deep_model_keras學習(有錯誤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.