首页 > 解决方案 > nameError 'model' 未定义 TensorFlow 问题

问题描述

我不确定我是否遗漏了包裹,或者问题是否出在其他地方。既然我已经安装了 Miniforge 并制作了 venv 等,我就可以导入 TensorFlow。(对我为实现这一目标所做的工作并不完全有信心,但我能够在 TensorFlow 和基本 python env 之间切换。)我也能够创建和编译一个模型。但是,我在跑步时没有冗长model.fit,也无法从中获取训练历史记录model.history.history。当我尝试获取历史记录时,我得到:

NameError:名称'模型'未定义'

我还注意到模型没有在 Vscode 中定义,它只是说加载。我试过设置 model.fit() = history 也没有帮助

我的尝试:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import utils as np_utils

from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential()

model.add(Dense(4, activation='relu'))
model.add(Dense(4, activation='relu')) 
model.add(Dense(4, activation='relu'))
model.add(Dense(1))

#compiling the model, mean squared error is used for regression models 
model.compile(optimizer='rmsprop', loss='mse')

model.fit(x = X_train,y=y_train,epochs=250, verbose=1)
model.history.history

数据:

'          price     feature1     feature2\n0    461.527929   999.787558   999.766096\n1    548.130011   998.861615  1001.042403\n2    410.297162  1000.070267   998.844015\n3    540.382220   999.952251  1000.440940\n4    546.024553  1000.446011  1000.338531\n..          ...          ...          ...\n995  476.526078  1000.018988   999.672732\n996  457.313186   998.855379  1000.020026\n997  456.720992  1001.451646   998.847606\n998  403.315576  1000.771023   998.562851\n999  599.367093   999.232244  1001.451407\n\n[1000 rows x 3 columns]' ```
  

标签: pythonkerasjupyter-notebooktensorflow2.0tf.keras

解决方案


更新。由于拒绝学习 venv 的功能,我搞砸了我所有的环境。如果你有类似的问题,我会告诉你我做了什么。

  1. 硬重置一切确保文件备份
  2. 安装python、homebrew、Xcode、命令行工具
  3. 使用苹果开发者网站,我找到了张量流设置的教程,来源到下面的链接。
  4. 我按照说明安装了 mini forge。
  5. 在我安装张量流之前,我用 conda 命令做了一个 venv。
  6. 在新的 venv 中安装了张量流。它工作得很好。我看到了一些错误,但它能够运行在网络上找到的测试模型。

而已。我在 Jupyter notebook 中运行时遇到了一些奇怪的错误,但顺序模型在 vs 代码中运行良好。我正在使用支持 gpu 的苹果 M1 和张量流。

苹果开发者链接:https ://developer.apple.com/metal/tensorflow-plugin/


推荐阅读