Why Tensorflow error: `failed to convert object of type <class 'dict'> to Tensor` happens and How can I solve it?

Why Tensorflow error: `failed to convert object of type <class 'dict'> to Tensor` happens and How can I solve it?

我正在执行流量分析任务,但由于代码中的一些错误而受阻。我的数据行是这样的:

qurter | DOW (Day of week)| Hour | density | speed | label (predicted speed for another half an hour)

值是这样的:

1, 6, 19, 23, 53.32, 45.23

这意味着在 Friday 19 点的 1st 一刻钟期间,在某些特定街道上,测量了交通密度 23,当前速度为 53.32。预测速度为 45.23

任务是通过上面给出的预测器预测另外半小时的速度。

我正在使用此代码为数据构建 TensorFlow DNNRegressor

import pandas as pd
data = pd.read_csv('dataset.csv')
X = data.iloc[:,:5].values
y = data.iloc[:, 5].values
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=0)

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(X_train)

X_train = pd.DataFrame(data=scaler.transform(X_train),columns =    ['quarter','DOW','hour','density','speed'])
X_test = pd.DataFrame(data=scaler.transform(X_test),columns = ['quarter','DOW','hour','density','speed'])

y_train = pd.DataFrame(data=y_train,columns = ['label'])
y_test = pd.DataFrame(data=y_test,columns = ['label'])

import tensorflow as tf

speed = tf.feature_column.numeric_column('speed')
hour = tf.feature_column.numeric_column('hour')
density = tf.feature_column.numeric_column('density')
quarter= tf.feature_column.numeric_column('quarter')
DOW = tf.feature_column.numeric_column('DOW')

feat_cols = [h_percentage, DOW, hour, density, speed]
input_func = tf.estimator.inputs.pandas_input_fn(x=X_train,y=y_train ,batch_size=10,num_epochs=1000,shuffle=False)

model = tf.estimator.DNNRegressor(hidden_units=[5,5,5],feature_columns=feat_cols)
model.train(input_fn=input_func,steps=25000)
predict_input_func = tf.estimator.inputs.pandas_input_fn(
  x=X_test,
  batch_size=10,
  num_epochs=1,
  shuffle=False)

pred_gen = model.predict(predict_input_func)

predictions = list(pred_gen)
final_preds = []
for pred in predictions:
    final_preds.append(pred['predictions'])

from sklearn.metrics import mean_squared_error

mean_squared_error(y_test,final_preds)**0.5

当我 运行 这段代码时,它会抛出一个结尾错误:

TypeError: Failed to convert object of type <class 'dict'> to Tensor. Contents: {'label': <tf.Tensor 'fifo_queue_DequeueUpTo:6' shape=(?,) dtype=float64>}. Consider casting elements to a supported type. 首先错误的概念是什么?由于处理错误的原因,我找不到来源。以及如何修改解决方案的代码?

其次,使用 tensorflow categorical_column_with_identity 而不是 numeric_columns 表示星期几的 DOW 是否会提高模型性能?

我还想知道将 quarterhour 合并为单个列是否有用,例如 day timequarter 是一小时中的分钟数在 0 和 1 之间归一化)?

First of all what is the concept of error? I couldn't find source for reason of error to deal with it. And how can I modify code for solution?

先说一下解决问题的方法。您需要按如下方式更改 pandas_input_fn 中的参数 y

input_func = tf.estimator.inputs.pandas_input_fn(x=X_train,y=y_train['label'],batch_size=10,num_epochs=1000,shuffle=False)

pandas_input_fn 中的参数 y 似乎不支持 dataframe 键入 运行 model.train()pandas_input_fn 将每个样本 y 解析为类似于本例中 {columnname: value} 的形式,但 model.train() 无法识别它。所以你需要传递 series type.

secondly does it improve the model performance to use tensorflow categorical_column_with_identity instead of numeric_columns for DOW which indicates days of week?

这涉及到我们应该选择categorical还是选择numeric进行特征工程。一个很简单的规则就是,如果你的特征内部比较有明显的大小差异,就选择numeric。如果该特征没有更大或更小的显着性,则应选择categorical。所以我倾向于选择categorical_column_with_identity作为特征DOW

I also want to know if it's useful to merge quarter and hour as a single column like day time (quarter is minutes in an hour which is going to be normalized between 0 and 1)?

交叉特征可能会带来一些好处,比如经纬度特征。我建议你在这里使用 tf.feature_column.crossed_column(link) 。它 returns 用于执行分类特征交叉的列。也可以同时继续保留模型中的特征quarterhour,。

我也遇到过类似的错误:

Failed to convert object of type <class 'tensorflow.python.autograph.operators.special_values.Undefined'> to Tensor.

它发生在 tf.function 中,当时我试图使用一个我之前没有分配过的变量。

要调试它,您必须从方法中删除 tf.function ;-)