无法使用 Plaidml 在 GPU 上 运行 Keras 模型
Cannot Run Keras Model On GPU With Plaidml
I Want to Run this Keras Model on My GPU but it runs on my cpu I used
Plaidml to use my AMD GPU, plaidml is properly set and it runs
perfectly on other models
I think Maybe Becaouse I'm importing tensoflow but I'm Not sure about that, I need the model to run on the GPU, I have other Models that doesn't import tensorflow that works perfectly with Plaidml
源代码
import numpy as np
from os import environ
environ["KERAS_BACKEND"] = "plaidml.keras.backend"
import keras
from keras.layers import Dense
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
master_url_root = "https://raw.githubusercontent.com/numenta/NAB/master/data/"
df_small_noise_url_suffix = "artificialNoAnomaly/art_daily_small_noise.csv"
df_small_noise_url = master_url_root + df_small_noise_url_suffix
df_small_noise = pd.read_csv(
df_small_noise_url, parse_dates=True, index_col="timestamp"
)
df_daily_jumpsup_url_suffix = "artificialWithAnomaly/art_daily_jumpsup.csv"
df_daily_jumpsup_url = master_url_root + df_daily_jumpsup_url_suffix
df_daily_jumpsup = pd.read_csv(
df_daily_jumpsup_url, parse_dates=True, index_col="timestamp"
)
fig, ax = plt.subplots()
df_small_noise.plot(legend=False, ax=ax)
plt.show()
training_mean = df_small_noise.mean()
training_std = df_small_noise.std()
df_training_value = (df_small_noise - training_mean) / training_std
print("Number of training samples:", len(df_training_value))
TIME_STEPS = 288
# Generated training sequences for use in the model.
def create_sequences(values, time_steps=TIME_STEPS):
output = []
for i in range(len(values) - time_steps + 1):
output.append(values[i : (i + time_steps)])
return np.stack(output)
x_train = create_sequences(df_training_value.values)
print("Training input shape: ", x_train.shape)
model = keras.Sequential(
[
layers.Input(shape=(x_train.shape[1], x_train.shape[2])),
layers.Conv1D(
filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
),
layers.Dropout(rate=0.2),
layers.Conv1D(
filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
),
layers.Conv1DTranspose(
filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
),
layers.Dropout(rate=0.2),
layers.Conv1DTranspose(
filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
),
layers.Conv1DTranspose(filters=1, kernel_size=7, padding="same"),
]
)
model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
model.summary()
history = model.fit(
x_train,
x_train,
epochs=50,
batch_size=128,
validation_split=0.1,
callbacks=[
keras.callbacks.EarlyStopping(monitor="val_loss", patience=5, mode="min")
],
)
plt.plot(history.history["loss"], label="Training Loss")
plt.plot(history.history["val_loss"], label="Validation Loss")
plt.legend()
plt.show()
这是我的任务管理器的图像(Plaidml 在 GPU 0 上设置为 运行 [它适用于其他源代码])
尝试从 keras 导入 keras,而不是使用 tensorflow keras。
import keras
from keras import layers
您应该仍然可以访问所有相同的层、模型等。您可能需要使用
安装没有 tensorflow 的 keras
pip install keras
同时查看您的 GPU 内存使用情况。它应该跳起来,因为数据存储在上面。在 plaidml 中,大多数计算实际上并不在 GPU 上 运行,而是将权重和数据存储在其上。
此外,也许使用以下代码块代替您当前的第一行代码。
import numpy as np
import plaidml.keras
import os
plaidml.keras.install_backend()
I Want to Run this Keras Model on My GPU but it runs on my cpu I used Plaidml to use my AMD GPU, plaidml is properly set and it runs perfectly on other models I think Maybe Becaouse I'm importing tensoflow but I'm Not sure about that, I need the model to run on the GPU, I have other Models that doesn't import tensorflow that works perfectly with Plaidml
源代码
import numpy as np
from os import environ
environ["KERAS_BACKEND"] = "plaidml.keras.backend"
import keras
from keras.layers import Dense
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
master_url_root = "https://raw.githubusercontent.com/numenta/NAB/master/data/"
df_small_noise_url_suffix = "artificialNoAnomaly/art_daily_small_noise.csv"
df_small_noise_url = master_url_root + df_small_noise_url_suffix
df_small_noise = pd.read_csv(
df_small_noise_url, parse_dates=True, index_col="timestamp"
)
df_daily_jumpsup_url_suffix = "artificialWithAnomaly/art_daily_jumpsup.csv"
df_daily_jumpsup_url = master_url_root + df_daily_jumpsup_url_suffix
df_daily_jumpsup = pd.read_csv(
df_daily_jumpsup_url, parse_dates=True, index_col="timestamp"
)
fig, ax = plt.subplots()
df_small_noise.plot(legend=False, ax=ax)
plt.show()
training_mean = df_small_noise.mean()
training_std = df_small_noise.std()
df_training_value = (df_small_noise - training_mean) / training_std
print("Number of training samples:", len(df_training_value))
TIME_STEPS = 288
# Generated training sequences for use in the model.
def create_sequences(values, time_steps=TIME_STEPS):
output = []
for i in range(len(values) - time_steps + 1):
output.append(values[i : (i + time_steps)])
return np.stack(output)
x_train = create_sequences(df_training_value.values)
print("Training input shape: ", x_train.shape)
model = keras.Sequential(
[
layers.Input(shape=(x_train.shape[1], x_train.shape[2])),
layers.Conv1D(
filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
),
layers.Dropout(rate=0.2),
layers.Conv1D(
filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
),
layers.Conv1DTranspose(
filters=16, kernel_size=7, padding="same", strides=2, activation="relu"
),
layers.Dropout(rate=0.2),
layers.Conv1DTranspose(
filters=32, kernel_size=7, padding="same", strides=2, activation="relu"
),
layers.Conv1DTranspose(filters=1, kernel_size=7, padding="same"),
]
)
model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss="mse")
model.summary()
history = model.fit(
x_train,
x_train,
epochs=50,
batch_size=128,
validation_split=0.1,
callbacks=[
keras.callbacks.EarlyStopping(monitor="val_loss", patience=5, mode="min")
],
)
plt.plot(history.history["loss"], label="Training Loss")
plt.plot(history.history["val_loss"], label="Validation Loss")
plt.legend()
plt.show()
这是我的任务管理器的图像(Plaidml 在 GPU 0 上设置为 运行 [它适用于其他源代码])
尝试从 keras 导入 keras,而不是使用 tensorflow keras。
import keras
from keras import layers
您应该仍然可以访问所有相同的层、模型等。您可能需要使用
安装没有 tensorflow 的 keras pip install keras
同时查看您的 GPU 内存使用情况。它应该跳起来,因为数据存储在上面。在 plaidml 中,大多数计算实际上并不在 GPU 上 运行,而是将权重和数据存储在其上。
此外,也许使用以下代码块代替您当前的第一行代码。
import numpy as np
import plaidml.keras
import os
plaidml.keras.install_backend()