import graphlab as gl
from graphlab import mxnet as mx
# Define the network symbol, equivalent to linear regression
net = mx.symbol.Variable('data')
net = mx.symbol.FullyConnected(data=net, name='fc1', num_hidden=1)
net = mx.symbol.LinearRegressionOutput(data=net, name='lr')
# Load data into SFrame and normalize features
sf = gl.SFrame.read_csv('https://static.turi.com/datasets/regression/houses.csv')
features = ['tax', 'bedroom', 'bath', 'size', 'lot']
for f in features:
sf[f] = sf[f] - sf[f].mean()
sf[f] = sf[f] / sf[f].std()
# Prepare the input iterator from SFrame
# `data_name` must match the first layer's name of the network.
# `label_name` must match the last layer's name plus "_label".
dataiter = mx.io.SFrameIter(sf, data_field=features, label_field='price',
data_name='data', label_name='lr_label',
batch_size=1)
# Train the network
model = mx.model.FeedForward.create(symbol=net, X=dataiter, num_epoch=20,
learning_rate=1e-2,
eval_metric='rmse')
# Make prediction
model.predict(dataiter)
我编写了几行代码来预测数据集中的参数,但它只为列车数据提供了RMSE,如图所示。有什么方法可以显示测试数据的RMSE? model.evaluate(dataiter)
不起作用,需要帮助
答案 0 :(得分:1)
mx.model
已被mx.module
取代,最终mx.gluon
接口是首选。使用mx.module
时,您可以使用eval_metric
指定评估指标,使用eval_data
指定评估数据(应该是DataIter)。例子类似于;
mod = mx.mod.Module(symbol=net,
context=mx.cpu(),
data_names=['data'],
label_names=['softmax_label'])
mod.fit(train_data=train_iter,
eval_data=val_iter,
eval_metric='rmse',
num_epoch=10)
这将为您提供输出中所需的指标;
INFO:root:Epoch[0] Train-RMSE=0.364625
INFO:root:Epoch[0] Time cost=0.388
INFO:root:Epoch[0] Validation-RMSE=0.557250
...