简体   繁体   中英

Using TensorFlow pre-processing (tf.feature_column) in combination with scikit-learn model

For my recent work I have to try different ML models on my given non-linear problem. I used TensorFlow and Keras to build a working version of logistic regression and of a neural network. Now I have to build an SVM classifier for the same problem. Since I could not find a working SVM estimator in TensorFlow, I was thinking of using scikit-learn instead. In my first two models I used tf.feature_column for pre-processing my data (bucketized_column, embedding_column, crossed_column etc.). Since this pre-processing is somehow complex and works pretty well, I was wondering if I can use the TensorFlow pre-processing in combination with scikit-learn.

Is this somehow possible? Or could I use scikit-learn somehow inside TensorFlow (similar like Keras), so I can also use TensorBoard for analysing my results?

Here an overview of the relevant parts in my code:

(feature_columns, train_ds, val_ds, test_ds) = preprocessing.getPreProcessedData(args.data, args.zip, args.batchSize)

In the model module I used this function call to get the pre-processed data. feature_columns is an array of the different types of the mappings of tf.feature_column. train_ds etc. are TensorFlow datasets.

Creating DenseFeature with the help of array feature_columns :

feature_layer = tf.keras.layers.DenseFeatures(feature_columns, trainable=False)

Usage of pre-processing to build Keras model:

    model = tf.keras.models.Sequential([
        feature_layer,
        tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
    ])

Fitting of the model:

    model.fit(train_ds,
              validation_data=val_ds,
              epochs=args.epoch,
              callbacks=[tensorboard_callback])

I could resolve my problem. I wrote a new function (feature_columns, train, val, test) = preprocessing.getPreProcessedDataframes(args.data, args.zip, args.batchSize) which returns panda dataframes instead of TensorFlow datasets. Afterwards I used the feature_layer to apply the pre-processing on the dataframes which act as training input for the sklearn classifier.

Applying the pre-processing on the dataframes:

x_train = feature_layer(dict(train)).numpy()
x_test = feature_layer(dict(test)).numpy()

Training:

model.fit(x_train, y_train)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM