␨␨ 2023-01-02 06:19 采纳率: 100%
浏览 136
已结题

如何在进行数据训练是报KeyError: 'val_mape?

在进行数据训练是报KeyError: 'val_mape',太困了挺不住了本人晓白,出来求助一下
代码:

# coding=utf-8
from __future__ import print_function, absolute_import
from gm.api import *
from sklearn.preprocessing import StandardScaler
from collections import deque
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from tensorflow.keras.callbacks import ModelCheckpoint
import numpy as np



# 可以直接提取数据,掘金终端需要打开,接口取数是通过网络请求的方式,效率一般,行情数据可通过subscribe订阅方式
# 设置token, 查看已有token ID,在用户-密钥管理里获取
set_token('3e3c1dfdd48923125246456f3c6b274d738e12c0')
data = history(symbol='SHSE.600000', frequency='1d', start_time='2001-01-01 09:00:00', end_time='2021-12-31 16:00:00',
               fields='open,high,low,close,volume', adjust=ADJUST_PREV, adjust_end_time='2020-12-31', df=True)
pre_days = 20
mem_his_days = 10  # 记忆天数


def stock_price_data_precesing(data, mem_his_days, pre_days):
    data.dropna(inplace=True)
    data.sort_index(inplace=True)
    data['label'] = data['close'].shift(-pre_days)

    # 归一化
    scaler = StandardScaler()
    sca_x = scaler.fit_transform(data.iloc[:, :-1])

    # 数据集
    deq = deque(maxlen=mem_his_days)

    x = []
    for i in sca_x:
        deq.append(list(i))
        if len(deq) == mem_his_days:
            x.append(list(deq))

    x_lately = x[-pre_days:]
    x = x[:-pre_days]

    y = data[['label']].values[mem_his_days - 1:-pre_days]

    x = np.array(x)
    y = np.array(y)
    return x, y, x_lately


pre_days = 10
mem_days = [5, 10, 15]
lstm_layers = [1, 2, 3]
dense_layers = [1, 2, 3]
units = [8, 16, 32]

for them_mem_days in mem_days:
    for the_lstm_layers in lstm_layers:
        for the_dense_layers in dense_layers:
            for the_units in units:
                filepath = '.models/_{epoch:02d}-accuracy{val_mape:.2f}'+f'men_{the_lstm_layers}_lstm_{the_lstm_layers}_dense_{the_dense_layers}_unit_{the_units}'
                checkpoint = ModelCheckpoint(
                    filepath=filepath,
                    save_weights_only=False,
                    monitor='val_mape',
                    mode='min',
                    save_bast_only=True)
                x, y, x_lately = stock_price_data_precesing(data, them_mem_days, pre_days)
                x_train, x_test, y_train, y_test = train_test_split(x, y, shuffle=False, test_size=0.1)

                model = Sequential()
                model.add(LSTM(the_units, input_shape=x.shape[1:], activation='relu', return_sequences=True))
                model.add(Dropout(0.1))

                model.add(LSTM(the_units, activation='relu', return_sequences=True))
                model.add(Dropout(0.1))

                for i in range(the_lstm_layers):
                    model.add(LSTM(the_units, activation='relu', return_sequences=True))
                    model.add(Dropout(0.1))

                for i in range(the_dense_layers):
                    model.add(LSTM(the_units, activation='relu'))
                    model.add(Dropout(0.1))

                model.add(Dense(1))

                model.compile(optimizer='adam',
                              loss='mse',
                              metrics=['mape'])

                model.fit(x_train, y_train, batch_size=32, epochs=50, validation_data=(x_test, y_test),
                          callbacks=[checkpoint])


报错:
D:\ruanjian\Anaconda\envs\tf2\python.exe D:\掘金数据\策略保存位\30699f03-84d3-11ed-9961-00e04c185fa8\main.py
2023-01-02 06:10:22.835852: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
WARNING:tensorflow:From D:\ruanjian\Anaconda\envs\tf2\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1630: calling BaseResourceVariable.init (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
WARNING:tensorflow:From D:\ruanjian\Anaconda\envs\tf2\lib\site-packages\tensorflow_core\python\ops\math_grad.py:1424: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Train on 3614 samples, validate on 402 samples
2023-01-02 06:10:30.857621: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2023-01-02 06:10:30.977273: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: GeForce GTX 960 major: 5 minor: 2 memoryClockRate(GHz): 1.1775
pciBusID: 0000:01:00.0
2023-01-02 06:10:30.977457: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2023-01-02 06:10:30.981851: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
2023-01-02 06:10:30.984883: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_100.dll
2023-01-02 06:10:30.986238: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_100.dll
2023-01-02 06:10:30.990216: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_100.dll
2023-01-02 06:10:30.992991: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_100.dll
2023-01-02 06:10:31.000930: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2023-01-02 06:10:31.003259: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2023-01-02 06:10:31.003648: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2023-01-02 06:10:31.005895: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: GeForce GTX 960 major: 5 minor: 2 memoryClockRate(GHz): 1.1775
pciBusID: 0000:01:00.0
2023-01-02 06:10:31.006080: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2023-01-02 06:10:31.006203: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
2023-01-02 06:10:31.006323: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_100.dll
2023-01-02 06:10:31.006446: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_100.dll
2023-01-02 06:10:31.006569: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_100.dll
2023-01-02 06:10:31.007039: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_100.dll
2023-01-02 06:10:31.007157: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2023-01-02 06:10:31.009061: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2023-01-02 06:10:31.587549: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2023-01-02 06:10:31.587684: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0
2023-01-02 06:10:31.587762: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N
2023-01-02 06:10:31.590186: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3024 MB memory) -> physical GPU (device: 0, name: GeForce GTX 960, pci bus id: 0000:01:00.0, compute capability: 5.2)
Epoch 1/50
2023-01-02 06:10:34.187451: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll

32/3614 [..............................] - ETA: 3:48 - loss: 43.9954 - mean_absolute_percentage_error: 100.0255
96/3614 [..............................] - ETA: 1:17 - loss: 62.0005 - mean_absolute_percentage_error: 99.9741
160/3614 [>.............................] - ETA: 46s - loss: 56.4942 - mean_absolute_percentage_error: 99.9165
224/3614 [>.............................] - ETA: 33s - loss: 54.9412 - mean_absolute_percentage_error: 99.8509
288/3614 [=>............................] - ETA: 26s - loss: 57.8558 - mean_absolute_percentage_error: 99.8001
352/3614 [=>............................] - ETA: 22s - loss: 56.4364 - mean_absolute_percentage_error: 99.7402
416/3614 [==>...........................] - ETA: 18s - loss: 56.3979 - mean_absolute_percentage_error: 99.6828
480/3614 [==>...........................] - ETA: 16s - loss: 56.1671 - mean_absolute_percentage_error: 99.6292
544/3614 [===>..........................] - ETA: 14s - loss: 56.9586 - mean_absolute_percentage_error: 99.5617
608/3614 [====>.........................] - ETA: 13s - loss: 55.7152 - mean_absolute_percentage_error: 99.4744
672/3614 [====>.........................] - ETA: 12s - loss: 55.8940 - mean_absolute_percentage_error: 99.4243
736/3614 [=====>........................] - ETA: 11s - loss: 56.2712 - mean_absolute_percentage_error: 99.3818
800/3614 [=====>........................] - ETA: 10s - loss: 56.5548 - mean_absolute_percentage_error: 99.3035
864/3614 [======>.......................] - ETA: 9s - loss: 56.3818 - mean_absolute_percentage_error: 99.2028
928/3614 [======>.......................] - ETA: 8s - loss: 55.5512 - mean_absolute_percentage_error: 99.0821
992/3614 [=======>......................] - ETA: 8s - loss: 55.8481 - mean_absolute_percentage_error: 99.0175
1056/3614 [=======>......................] - ETA: 7s - loss: 55.7318 - mean_absolute_percentage_error: 98.9219
1120/3614 [========>.....................] - ETA: 7s - loss: 55.3857 - mean_absolute_percentage_error: 98.8261
1184/3614 [========>.....................] - ETA: 6s - loss: 54.8773 - mean_absolute_percentage_error: 98.6873
1248/3614 [=========>....................] - ETA: 6s - loss: 54.9429 - mean_absolute_percentage_error: 98.6143
1312/3614 [=========>....................] - ETA: 6s - loss: 55.0219 - mean_absolute_percentage_error: 98.5439
1376/3614 [==========>...................] - ETA: 5s - loss: 54.8453 - mean_absolute_percentage_error: 98.4303
1440/3614 [==========>...................] - ETA: 5s - loss: 54.5833 - mean_absolute_percentage_error: 98.3059
1504/3614 [===========>..................] - ETA: 5s - loss: 54.2378 - mean_absolute_percentage_error: 98.1606
1568/3614 [============>.................] - ETA: 4s - loss: 54.4197 - mean_absolute_percentage_error: 98.0319
1632/3614 [============>.................] - ETA: 4s - loss: 54.5155 - mean_absolute_percentage_error: 97.9193
1696/3614 [=============>................] - ETA: 4s - loss: 54.1869 - mean_absolute_percentage_error: 97.7990
1760/3614 [=============>................] - ETA: 4s - loss: 53.9778 - mean_absolute_percentage_error: 97.6215
1824/3614 [==============>...............] - ETA: 3s - loss: 53.7663 - mean_absolute_percentage_error: 97.4412
1888/3614 [==============>...............] - ETA: 3s - loss: 53.6402 - mean_absolute_percentage_error: 97.2901
1952/3614 [===============>..............] - ETA: 3s - loss: 53.2371 - mean_absolute_percentage_error: 97.0918
2016/3614 [===============>..............] - ETA: 3s - loss: 53.2523 - mean_absolute_percentage_error: 96.8978
2080/3614 [================>.............] - ETA: 3s - loss: 53.1866 - mean_absolute_percentage_error: 96.6726
2144/3614 [================>.............] - ETA: 2s - loss: 53.1390 - mean_absolute_percentage_error: 96.4069
2208/3614 [=================>............] - ETA: 2s - loss: 52.7757 - mean_absolute_percentage_error: 95.9891
2272/3614 [=================>............] - ETA: 2s - loss: 52.6894 - mean_absolute_percentage_error: 95.6743
2336/3614 [==================>...........] - ETA: 2s - loss: 52.4167 - mean_absolute_percentage_error: 95.2560
2400/3614 [==================>...........] - ETA: 2s - loss: 52.1452 - mean_absolute_percentage_error: 94.9096
2464/3614 [===================>..........] - ETA: 2s - loss: 51.9539 - mean_absolute_percentage_error: 94.4691
2528/3614 [===================>..........] - ETA: 2s - loss: 51.7230 - mean_absolute_percentage_error: 94.1516
2592/3614 [====================>.........] - ETA: 1s - loss: 51.6239 - mean_absolute_percentage_error: 93.7857
2656/3614 [=====================>........] - ETA: 1s - loss: 51.2992 - mean_absolute_percentage_error: 93.4370
2720/3614 [=====================>........] - ETA: 1s - loss: 51.1128 - mean_absolute_percentage_error: 93.2620
2784/3614 [======================>.......] - ETA: 1s - loss: 50.6716 - mean_absolute_percentage_error: 92.9862
2848/3614 [======================>.......] - ETA: 1s - loss: 50.5519 - mean_absolute_percentage_error: 92.6520
2912/3614 [=======================>......] - ETA: 1s - loss: 50.3740 - mean_absolute_percentage_error: 92.5415
2976/3614 [=======================>......] - ETA: 1s - loss: 49.9548 - mean_absolute_percentage_error: 92.2648
3040/3614 [========================>.....] - ETA: 1s - loss: 49.5896 - mean_absolute_percentage_error: 92.1709
3104/3614 [========================>.....] - ETA: 0s - loss: 49.1311 - mean_absolute_percentage_error: 92.3879
3168/3614 [=========================>....] - ETA: 0s - loss: 48.8844 - mean_absolute_percentage_error: 92.3431
3232/3614 [=========================>....] - ETA: 0s - loss: 48.4132 - mean_absolute_percentage_error: 92.1464
3296/3614 [==========================>...] - ETA: 0s - loss: 47.9590 - mean_absolute_percentage_error: 92.0030
3360/3614 [==========================>...] - ETA: 0s - loss: 47.4387 - mean_absolute_percentage_error: 91.6889
3424/3614 [===========================>..] - ETA: 0s - loss: 46.9361 - mean_absolute_percentage_error: 91.5066
3488/3614 [===========================>..] - ETA: 0s - loss: 46.3927 - mean_absolute_percentage_error: 91.7258
3552/3614 [============================>.] - ETA: 0s - loss: 45.7963 - mean_absolute_percentage_error: 91.6464Traceback (most recent call last):
File "D:\掘金数据\策略保存位\30699f03-84d3-11ed-9961-00e04c185fa8\main.py", line 94, in
callbacks=[checkpoint])
File "D:\ruanjian\Anaconda\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 727, in fit
use_multiprocessing=use_multiprocessing)
File "D:\ruanjian\Anaconda\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py", line 675, in fit
steps_name='steps_per_epoch')
File "D:\ruanjian\Anaconda\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py", line 450, in model_iteration
callbacks.on_epoch_end(epoch, epoch_logs)
File "D:\ruanjian\Anaconda\envs\tf2\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 299, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "D:\ruanjian\Anaconda\envs\tf2\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 968, in on_epoch_end
self._save_model(epoch=epoch, logs=logs)
File "D:\ruanjian\Anaconda\envs\tf2\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 987, in _save_model
filepath = self._get_file_path(epoch, logs)
File "D:\ruanjian\Anaconda\envs\tf2\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 1024, in _get_file_path
return self.filepath.format(epoch=epoch + 1, **logs)
KeyError: 'val_mape'

进程已结束,退出代码1

  • 写回答

6条回答 默认 最新

  • |__WhoAmI__| 2023-01-02 16:43
    关注

    出现 KeyError: 'val_mape' 的原因可能是在代码中没有计算并输出 val_mape 这个指标。

    在训练模型时使用了 ModelCheckpoint 回调函数来保存模型权重,并使用了 'val_mape' 作为监测指标,所以在训练过程中需要计算并输出 'val_mape' 指标。

    可以使用 tensorflow.keras.metrics 模块中的 MeanAbsolutePercentageError 函数来计算这个指标:

    from tensorflow.keras.metrics import MeanAbsolutePercentageError
    
    model.compile(optimizer='adam', loss='mean_absolute_error', metrics=[MeanAbsolutePercentageError()])
    

    然后在训练模型时传入 ModelCheckpoint 回调函数:

    model.fit(x, y, epochs=10, validation_split=0.2, callbacks=[checkpoint])
    

    这样在每个 epoch 结束时,ModelCheckpoint 回调函数就会根据 'val_mape' 指标保存模型权重。
    仅供参考,望采纳,谢谢。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 1月10日
  • 已采纳回答 1月2日
  • 创建了问题 1月2日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表