尽管安装成功,但仍会出现Keras和PlaidML错误

2024-06-14 10:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经安装了Keras和PlaidML的最新版本。我运行了文件plaidml安装程序,并将plaidml配置为使用AMD GPU:

C:\WinPython\python-3.6.1.amd64\Scripts>plaidml-setup

PlaidML Setup (0.7.0)

(...)


Default Config Devices:
   llvm_cpu.0 : CPU (via LLVM)

Experimental Config Devices:
   llvm_cpu.0 : CPU (via LLVM)
   opencl_amd_gfx902.0 : Advanced Micro Devices, Inc. gfx902 (OpenCL)

Using experimental devices can cause poor performance, crashes, and other nastiness.

Enable experimental device support? (y,n)[n]:y

Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS).
Please choose a default device:

   1 : llvm_cpu.0
   2 : opencl_amd_gfx902.0

Default device? (1,2)[1]:2

Selected device:
    opencl_amd_gfx902.0

Almost done. Multiplying some matrices...
Tile code:
  function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }
Whew. That worked.

Save settings to C:\Users\jsupi\.plaidml? (y,n)[y]:y
Success!

我通过运行plaidbench keras mobilenet成功地测试了安装:

C:\WinPython\python-3.6.1.amd64\Scripts>plaidbench keras mobilenet
Running 1024 examples with mobilenet, batch size 1, on backend plaid
INFO:plaidml:Opening device "opencl_amd_gfx902.0"
Compiling network... Warming up... Running...
Example finished, elapsed: 7.484s (compile), 26.724s (execution)

-----------------------------------------------------------------------------------------
Network Name         Inference Latency         Time / FPS
-----------------------------------------------------------------------------------------
mobilenet            26.10 ms                  11.90 ms / 84.02 fps
Correctness: PASS, max_error: 1.8053706298815086e-05, max_abs_error: 9.760260581970215e-07, fail_ratio: 0.0

然后我想在我的GPU上运行一些python模块。我在this answer中读到需要设置os.environ["RUNFILES_DIR"]os.environ["PLAIDML_NATIVE_PATH"]以更正路径,例如:

os.environ["RUNFILES_DIR"] = "/Library/Frameworks/Python.framework/Versions/3.7/share/plaidml"
os.environ["PLAIDML_NATIVE_PATH"] = "/Library/Frameworks/Python.framework/Versions/3.7/lib/libplaidml.dylib"

问题是我在我的系统中找不到任何类似于上一个的东西。我运行了Windows搜索功能,但它在任何地方都找不到libplaidml.dylib文件。因此,我尝试了以下方法:

import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
os.environ["RUNFILES_DIR"] = "C://Users/jsupi/.plaidml"
#os.environ["PLAIDML_NATIVE_PATH"] = "C:/Windows/WinPython/python-3.6.1.amd64/Lib/site-packages"
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import keras


from keras.datasets import mnist #to import our dataset
from keras.models import Sequential, Model # imports our type of network
from keras.layers import Dense, Flatten, Input # imports our layers we want to use

from keras.losses import categorical_crossentropy #loss function
from keras.optimizers import Adam, SGD #optimisers
from keras.utils import to_categorical #some function for data preparation


batch_size = 128
num_classes = 10
epochs = 50

# input image dimensions
img_rows, img_cols = 28, 28

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()


x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)



#Neural network with single dense hidden layer

model = Sequential()
#model.add(Input(input_shape=(28,28)))
model.add(Flatten(input_shape=(28,28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

并收到错误消息:

Traceback (most recent call last):
  File "D:\Kuba\Machine Learning\DigitRecognitionKeras.py", line 51, in <module>
    model.add(Dense(128, activation='relu'))
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\engine\sequential.py", line 181, in add
    output_tensor = layer(self.outputs[0])
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\engine\base_layer.py", line 431, in __call__
    self.build(unpack_singleton(input_shapes))
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\layers\core.py", line 866, in build
    constraint=self.kernel_constraint)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\engine\base_layer.py", line 249, in add_weight
    weight = K.variable(initializer(shape),
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\keras\initializers.py", line 218, in __call__
    dtype=dtype, seed=self.seed)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 59, in wrapper
    return func(*args, **kwargs)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 1305, in random_uniform
    rng_state = _make_rng_state(seed)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 205, in _make_rng_state
    rng_state = variable(rng_init, dtype='uint32')
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 59, in wrapper
    return func(*args, **kwargs)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 1935, in variable
    _device(), plaidml.Shape(_ctx, ptile.convert_np_dtype_to_pml(dtype), *value.shape))
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\keras\backend.py", line 102, in _device
    devices = plaidml.devices(_ctx)
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\__init__.py", line 1075, in devices
    plaidml.settings.start_session()
  File "C:\WinPython\python-3.6.1.amd64\lib\site-packages\plaidml\settings.py", line 77, in start_session
    raise plaidml.exceptions.PlaidMLError('PlaidML is not configured. Run plaidml-setup.')
plaidml.exceptions.PlaidMLError: PlaidML is not configured. Run plaidml-setup.

注意最后一行,它说PlaidML没有配置,尽管我刚刚完成了配置并成功地测试了它。如果我注释掉前3行(因此在没有plaidml的情况下运行),并在所有“import”行中写入tensorflow.keras而不是keras(似乎没有plaidml是必要的),那么程序运行良好

你对如何解决这个问题有什么想法吗?我有Windows10和Python 3.6


Tags: toinpytestimportlibpackagesline
1条回答
网友
1楼 · 发布于 2024-06-14 10:19:58

我也在使用最近开始的plaidml。 我接受了你的代码并对一条导入语句进行了注释

import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
os.environ["RUNFILES_DIR"] = "C://Users/jsupi/.plaidml"
#os.environ["PLAIDML_NATIVE_PATH"] = "C:/Windows/WinPython/python-3.6.1.amd64/Lib/site-packages"
import numpy as np
#import tensorflow as tf <- commented this line, as I did not install tensorflow
import matplotlib.pyplot as plt
import keras


from keras.datasets import mnist #to import our dataset
from keras.models import Sequential, Model # imports our type of network
from keras.layers import Dense, Flatten, Input # imports our layers we want to use

from keras.losses import categorical_crossentropy #loss function
from keras.optimizers import Adam, SGD #optimisers
from keras.utils import to_categorical #some function for data preparation


batch_size = 128
num_classes = 10
epochs = 50

# input image dimensions
img_rows, img_cols = 28, 28

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()


x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)



#Neural network with single dense hidden layer

model = Sequential()
#model.add(Input(input_shape=(28,28)))
model.add(Flatten(input_shape=(28,28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

它对产量起作用

Using plaidml.keras.backend backend.
x_train shape: (60000, 28, 28)
60000 train samples
10000 test samples
INFO:plaidml:Opening device "opencl_amd_ellesmere.0"

可能是您的plaidml设置不正确。 我没有安装tensorflow框架,只使用plaidml、keras。不过,我看到了一些带有tensorflow的安装指南plaidml

相关问题 更多 >