Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use plaidML for machine learning on macOS with AMD GPU

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to use plaidML in the macOS with AMD GPU for machine learning, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Do you want to train machine learning models on Mac's integrated AMD GPU or external graphics cards? Ask for nothing but PlaidML.

Anyone who tries to use TensorFlow to train neural networks on macOS knows that the process is bad. Because Nvidia chipset is needed for GPU accelerated training, TensorFlow can only use CPU on Mac. Most large models spend several orders of magnitude more training time on CPU than on simple GPU.

To make matters worse, many Mac have powerful, cautious AMD GPU that are forced to sit idle during training. TensorFlow only supports Nvidia devices that are not compatible with macOS. This is where plaidML appears.

You can install PlaidML and use it to train the Keras model on Mac's GPU without spending time on cloud-based systems or buying new machines.

TensorFlow is used as the back end of Keras, interpreting Keras's advanced Python syntax and translating it into instructions that can be executed in parallel on specialized hardware, such as GPU.

PlaidML is an alternative to Keras and supports parallelization frameworks other than Nvidia CUDA. On Mac, you can use PlaidML to train Keras models on CPU,CPU 's integrated graphics, cautious AMD GPUs, and even external AMD GPU connected through Thunderbolt 3.

Start using PlaidML because you are looking for a way to train deep convolution neural networks on very large image data sets. I tried to do this in Google's Colab, but the online tool turned out to be very frustrating for long-running work. There is a Radeon RX580 eGPU dust deposit, so I want to use a method to train the model locally on the MacBook.

After a few quick steps, you start using PlaidML. This is the way to use it on the system. First install PlaidML through pip. It is strongly recommended that you use a virtual environment here to isolate the PlaidML installation from the rest of the system.

The power of PlaidML comes from its simplicity. After installation, activating GPU is as simple as running

Plaidml-setup

After you choose whether you want to enable experimental features, the tool asks which computing device to use. You should see a list similar to the following:

1: llvm_cpu.0

2: metal_intel (r) _ hd_graphics_530.0

3: metal_amd_radeon_pro_450.0

4: metal_amd_radeon_rx_580.0

The first choice is my CPU, the second is the Intel integrated graphics card in the CPU, the third is the cautious AMD GPU in the 15-inch MacBook Pro, and the fourth is my RX 580eGPU. Absolutely love its simplicity to switch processors; this makes it possible to use the careful GPU of laptops to train simple models anytime, anywhere, and use eGPU for heavier tasks.

The only thing to note is that you no longer have access to TensorFlow features (such as TensorFlow datasets). All code you write must use pure Keras. It hasn't been found that this is a big limitation, but it will lead to more portable software anyway. PlaidML can also be used with Nvidia GPU, so PlaidML makes things very simple if you work in a team that uses a different GPU architecture. Using PlaidML as the backend of Keras is simple, as follows:

From os import environenviron ["KERAS_BACKEND"] = "plaidml.keras.backend" import keras

okay. The following is a complete example that you can try on your own system after installing PlaidML. It uses a hidden layer to train a very simple neural network to sum the input vectors.

Import numpy as npfrom os import environenviron ["KERAS_BACKEND"] = "plaidml.keras.backend" import kerasfrom keras.layers import Densefrom matplotlib import pyplot as plt # Paramsnum_samples = 1000000; vect_len = 20; max_int = 10; min_int = 1 # Generate datasetX = np.random.randint (min_int, max_int, (num_samples, vect_len)) Y = np.sum (X, axis=1) # Get 80% of data for trainingsplit_idx = int (0.8 * len (Y)) train_X = X [: split_idx,:]; test_X = X [split _ idx:,:] train_Y = Y [: split_idx] Test_Y = Y [split _ idx:] # Make modelmodel = keras.models.Sequential () model.add (keras.layers.Dense (32, activation='relu', input_shape= (vect_len,)) model.add (keras.layers.Dense (1)) model.compile ('adam',' mse') history = model.fit (train_X, train_Y, validation_data= (test_X, test_Y),\ epochs=10 Batch_size=100) # summarize historyplt.plot (history.history ['loss']) plt.plot (history.history [' val_loss']) plt.title ('model loss') plt.ylabel (' loss') plt.xlabel ('epoch') plt.legend ([' train', 'test'], loc='upper left') plt.show ()

You can try this on other computing devices. You may find that it is faster to train this model on CPU because the dataset is very small and the model is very simple. But for more complex models, the speed will be greatly improved. You can find some heavier tests on the PlaidML GitHub page.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report