在El Capitan笔记本部署Keras遇Illegal instruction:4错误求助
That "Illegal instruction: 4" error is almost always tied to incompatible CPU instruction sets—the pre-built TensorFlow packages available via pip/conda often include optimizations like AVX/AVX2 that older laptops running El Capitan don't support. Your previous fixes (updating Theano, installing mxnet-mkl, downgrading NumPy) didn't address the root issue because Keras was still using the TensorFlow backend, which was the source of the instruction mismatch. Let's walk through the correct steps to resolve this:
Step 1: Uninstall the problematic TensorFlow version
First, remove the current TensorFlow installation that's causing the instruction error:
pip uninstall tensorflow # If you installed tensorflow-gpu, use this instead: # pip uninstall tensorflow-gpu
Step 2: Install a TensorFlow version compatible with older CPUs
Pre-built TensorFlow packages started enforcing AVX optimizations around version 1.14, so we'll install an earlier, compatible release. For El Capitan, TensorFlow 1.13.1 is a reliable choice—it doesn't require AVX support and works well with Keras:
pip install tensorflow==1.13.1
If you're using Conda, you can use this command instead for better system compatibility:
conda install tensorflow=1.13.1
Step 3: Match Keras version to TensorFlow
To avoid version mismatches, install a Keras release that's compatible with TensorFlow 1.13.1. Keras 2.2.4 is the perfect match:
pip install keras==2.2.4
Step 4: Verify the fix
Create a simple test script to confirm Keras imports and runs without errors:
import keras from keras.models import Sequential from keras.layers import Dense # Build a minimal model to test functionality model = Sequential() model.add(Dense(units=64, activation='relu', input_dim=100)) model.add(Dense(units=10, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) print("✅ Keras imported successfully and model compiled without issues!")
Run the script with python your_test_script.py—if you see the success message, you're good to go.
Additional Notes
- If you still run into issues, double-check your Python version: TensorFlow 1.13.1 supports Python 3.5, 3.6, and 3.7. El Capitan's default Python may be outdated, so consider using a virtual environment with a compatible Python version.
- Switching Keras backend to Theano is another option, but you'd need to configure it explicitly by editing
~/.keras/keras.jsonand setting"backend": "theano". However, TensorFlow is generally more widely supported for most use cases.
内容的提问来源于stack exchange,提问作者pun_intended

