【问题标题】:My kernel died whenever I run model.predict_classes()每当我运行 model.predict_classes() 时,我的内核就死了
【发布时间】:2021-07-16 08:57:13
【问题描述】:

我正在使用 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
张量流 2.4.0-rc0

有一些 WARNING 来了,但我认为这不重要,如果可能,请让我在这里发布。

【问题讨论】:

  • 你能把标题改成你要问的吗?

标签: python tensorflow apple-m1


【解决方案1】:

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

【讨论】:

  • 是二分类,所以y被y = LabelEncoder().fit_transform(y)预处理,变成0和1的数组。在colab上运行没有报错。我很困惑为什么内核在我的 macbook 上死掉了。
  • 嗨,我也在我的 MacPro(M1) 上试过你的代码。首先,我遇到了同样的问题,但是当我将 'metrics = ["accuracy"]' 添加到编译过程 'model.compile(optimizer='adam', loss='binary_crossentropy', metrics = ["准确性”])'。不知道是什么原因,或许你可以试试。
猜你喜欢
  • 1970-01-01
  • 2019-07-31
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 2020-02-08
  • 2015-09-26
相关资源
最近更新 更多