这是我使用的代码:
import math
import pandas_datareader as web
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
import matplotlib.pyplot as plt
Stock = 'BTC-USD'
#Get the stock quote
df = web.DataReader(Stock, data_source='yahoo', start='2016-01-01', end='2020-12-17')
#Show the Data
#print(df)
#Get the number of rows and columns in the data set
#print(df.shape)
#Visualize the closing price history
plt.figure(figsize=(16,8))
plt.title("Close Price History")
plt.plot(df['Close'])
plt.xlabel('Data', fontsize=18)
plt.ylabel('Close Price USD ($)', fontsize=18)
plt.show()
print(1)
#Create a new Dataframe with only the 'close column'
data = df.filter(['Close'])
#Convert the dataframe to a numpy array
dataset = data.values
#Get the number of rows to train the model on
training_data_len = math.ceil(len(dataset) * .8)
print(2)
#print(training_data_len)
#Scale the data
scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(dataset)
print(3)
#print(scaled_data)
#Create the training data set
#Create the scaled training data set
train_data = scaled_data[0:training_data_len, :]
#Split the data into x_train and y_train data sets
x_train = []
y_train = []
print(4)
for i in range(60, len(train_data)):
x_train.append(train_data[i-60:i, 0])
y_train.append(train_data[i, 0])
if i<= 61:
print(x_train)
print(y_train)
print()
print(5)
#Convert the x_train and y_train to numpy arrays
x_train, y_train = np.array(x_train), np.array(y_train)
print(6)
#Reshape the x_train data set
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
#print(x_train.shape)
print(7)
#Build the LSTM model
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(LSTM(50, return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))
print(8)
#Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')
print(9)
#Train the model
model.fit(x_train, y_train, batch_size=1, epochs=1)
print(10)
#Create the test data set
#Create a new array containing scaled values from index 1543 to 2003
test_data = scaled_data[training_data_len - 60: 2003]
#Create the data sets x_test and y_test
x_test = []
y_test = dataset[training_data_len:, :]
for i in range(60, len(test_data)):
x_test.append(test_data[i-60:i, 0])
print(11)
#Convert the data to a numpy array
x_test = np.array(x_test)
#print(x_test.shape)
#Reshape the data
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
#Get the models predicted price values
predictions = model.predict(x_test)
predictions = scaler.inverse_transform(predictions)
#Evaluate the model. Get the root mean squared error (RMSE)
rmse = np.sqrt(np.mean(predictions - y_test)**2)
print(rmse)
#Plot the data
train = data[:training_data_len]
valid = data[training_data_len: ]
valid['Predictions'] = predictions
#Visualize the data
plt.figure(figsize=(16,8))
plt.title('Model')
plt.xlabel('Date', fontsize=18)
plt.ylabel('Close Price USD ($)', fontsize=18)
plt.plot(train['Close'])
plt.plot(valid[['Close', 'Predictions']])
plt.legend(['Train', 'Val', 'Predictions'], loc='lower right')
plt.show()
#Show the valid and predicted prices
print(valid)
如果您查看我的“打印”调试方法,它会在 9 到 10 之间停止,这是输出:
C:\Users\gunne\anaconda3\envs\EnvBioWell\python.exe C:/Users/gunne/PycharmProjects/pythonProject/Stocks_Price.py
2021-02-19 09:57:52.322041: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
1
2
3
4
[array([0.00307386, 0.00303452, 0.00288404, 0.00301928, 0.00296962,
0.00284426, 0.00411515, 0.00390359, 0.00365686, 0.00367355,
0.00369274, 0.00313341, 0.00298767, 0.00289699, 0. ,
0.00101894, 0.00078898, 0.00100278, 0.00069457, 0.00245455,
0.00201685, 0.00079746, 0.00101697, 0.0016967 , 0.00120293,
0.00122168, 0.00134546, 0.00070072, 0.00066494, 0.00061141,
0.00019479, 0.00038312, 0.00044424, 0.00024669, 0.00110931,
0.0009756 , 0.00053531, 0.00053962, 0.00040029, 0.00051366,
0.00076044, 0.00067284, 0.00087522, 0.00120881, 0.00188371,
0.00157436, 0.00189504, 0.00228295, 0.00254865, 0.00247892,
0.00319813, 0.00326988, 0.00322377, 0.00247677, 0.00266203,
0.00264398, 0.00297805, 0.00299417, 0.00303742, 0.00322153])]
[0.003108507179665692]
[array([0.00307386, 0.00303452, 0.00288404, 0.00301928, 0.00296962,
0.00284426, 0.00411515, 0.00390359, 0.00365686, 0.00367355,
0.00369274, 0.00313341, 0.00298767, 0.00289699, 0. ,
0.00101894, 0.00078898, 0.00100278, 0.00069457, 0.00245455,
0.00201685, 0.00079746, 0.00101697, 0.0016967 , 0.00120293,
0.00122168, 0.00134546, 0.00070072, 0.00066494, 0.00061141,
0.00019479, 0.00038312, 0.00044424, 0.00024669, 0.00110931,
0.0009756 , 0.00053531, 0.00053962, 0.00040029, 0.00051366,
0.00076044, 0.00067284, 0.00087522, 0.00120881, 0.00188371,
0.00157436, 0.00189504, 0.00228295, 0.00254865, 0.00247892,
0.00319813, 0.00326988, 0.00322377, 0.00247677, 0.00266203,
0.00264398, 0.00297805, 0.00299417, 0.00303742, 0.00322153]), array([0.00303452, 0.00288404, 0.00301928, 0.00296962, 0.00284426,
0.00411515, 0.00390359, 0.00365686, 0.00367355, 0.00369274,
0.00313341, 0.00298767, 0.00289699, 0. , 0.00101894,
0.00078898, 0.00100278, 0.00069457, 0.00245455, 0.00201685,
0.00079746, 0.00101697, 0.0016967 , 0.00120293, 0.00122168,
0.00134546, 0.00070072, 0.00066494, 0.00061141, 0.00019479,
0.00038312, 0.00044424, 0.00024669, 0.00110931, 0.0009756 ,
0.00053531, 0.00053962, 0.00040029, 0.00051366, 0.00076044,
0.00067284, 0.00087522, 0.00120881, 0.00188371, 0.00157436,
0.00189504, 0.00228295, 0.00254865, 0.00247892, 0.00319813,
0.00326988, 0.00322377, 0.00247677, 0.00266203, 0.00264398,
0.00297805, 0.00299417, 0.00303742, 0.00322153, 0.00310851])]
[0.003108507179665692, 0.0026196096172032522]
5
6
7
2021-02-19 09:57:55.479350: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-02-19 09:57:55.479830: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library nvcuda.dll
2021-02-19 09:57:55.506019: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:0e:00.0 name: GeForce RTX 3080 computeCapability: 8.6
coreClock: 1.74GHz coreCount: 68 deviceMemorySize: 10.00GiB deviceMemoryBandwidth: 707.88GiB/s
2021-02-19 09:57:55.506184: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-02-19 09:57:55.509645: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-02-19 09:57:55.509728: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-02-19 09:57:55.511906: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-02-19 09:57:55.512542: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-02-19 09:57:55.516144: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-02-19 09:57:55.517468: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-02-19 09:57:55.518014: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-02-19 09:57:55.518133: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
2021-02-19 09:57:55.518454: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-02-19 09:57:55.519487: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:0e:00.0 name: GeForce RTX 3080 computeCapability: 8.6
coreClock: 1.74GHz coreCount: 68 deviceMemorySize: 10.00GiB deviceMemoryBandwidth: 707.88GiB/s
2021-02-19 09:57:55.519678: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-02-19 09:57:55.519763: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-02-19 09:57:55.519836: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-02-19 09:57:55.519915: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-02-19 09:57:55.519995: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-02-19 09:57:55.520066: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-02-19 09:57:55.520138: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-02-19 09:57:55.520212: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-02-19 09:57:55.520304: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
2021-02-19 09:57:56.033929: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1261] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-02-19 09:57:56.034021: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1267] 0
2021-02-19 09:57:56.034068: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 0: N
2021-02-19 09:57:56.034278: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1406] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 8444 MB memory) -> physical GPU (device: 0, name: GeForce RTX 3080, pci bus id: 0000:0e:00.0, compute capability: 8.6)
2021-02-19 09:57:56.035130: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
8
9
2021-02-19 09:57:56.610845: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2021-02-19 09:57:57.789418: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-02-19 09:57:58.358210: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-02-19 09:57:58.377765: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
Process finished with exit code -1073740791 (0xC0000409)
我将 RTX 3080 GPU 与 Cuddn 8.0 和 Cuda Toolkit 11.0 以及 tensorflow-gpu 2.4.0 与 python 3.7 一起使用。如果有人有任何建议,请告诉我!我已经尝试了所有我能想到的方法,当我运行代码来检查 GPU 时,它会将它拉起来,所以它应该可以工作,但我就是无法做到。一个月前我让它工作了,然后我搞砸了,现在它在输出中显示了所有这些时间戳以及它打开的包。不知道为什么,但我只是需要帮助。
答案 0 :(得分:-2)
您是否尝试过此链接:
https://www.reddit.com/r/tensorflow/comments/jsalkw/rtx_3090_and_tensorflow_for_windows_10_step_by/
或者你已经解决了吗?
链接内容如下:
NVIDIA 3000 系列 GPU (Ampere) 需要 CUDA v11 和 cuDNN v8 才能工作。 anaconda 上的 tensorflow 版本和 Windows 上的 pip(目前最大 tensorflow 2.3)不包括使用 CUDA v11 构建的 tensorflow。但是您可以使用 pip 安装使用 CUDA v11 构建的每晚构建的 tensorflow(当前为 tensorflow 2.5)。除了使用 CUDA v11 构建 tensorflow 之外,您还需要 CUDA v11 和 cuDNN v8 的实际 DLL。通常,您只需使用带有 cudatoolkit 和 cudnn 软件包的 anaconda 安装它们,但是虽然 cudatoolkit 可用于 v11,但对于 cudnn,至少对于 Windows,v8 在 anaconda 中不可用。解决方法是手动获取这些 DLL 并将它们设置在系统环境路径中(以便 python/tensorflow 可以找到并加载它们)。那么让我们开始吧:
首先,如果您还没有安装 anaconda。以管理员权限打开 anaconda 提示符。
输入 conda create -n tf2 python=3.8 并回车以使用 python 3.8 创建一个新的 anaconda 环境(tensorflow nightly build 需要 python 3.8 或更高版本,这就是我们使用 python 3.8 的原因)
输入 activate tf2 或 conda activate tf2 并按回车键进入新环境。
使用 pip3 install tf-nightly-gpu 安装夜间 tensorflow 构建
安装您可能需要的其他软件包。对我来说,它是 conda install jupyter scikit-learn matplotlib pandas
现在,从 NVIDIA(https://developer.nvidia.com/cuda-downloads 或 https://developer.nvidia.com/cuda-toolkit-archive)下载 CUDA v11。是的,文件很大,有 3GB。
此外,显然我们还需要一个用于 C++ 的 Microsoft Visual Studio 版本,以便安装程序正常运行。下载免费的 Visual Studio 社区版 (https://visualstudio.microsoft.com/downloads/) 并安装 C++ 组件。为此,选择“Desktop development with C++”,选择前6个选项并安装。此步骤取自我之前提到的指南,因此如果您遇到此问题,请参考它。对我来说,我已经在我的计算机上设置了带有 C++ 的 Visual Studio,所以我可以跳过这一步。
现在,让我们首先执行 CUDA v11 安装程序。执行它。您可以进行快速安装,但如果您已经安装了 GeForce Experience,您还可以选择自定义选项并取消选择您已经安装的更高版本的所有内容。对我来说,我只需要带有 CUDA 选项的第一个复选框,这样就足够了。
CUDA v11 安装程序基本上所做的是在目录“C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1”中安装所有 CUDA v11 DLL、头文件和内容(版本可能不同你)。我们接下来要做的事情:在这个目录中也添加 cuDNN DLLs、Headers 等,然后将此目录添加到系统路径中。好的,走吧。
从 NVIDIA (https://developer.nvidia.com/rdp/cudnn-download) 下载 cuDNN。这个文件大约 700MB。您需要注册为开发人员并回答一些问题,但别担心,它是免费的。当要求您提供电子邮件时,您可以输入任何电子邮件,因为在下一页中,您将可以选择使用 google 或 facebook 作为替代登录(您可能喜欢也可能不喜欢)。下载文件后,将其解压缩。进入目录,你会看到三个文件夹“bin”、“include”、“lib”。将其与 CUDA v11 目录(C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1)进行比较,您会注意到这些目录也存在于那里!所以只需将文件夹从 cuDNN 复制到 CUDA v11 目录。 Windows 会将文件添加到现有文件夹中。
现在,让我们将这些目录添加到系统路径中。在 Windows 中,打开开始并搜索“This PC”。右键单击并选择“属性”以打开一个名为“系统”的窗口。在底部左侧,选择“高级系统设置”。单击底部的“环境变量...”。在这里,在下半部分,在“系统变量”中,找到并打开“路径”。在这里,单击“新建”将新目录添加到系统路径。每次对以下每个目录执行此操作(如前所述,版本号可能因您而异)。一些目录可能已经列在那里,所以可以随意跳过它们(虽然重复条目没有负面影响,所以不要太担心):C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11 .1\bin C:\Program Files\NVIDIA GPU 计算工具包\CUDA\v11.1\libnvvp C:\Program Files\NVIDIA GPU 计算工具包\CUDA\v11.1\extras\CUPTI\lib64 C:\Program Files\ NVIDIA GPU 计算工具包\CUDA\v11.1\include
现在非常重要:重启你的系统!
现在,运行您的代码以查看是否一切正常。对我来说,它是通过 jupyter notebook 实现的。首先要做的一件简单的事情是导入tensorflow并检查物理设备:import tensorflow as tftf.config.list_physical_devices()
您的 GPU 可能不会显示。仔细查看控制台的输出(对我来说,这是我启动 jupyter 笔记本的 anaconda 提示符)。在那里,你应该看到像 tensorflow/stream_executor/platform/default/dso_loader.cc:49] 成功打开动态库 cudart64_110.dll 或类似的日志,说明无法加载某个 DLL!就我而言,除了 DLL“cusolver64_10.dll”之外的所有内容都已加载。因此,我转到 CUDA v11 目录(C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1),打开“bin”文件夹(DLL 在那里)以检查该 DLL 是否在那里。不,它不是。取而代之的是“cusolver64_11.dll”。所以我所做的只是复制该 DLL 并将副本重命名为“cusolver64_10.dll”。是的,听起来很蠢,但在那之后,一切正常。