救助!'Model' object has no attribute '_metrics'
收藏
做的线性回归
import paddle
import paddle.nn.functional as F
paddle.set_default_dtype("float32")
# step1:用高层API定义数据集,无需进行数据处理等,高层API为你一条龙搞定
class MyModel(paddle.nn.Layer):
def __init__(self):
super(MyModel, self).__init__()
self.linear1 = paddle.nn.Linear(in_features=27, out_features=120)
self.linear2 = paddle.nn.Linear(in_features=120, out_features=1)
def forward(self, x):
x = self.linear1(x)
x = F.relu(x)
x = self.linear2(x)
return x
# step3:训练模型
model = paddle.Model(MyModel())
model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),
paddle.nn.MSELoss(),
metrics=paddle.metric.Precision())
# 为模型训练做准备,设置优化器及其学习率,并将网络的参数传入优化器,设置损失函数和精度计算方式
model.fit(dataset, epochs=2, batch_size=8, verbose=1)
paddle.disable_static()
model.save('moxing/test1')
输出:
The loss value printed in the log is the current step, and the metric is the average value of previous steps.
Epoch 1/2
step 6156/6156 [==============================] - loss: 0.9799 - precision: 0.0000e+00 - 2ms/step
Epoch 2/2
step 6156/6156 [==============================] - loss: 0.4947 - precision: 0.0000e+00 - 2ms/step
但是调用保存的模型后却不能正常运行model.predict()函数。
直接运行内存中保存的模型是可以正常预测的。
就很奇怪?保存和读取也就两行代码,哪里错了?
input_dir = 'C:/Users/win10/Desktop/fengdian/input/1/FD01.csv'
output_file = 'test23.json'
class MyModel(paddle.nn.Layer):
def __init__(self):
super(MyModel, self).__init__()
self.linear1 = paddle.nn.Linear(in_features=27, out_features=120)
self.linear2 = paddle.nn.Linear(in_features=120, out_features=1)
def forward(self, x):
x = self.linear1(x)
x = F.relu(x)
x = self.linear2(x)
return x
mymodel = paddle.Model(MyModel())
mymodel.load('moxing/test1')
X = pd.read_csv(input_dir, encoding='gbk')
X = X.sort_values(by='预报时刻')
X['时间'] = X['预报时刻']
X = X.drop(columns=['起报时刻', '预报时刻'])
if 'Unnamed: 0' in X.columns:
X = X.drop(columns=['Unnamed: 0'])
X = scale_op.transform(X)
X = time_extract(X)
X = scale_op.transform(X)
xx=X.values.tolist()
xxx = np.array(xx).astype('float32')
# 不同设备建议使用不同模型预测,装机容量不一致。此处使用同一模型做演示
y = mymodel.predict(xxx)
报错信息:
Predict begin...
step 670/672 [============================>.] - ETA: 0s - 729us/step
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_15608\3428503837.py in
19 # 不同设备建议使用不同模型预测,装机容量不一致。此处使用同一模型做演示
20
---> 21 y = mymodel.predict(xxx)
22 #X = self.scale_op1.transform(X)
23
D:\study_software\annoconda\envs\fd\lib\site-packages\paddle\hapi\model.py in predict(self, test_data, batch_size, num_workers, stack_outputs, verbose, callbacks)
1986 outputs = []
1987
-> 1988 logs, outputs = self._run_one_epoch(test_loader, cbks, 'predict')
1989
1990 outputs = list(zip(*outputs))
D:\study_software\annoconda\envs\fd\lib\site-packages\paddle\hapi\model.py in _run_one_epoch(self, data_loader, callbacks, mode, logs)
2134 del self.num_iters
2135 break
-> 2136 self._reset_metrics()
2137
2138 if mode == 'predict':
D:\study_software\annoconda\envs\fd\lib\site-packages\paddle\hapi\model.py in _reset_metrics(self)
2224
2225 def _reset_metrics(self):
-> 2226 for metric in self._metrics:
2227 metric.reset()
2228
AttributeError: 'Model' object has no attribute '_metrics'
0
收藏
请登录后评论
我记得好像是纬度问题,之前碰到过这种问题,但是忘记怎么解决的了
把测试集的label和text分开,这个问题可以解决