简体   繁体   English

在运行模型时使用 tensorflow 进行训练时出现 valueError.Evaluate()

[英]valueError while training with tensorflow while running model.Evaluate()

Code 代码

All of the 4 columns are float64.所有 4 列都是 float64。 I'm not sure what to do about this error as I've looked at similar stack overflow issues and nothing regarding converting it to numpy array and/or float32 seem to solve this.我不确定如何处理此错误,因为我已经查看了类似的堆栈溢出问题,并且没有任何关于将其转换为 numpy 数组和/或 float32 似乎可以解决此问题。

This code is base on: Google Colab此代码基于: Google Colab

I just replaced the housing data with mlb pitching data and applied dropna() to the train and test dataframes.我刚刚用 mlb 投球数据替换了住房数据,并将 dropna() 应用于训练和测试数据帧。

Thank you.谢谢你。

In the function train_model() , specify the array .astype(float) .在函数train_model()中,指定数组.astype(float)

So the line you need to edit:所以你需要编辑的行:

features = {name:np.array(value) for name, value in dataset.items()}

... change to: ... 改成:

features = {name:np.array(value).astype(float) for name, value in dataset.items()}

You'll also need to do it in the next block as well:您还需要在下一个块中执行此操作:

test_features = {name:np.array(value).astype(float) for name, value in cut_test_df_norm.items()}

Full function code:完整功能代码:

def train_model(model, dataset, epochs, label_name,
                batch_size=None):
  """Train the model by feeding it data."""

  # Split the dataset into features and label.
  features = {name:np.array(value).astype(float) for name, value in dataset.items()}
  label = np.array(features.pop(label_name))
  history = model.fit(x=features, y=label, batch_size=batch_size,
                      epochs=epochs, shuffle=True) 
  
  print(features)

  # The list of epochs is stored separately from the rest of history.
  epochs = history.epoch
  
  # To track the progression of training, gather a snapshot
  # of the model's mean squared error at each epoch. 
  hist = pd.DataFrame(history.history)
  mse = hist["mean_squared_error"]

  return epochs, mse

And:和:

# After building a model against the training set, test that model
# against the test set.
test_features = {name:np.array(value).astype(float) for name, value in cut_test_df_norm.items()}
test_label = np.array(test_features.pop(label_name)) # isolate the label
print("\n Evaluate the new model against the test set:")
my_model.evaluate(x = test_features, y = test_label, batch_size=batch_size)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM