tensorflow-metal makes jupyter notebook kernel dead

I ran the following notebook with tensorflow-metal--0.5.0
import tensorflow as tf
from tensorflow import keras
import numpy as np
(X_train_full, y_train_full), (X_test, y_test) = keras.datasets.fashion_mnist.load_data()
X_train, X_valid = X_train_full[:-5000], X_train_full[-5000:]
y_train, y_valid = y_train_full[:-5000], y_train_full[-5000:]

X_mean = X_train.mean(axis=0, keepdims=True)
X_std = X_train.std(axis=0, keepdims=True) + 1e-7
X_train = (X_train - X_mean) / X_std
X_valid = (X_valid - X_mean) / X_std
X_test = (X_test - X_mean) / X_std

X_train = X_train[..., np.newaxis]
X_valid = X_valid[..., np.newaxis]
X_test = X_test[..., np.newaxis]
from functools import partial

DefaultConv2D = partial(keras.layers.Conv2D,
                        kernel_size=3, activation='relu', padding="SAME")

input_ = keras.layers.Input(shape=[28, 28, 1])
conv0 = DefaultConv2D(filters=64, kernel_size=7)(input_)
pool1 = keras.layers.MaxPooling2D(pool_size=2)(conv0)
conv1 = DefaultConv2D(filters=128)(pool1)
conv2 = DefaultConv2D(filters=128)(conv1)
pool2 = keras.layers.MaxPooling2D(pool_size=2)(conv2)
conv3 = DefaultConv2D(filters=256)(pool2)
conv4 = DefaultConv2D(filters=256)(conv3)
pool3 = keras.layers.MaxPooling2D(pool_size=2)(conv4)
flatten = keras.layers.Flatten()(conv4)
hidden1 = keras.layers.Dense(units=128, activation='relu')(flatten)
dropout1 = keras.layers.Dropout(0.5)(hidden1)
hidden2 = keras.layers.Dense(units=64, activation='relu')(dropout1)
dropout2 = keras.layers.Dropout(0.5)(hidden2)
output = keras.layers.Dense(units=10, activation='softmax')(dropout2)

model = keras.Model(inputs=[input_], outputs=[output])
model.compile(loss="sparse_categorical_crossentropy", optimizer="nadam", metrics=["accuracy"])

model.fit(X_train, y_train, epochs=20, validation_data=(X_valid, y_valid))

However I got the error message the kernel appears to have died. it will restart automatically. in the 3rd cell.

I also ran this python script from terminal one line at a time, and I got the error message I attached above when I tried to run the code conv0 = DefaultConv2D(filters=64, kernel_size=7)(input_)

With tensorflow-metal uninstalled, this code runs without any error messages.

Hi @ishibashi

Thanks for reporting the issue! Based on the attached error message:

[MPSGraph randomUniformTensorWithShape:stateTensor:name:]: unrecognized selector sent to instance 0x6000007a41a0

I'm fairly confident that the issue comes from the random op used in the initializer for layers calling upon an MPSGraph API that is available for MacOs 12.1+ (https://developer.apple.com/documentation/metalperformanceshadersgraph/mpsgraph/3901486-randomuniformtensorwithshape). This is indeed a change made between tensorflow-metal 0.4.0 and 0.5.0 and would explain the issue you are seeing. Sorry this wasn't communicated clearly anywhere, we are working on improving the outward visibility of changes taking places between different releases.

Could you verify which MacOs version you are on? And if at all possible try updating it to the latest or at least 12.1 or newer to make the latest APIs available to see if it solves your issue.

Thank you for your answer. The issue was completely solved!

I have this problem too (Intel Mac). Is there a fix? I get the error when I try to import tensorflow in a Jupyter notebook:

Kernel Restarting
The kernel for Untitled.ipynb appears to have died. It will restart automatically.

Here are the relevant package versions:

macos 12.5.1
tensorflow-macos 2.9.2
tensorflow-metal 0.5.1 
python 3.8.13
numpy 1.22.4
ipython 8.1.1
jupyter 1.0.0
tensorflow-metal makes jupyter notebook kernel dead
 
 
Q