首页 > 解决方案 > 每当我运行 model.predict_classes() 时,我的内核就死了

问题描述

我正在使用 macbook pro m1,发现我不能将 tensorflow 与 Anaconda 一起使用,所以我通过以下链接逐步安装它: https ://towardsdatascience.com/installing-tensorflow-on-the-m1-mac -410bb36b776

我现在可以导入 tensorflow 并使用以下链接中的代码进行测试,但遇到了问题。https://machinelearningmastery.com/neural-network-for-cancer-survival-dataset/

它在 colab 上成功运行,但在我的 macbook 上运行不成功。

以下是代码:

# fit a simple mlp model on the haberman and review learning curves
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from matplotlib import pyplot
# load the dataset
path = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/haberman.csv'
df = read_csv(path, header=None)
# split into input and output columns
X, y = df.values[:, :-1], df.values[:, -1]
# ensure all data are floating point values
X = X.astype('float32')
# encode strings to integer
y = LabelEncoder().fit_transform(y)
# split into train and test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, stratify=y, random_state=3)
# determine the number of input features
n_features = X.shape[1]
# define model
model = Sequential()
model.add(Dense(10, activation='relu', kernel_initializer='he_normal', input_shape=(n_features,)))
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy')
# fit the model
history = model.fit(X_train, y_train, epochs=200, batch_size=16, verbose=0, validation_data=(X_test,y_test))

当我运行这个时:

# predict test set
yhat = model.predict_classes(X_test)

内核死了。

我尝试删除 miniforge3 文件夹并再次安装 tensorflow,但问题仍然存在。

版本:
Python 3.8.10
tensorflow 2.4.0-rc0

有一些WARNING即将出现,但我认为这并不重要,如果可能,请让我在这里发布。

标签: pythontensorflowapple-m1

解决方案


当标签 y 是多类任务中的稀疏数字时,我遇到了同样的问题。在我将标签 y 转换为 one-hot 向量后,问题就消失了。但是,我在二进制类问题中没有遇到这个问题。也许对标签做一些预处理。


推荐阅读