十进制DNN回归函数的StringToHashBucketFast误差

2024-04-25 09:04:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在Tensorflow中实现DNN回归模型。在实现以下代码行时,我一直遇到此错误消息TypeError: Input 'input' of 'StringToHashBucketFast' Op has type int32 that does not match expected type of string: `在

x = pd.read_csv(datapath+"[filename].csv")    
x = x.drop('completion',axis=1)
x.wd = x.wd.astype(int).astype(np.int32)
wd = tf.contrib.layers.sparse_column_with_hash_bucket(
      "wd", hash_bucket_size=100)
hr = tf.contrib.layers.real_valued_column("hr")
acc_completion = tf.contrib.layers.real_valued_column("acc_completion")
deep_columns = [tf.contrib.layers.embedding_column(wd, dimension=8), hr, acc_completion]
train_set, test_set = train_test_split(x, test_size=0.2, random_state = random.randint(20, 200))
def input_fn(df):
  # Creates a dictionary mapping from each continuous feature column name (k) to
  # the values of that column stored in a constant Tensor.
    continuous_cols = {k: tf.constant(df[k].values) for k in ["hr","acc_completion"]}
# Creates a dictionary mapping from each categorical feature column name (k)
# to the values of that column stored in a tf.SparseTensor.
categorical_cols = {
      k: tf.SparseTensor(
          indices=[[i, 0] for i in range(df[k].size)],
          values=df[k].values,
          dense_shape=[df[k].size, 1])
      for k in ["wd"]}
# Merges the two dictionaries into one.
    feature_cols = dict(continuous_cols)
    feature_cols.update(categorical_cols)
# Converts the label column into a constant Tensor.
    label = tf.constant(df["day_completion"].values)
  # Returns the feature columns and the label.
    return feature_cols, label
# Parameters
num_epochs = 1000
STEPS = 20000
BATCH_SIZE = 80

#Deep Neural Network Regressor 
regressor = learn.DNNRegressor(feature_columns = deep_columns, hidden_units= [100,50])
regressor.fit(input_fn=lambda: input_fn(train_set), max_steps= STEPS)

我不明白为什么我总是得到这个错误,即使我遵循了Tensorflow网站(https://www.tensorflow.org/versions/r0.12/tutorials/estimators/)上的相同说明。有谁能帮我解决这个问题吗?在


Tags: oftheindfinputsizelayerstf