nameError 'model' not defined TensorFlow 问题

nameError 'model' not defined TensorFlow issue

我不确定是我丢失了包裹还是其他问题。我现在可以导入 TensorFlow,因为我已经安装了 Miniforge 并制作了 venv 等。(我对实现此目标所做的工作并不完全有信心,但我可以在 TensorFlow 和基础 python env 之间切换。)我也能够创建和编译模型。但是,当 运行 model.fit 时我不会冗长,也无法从 model.history.history 获取训练历史记录。当我尝试获取历史记录时,我得到:

NameError: name 'model' is not defined'

我还注意到 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]' ```
  

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

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

就是这样。我在 Jupyter notebook 中 运行 时遇到了一些奇怪的错误,但是顺序模型 运行 在 vs 代码中很好。我正在使用支持 gpu 的 apple M1 和张量流。

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