小编Irv*_*rez的帖子

如何处理tensorflow与sklearn中的管道

我刚刚开始研究深度学习,我正在尝试实现管道以实现更好的数据流。我有 scikit learn 背景,其中管道非常简单:

logreg = Pipeline(
[('scaler', StandardScaler()), 
 ('classifier', RandomForestClassifier(n_estimators= 50))]
Run Code Online (Sandbox Code Playgroud)

只需说明您的转换并在最后附加一个适合的模型即可。另一方面,使用 Tensorflow 的 tf.data 则要麻烦得多:

dataset = tf.data.Dataset.from_tensor_slices((X, y))

def preprocess(x, y):
    # Standard scaling
    x = tf.cast(x, tf.float32)
    mean, variance = tf.nn.moments(x, axes=[0])
    x = (x - mean) / tf.sqrt(variance)
    return x, y

batch_size = 32
dataset = dataset.map(preprocess).batch(batch_size)

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(16, activation='relu', input_shape=(8,)),
    tf.keras.layers.Dense(8, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

Run Code Online (Sandbox Code Playgroud)

现在,我的问题具体在于如何实现此管道(而不是模型),因为您应该不断向数据集添加方法以完成管道。我知道可以通过 keras 包装器将张量流模型组合到 sklearn 管道中:

from tensorflow.keras.wrappers.scikit_learn import KerasClassifier

def create_model():
    model = …
Run Code Online (Sandbox Code Playgroud)

python deep-learning tensorflow

5
推荐指数
0
解决办法
220
查看次数

标签 统计

deep-learning ×1

python ×1

tensorflow ×1