【发布时间】:2019-08-21 04:07:27
【问题描述】:
我有一个回归任务的数据。
独立特征(X_train)使用标准缩放器进行缩放。
构建了一个添加隐藏层的 Keras 顺序模型。编译模型。
然后用model.fit(X_train_scaled, y_train )拟合模型
然后我将模型保存在.hdf5 文件中。
现在如何在保存的模型中包含缩放部分, 以便可以将相同的缩放参数应用于看不见的测试数据。
#imported all the libraries for training and evaluating the model
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=42)
sc = StandardScaler()
X_train_scaled = sc.fit_transform(X_train)
X_test_scaled= sc.transform (X_test)
def build_model():
model = keras.Sequential([layers.Dense(64, activation=tf.nn.relu,input_shape=[len(train_dataset.keys())]),
layers.Dense(64, activation=tf.nn.relu),
layers.Dense(1)
])
optimizer = tf.keras.optimizers.RMSprop(0.001)
model.compile(loss='mean_squared_error',
optimizer=optimizer,
metrics=['mean_absolute_error', 'mean_squared_error'])
return model
model = build_model()
EPOCHS=1000
history = model.fit(X_train_scaled, y_train, epochs=EPOCHS,
validation_split = 0.2, verbose=0)
loss, mae, mse = model.evaluate(X_test_scaled, y_test, verbose=0)
【问题讨论】:
标签: python-3.x keras pipeline keras-layer tensorflow-serving