Closed oeddyo closed 7 months ago
Check if you're training with MPS enabled. If that's the case, consider disabling it and return the operation in CPU mode. This step will help determine if MPS is contributing to the problem. And Try:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
Try the code below. And see what you get. I suspect the issue might be related to the optimizer. Consider switching to SGD or another alternative to see if it resolves the problem.
relu_layer = ReLU(
max_value=5,
negative_slope=0.2,
threshold=0,
)
print(relu_layer(np.array([-10, -5, 0.0, 5, 10])))
At last, try downgrade your tf version to 2.14 to evaluate whether the issue persists.
Thanks @dugujiujian1999 for getting back so fast.
For the first suggestion:
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 472974225874757572
xla_global_id: -1
, name: "/device:GPU:0"
device_type: "GPU"
locality {
bus_id: 1
}
incarnation: 14547657671560620541
physical_device_desc: "device: 0, name: METAL, pci bus id: <undefined>"
xla_global_id: -1
]
tf.Tensor([-2. -1. 0. 5. 5.], shape=(5,), dtype=float32)
I tried sgd
but the issue persists. It seems to still be linear on the boundary. I suspect something's off for me with ReLU
Downgrading to 2.14 indeed fixed the issue
From the conversation, this issue seems to be related to TensorFlow Mac build. Can we move this issue discussion to TensorFlow Repo?
Hi,
I was able to run this successfully on MacOS M1 using Keras 3.
Make sure you install keras 3 using
!pip install -U keras
Below is the code and the output.
import os
os.environ["KERAS_BACKEND"]="tensorflow"
import keras
from keras import layers
from keras import ops
import numpy as np
from sklearn.datasets import make_moons
import tensorflow as tf
import matplotlib.pyplot as plt
print("TensorFlow version:", tf.__version__)
print("Keras version:", keras)
# Prepare the data
X, y = make_moons(n_samples=1000, noise=0.1)
batch_size = 128
# Define the model using Functional API
inputs = layers.Input(shape=(2,))
x = layers.Dense(200)(inputs)
x = layers.ReLU()(x) # Using ReLU as a layer
x = layers.Dense(200)(x)
x = layers.ReLU()(x) # Using ReLU as a layer
outputs = layers.Dense(1, activation='sigmoid')(x)
model = keras.Model(inputs=inputs, outputs=outputs)
# Compile the model
model.compile(optimizer="adam", loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X, y, epochs=50, batch_size=batch_size)
print(model.predict(X)[:10])
# Create a meshgrid for visualization
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100))
# Predict with the model
mesh = np.c_[xx.ravel(), yy.ravel()]
mesh_predictions = model.predict(mesh).round()
mesh_predictions = mesh_predictions.reshape(xx.shape)
# Plot the decision boundary
plt.contourf(xx, yy, mesh_predictions, alpha=0.3, cmap='jet')
plt.scatter(X[:, 0], X[:, 1], c=y, s=20, cmap='jet')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("Decision Boundary")
plt.show()
Output:
I'm not quite sure as I don't have a Mac with M1 to test, but according to the description from pypi.org/project/tensorflow-metal, the currently confirmed latest supported version is only 2.14.
@sachinprasadhs May I know what TF version you're using?
By using tf=2.14.0 with keras 3.0.0 I'm able to get non-linearity
tensorflow-deps 2.10.0 0 apple
tensorflow-estimator 2.14.0 pypi_0 pypi
tensorflow-io-gcs-filesystem 0.34.0 pypi_0 pypi
tensorflow-macos 2.14.0 pypi_0 pypi
tensorflow-metal 1.1.0 pypi_0 pypi```
```keras 3.0.0 pypi_0 pypi```
@oeddyo , Use latest Tensorflow 2.15 and Keras 3.0.2 which is compatible.
This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.
This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.
Hi there,
Not sure if I have something get wrong but it seems somehow the Relu activation is not triggered on Mac M1.
Here's exactly the same code run on Google Colab v.s. Mac M1:
On GCS:
On mac:
Both print: