apple / tensorflow_macos

TensorFlow for macOS 11.0+ accelerated using Apple's ML Compute framework.
Other
3.66k stars 308 forks source link

Initialization issues and model.predict doesn't work #266

Open edavidk7 opened 3 years ago

edavidk7 commented 3 years ago

I am new to both TensorFlow and machine learning, I built working code on my windows system and wanted to try it on M1. I used the virtual environment created by the installer, but it seems to have issues especially with fitting (model.predict) my own data. Here is the code: `import tensorflow as tf from tensorflow import keras

import matplotlib.pyplot as plt

%matplotlib inline

import numpy as np (X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data() X_train, X_test = X_train/255, X_test/255 X_train_flattened = X_train.reshape(len(X_train),2828) X_test_flattened = X_test.reshape(len(X_test),2828) model = keras.Sequential([ keras.layers.Dense(600,input_shape=(784,), activation = "relu"), keras.layers.Dense(400, activation = "sigmoid"), keras.layers.Dense(200, activation = "sigmoid"), keras.layers.Dense(75, activation = "sigmoid"), keras.layers.Dense(10, activation = "sigmoid") ]) model.compile( optimizer = "adam", loss = "sparse_categorical_crossentropy", metrics = ["accuracy"] ) model.fit(X_train_flattened, y_train, epochs=28) model.evaluate(X_test_flattened,y_test)

print("Accuracy assesment:") y_predictions = model.predict(X_test_flattened) print(y_test[:15])

y_predicted_labels = [np.argmax(j) for j in y_predictions] print(y_predicted_labels[:15]) cm = tf.math.confusion_matrix(labels=y_test, predictions=y_predicted_labels)

plt.matshow(cm)`

and this is the console output: (tensorflow_macos_venv) davidkorcak@David-Air Documents % /usr/bin/env /Users/davidkorcak/Documents/tensorflow_macos_venv/bin/python /Users/davidkorcak/.vscode/extensions/ms-python.python-2021.5.842923320/pythonFiles/lib/python/debugpy/launcher 60087 -- /Users/davidkorcak/Documents/test.py (tensorflow_macos_venv) davidkorcak@David-Air Documents % cd /Users/davidkorcak/Documents ; /usr/bin/env /Users/davidkorcak/Documents/tensorflow_macos_venv/bin/python /Users/davidkorcak/.vscode/extensions/ms-python.python-2021.5.842923320/pythonFiles/lib/python/debugpy/launcher 60109 -- /Users/davidkorcak/Documents/test.py 2021-05-19 11:53:53.554146: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2) 2021-05-19 11:53:53.554439: W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] Failed to get CPU frequency: 0 Hz Epoch 1/28 1875/1875 [==============================] - 4s 2ms/step - loss: 0.6058 - accuracy: 0.8328 Epoch 2/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.1043 - accuracy: 0.9695 Epoch 3/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0654 - accuracy: 0.9807 Epoch 4/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0516 - accuracy: 0.9850 Epoch 5/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0371 - accuracy: 0.9887 Epoch 6/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0304 - accuracy: 0.9913 Epoch 7/28 1875/1875 [==============================] - 3s 1ms/step - loss: 0.0233 - accuracy: 0.9928 Epoch 8/28 1875/1875 [==============================] - 3s 1ms/step - loss: 0.0250 - accuracy: 0.9917 Epoch 9/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0176 - accuracy: 0.9946 Epoch 10/28 1875/1875 [==============================] - 3s 1ms/step - loss: 0.0148 - accuracy: 0.9954 Epoch 11/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0118 - accuracy: 0.9965 Epoch 12/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0143 - accuracy: 0.9954 Epoch 13/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0105 - accuracy: 0.9965 Epoch 14/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0091 - accuracy: 0.9975 Epoch 15/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0120 - accuracy: 0.9962 Epoch 16/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0084 - accuracy: 0.9975 Epoch 17/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0099 - accuracy: 0.9970 Epoch 18/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0068 - accuracy: 0.9978 Epoch 19/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0072 - accuracy: 0.9978 Epoch 20/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0073 - accuracy: 0.9977 Epoch 21/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0077 - accuracy: 0.9978 Epoch 22/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0045 - accuracy: 0.9988 Epoch 23/28 1875/1875 [==============================] - 3s 1ms/step - loss: 0.0048 - accuracy: 0.9983 Epoch 24/28 1875/1875 [==============================] - 3s 1ms/step - loss: 0.0046 - accuracy: 0.9987 Epoch 25/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0058 - accuracy: 0.9985 Epoch 26/28 1875/1875 [==============================] - 3s 1ms/step - loss: 0.0041 - accuracy: 0.9987 Epoch 27/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0059 - accuracy: 0.9980 Epoch 28/28 1875/1875 [==============================] - 3s 2ms/step - loss: 0.0041 - accuracy: 0.9988 313/313 [==============================] - 0s 607us/step - loss: 0.0869 - accuracy: 0.9829 Accuracy assesment: 2021-05-19 11:55:15.583944: I tensorflow/compiler/tf2mlcompute/kernels/mlc_subgraph_op.cc:326] Compute: Failed in processing TensorFlow graph sequential/MLCSubgraphOp_2_0 with frame_id = 0 and iter_id = 0 with error: Internal: ExecuteMLCInferenceGraph: Failed to execute MLC inference graph. (error will be reported 5 times unless TF_MLC_LOGGING=1). 2021-05-19 11:55:15.587260: F tensorflow/core/framework/op_kernel.cc:983] Check failed: outputs_[index].tensor == nullptr (0x124715490 vs. nullptr) Any ideas on what to do? Besides the tensorflow-macos dependencies, I also have Jupyter installed

masasakai commented 3 years ago

I have a same issue. MacTF a1,a2,a3 are same. Please suggest the environment for MacTF, For example numpy etc.

devnev39 commented 3 years ago

It is working fine for me. Try this ->

  1. If you have numpy 1.20 then try to install numpy 1.19.5
  2. Check if tf.executing_eagerly() is set to True as it should be
Screenshot 2021-05-21 at 11 04 26 AM Screenshot 2021-05-21 at 11 04 41 AM Screenshot 2021-05-21 at 11 04 48 AM
masasakai commented 3 years ago

Thank you, @devnev39. I tried your code on my M1 mac. It is works! I understand this issue that is graph executing or eagerly executing. I change to design eagerly executable models. Thank you again.

edavidk7 commented 3 years ago

@devnev39 I have the numpy version supplied with TensorFlow-macOS, which is 1.18.5. I will try your recommended code and the changing the eagerly execution parameter. Thank you.

edavidk7 commented 3 years ago

@devnev39 It seems the activation function in the last layer is the issue here. I changed the code line by line to match yours and it only works once the activation function parameter is removed in the last layer. Edit: linear activation for output layer works fine, issue seems to be only with sigmoid

devnev39 commented 3 years ago

I guess there is some error in your side of code. I just did all the possibilities with sigmoid and relu and also setting from_logits=True (generally used in loss function when there is no activation at output layer) and they all executed the same code in the pictures without any error.