因CUDA错误无法通过Keras模型进行预测求助
Hey there! Let's tackle your two main issues—those confusing CUDA messages and the missing plot—plus fix a small logic bug in your prediction visualization.
一、关于CUDA相关警告的解释与处理
First off, those CUDA messages are warnings, not errors—your code is still running just fine on your CPU! Here's what's going on:
- TensorFlow checks for GPU support by default, but your machine doesn't have an NVIDIA GPU (or you haven't installed the CUDA toolkit/drivers).
- The line
Ignore above cudart dlerror if you do not have a GPU set up on your machineexplicitly tells you this is safe to ignore.
If you want to hide those warnings to clean up your output, add these lines at the very top of your code:
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
This will suppress info and warning messages from TensorFlow, leaving only errors visible.
二、为什么Plotly图表不显示?
There are a few common reasons for this, plus a small bug in your code that would have made the prediction line show incorrectly even if the plot worked:
1. Fix the Prediction Trace Bug
Looking at your code, you defined trace2 as the "Prediction" but used y = value_test—that's the actual test data, not your model's predictions! Change that line to use your prediction variable, and slice the date array to match the prediction length (since your generator uses look_back=15, predictions start 15 points into the test set):
trace2 = go.Scatter( x = date_test[look_back:], y = prediction, mode = 'lines', name = 'Prediction' )
2. Fix Plot Rendering Issues
If you're running this as a local Python script (not Jupyter):
Plotly's default renderer might not pop up automatically. Force it to open in your browser by changing fig.show() to:
fig.show(renderer="browser")
Alternatively, you can save the plot as an HTML file to open manually:
fig.write_html("strom_verbrauch_prediction.html")
If you're using Jupyter Notebook/Lab:
You need to initialize Plotly's offline mode first. Add these lines right after importing plotly:
from plotly.offline import init_notebook_mode, iplot init_notebook_mode(connected=True)
Then replace fig.show() with:
iplot(fig)
3. Bonus: Add Your 365-Day Forecast to the Plot
You generated a 365-day forecast but didn't add it to your figure. Here's how to include it for a complete visualization:
trace3 = go.Scatter( x = forecast_dates[1:], # 去掉重复的最后一个原始日期 y = forecast, mode = 'lines', name = '365-Day Forecast', line = dict(color='red', dash='dash') )
三、Final Tweaked Code Snippet
Here's the corrected part of your code with all fixes applied:
# 可选:添加CUDA警告抑制 import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # ... 你的其他导入和数据处理代码 ... # 修正预测轨迹 trace2 = go.Scatter( x = date_test[look_back:], y = prediction, mode = 'lines', name = 'Prediction' ) # 添加未来365天预测轨迹 trace3 = go.Scatter( x = forecast_dates[1:], y = forecast, mode = 'lines', name = '365-Day Forecast', line = dict(color='red', dash='dash') ) layout = go.Layout( title = 'Strom Verbrauch In Der Wasserversorgung', xaxis = {'title' : 'Datum'}, yaxis = {'title' : 'Werte'} ) fig = go.Figure(data = [trace1, trace2, trace3], layout = layout) # 根据你的环境选择显示方式 # 本地脚本: fig.show(renderer="browser") # 或者保存为HTML: # fig.write_html("strom_verbrauch.html") # Jupyter环境: # iplot(fig)
内容的提问来源于stack exchange,提问作者WhatIsLife

