简体   繁体   English

python - tensorflow - 输入到StringToHashBucketFast操作类型错误

[英]python - tensorflow - Input to StringToHashBucketFast Operation Type Error

I am trying to train a LinearRegressor in TensorFlow. 我想在TensorFlow中训练一个LinearRegressor。 I have been working through the tutorials on the site and am now trying to apply this to my own data set. 我一直在研究网站上的教程,现在我正在尝试将它应用到我自己的数据集中。

Having made many changes a similar error occurs, specifically around what data type is being passed VS expected. 进行了许多更改后,会发生类似的错误,特别是围绕VS预期传递的数据类型。

import pandas as pd
import tempfile
COLUMNS = ['imp_time', 'width', 'height', 
           'geo_region', 'venue_id', 'seller_member_id', 
           'site_domain', 'tag_id', 'geo_city', 'fold_position', 'event_type']

train_file = 'imp-train.csv' 
test_file =  'imp-test.csv'

df_train = pd.read_table(train_file, names=COLUMNS, skipinitialspace=True, skiprows=1).dropna()
df_test = pd.read_table(test_file, names=COLUMNS, skipinitialspace=True, skiprows=1).dropna()

LABEL_COLUMN = "label"
df_train[LABEL_COLUMN] = (df_train["event_type"].apply(lambda x: 1 if x == "click" else 0)).astype(int)
df_test[LABEL_COLUMN] = (df_test["event_type"].apply(lambda x: 1 if x == "click" else 0)).astype(int)

CATEGORICAL_COLUMNS = ["width", "height","geo_region", "venue_id",
                       "seller_member_id", "site_domain", "tag_id", "geo_city", "fold_position"]
CONTINUOUS_COLUMNS = []
import tensorflow as tf

def input_fn(df):
    # creates dict mapping from each continous feature column name (k) to
    # the values of that column stored in a constant Tensor
    continous_cols = {k: tf.constant(df[k].values)
                     for k in CONTINUOUS_COLUMNS}
    # creates a dict mapping from each categorocal 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,
          shape=[df[k].size, 1])
                          for k in CATEGORICAL_COLUMNS}
    # merge the two dicts into one
    feature_cols = dict(continous_cols.items() + categorical_cols.items())
    # convert the label col into a constant Tensor
    label = tf.constant(df[LABEL_COLUMN].values)
    # return the feature cols and label
    return feature_cols, label

def train_input_fn():
    return input_fn(df_train)

def eval_input_fn():
    return input_fn(df_test)   

# base categorical feature cols
width = tf.contrib.layers.sparse_column_with_hash_bucket("width", hash_bucket_size=100)
height = tf.contrib.layers.sparse_column_with_hash_bucket("height", hash_bucket_size=100)
geo_region = tf.contrib.layers.sparse_column_with_hash_bucket("geo_region", hash_bucket_size=10000)
venue_id = tf.contrib.layers.sparse_column_with_hash_bucket("venue_id", hash_bucket_size=10000)
seller_member_id = tf.contrib.layers.sparse_column_with_hash_bucket("seller_member_id", hash_bucket_size=10000)
site_domain = tf.contrib.layers.sparse_column_with_hash_bucket("site_domain", hash_bucket_size=10000)
tag_id = tf.contrib.layers.sparse_column_with_hash_bucket("tag_id", hash_bucket_size=100000)
fold_position = tf.contrib.layers.sparse_column_with_hash_bucket("fold_position", hash_bucket_size=10)

# intersecting multiple cols with CrossedColumn
width_x_height = tf.contrib.layers.crossed_column([width, height], hash_bucket_size=10000)

# building the model
model_dir = tempfile.mkdtemp()

m = tf.contrib.learn.LinearRegressor(feature_columns=[
  geo_region, venue_id, seller_member_id, site_domain, tag_id, fold_position, width_x_height],
  optimizer=tf.train.FtrlOptimizer(
    learning_rate=0.1,
    l1_regularization_strength=1.0,
    l2_regularization_strength=1.0),
  model_dir=model_dir)

# train model
m.fit(input_fn=train_input_fn, steps=200)

My error trace is as follows: 我的错误跟踪如下:

TypeError                                 Traceback (most recent call last)
<ipython-input-83-4f4e07dac1eb> in <module>()
     11 
     12 # train model
---> 13 m.fit(input_fn=train_input_fn, steps=200)

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in fit(self, x, y, input_fn, steps, batch_size, monitors, max_steps)
    238                              steps=steps,
    239                              monitors=monitors,
--> 240                              max_steps=max_steps)
    241     logging.info('Loss for final step: %s.', loss)
    242     return self

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in _train_model(self, input_fn, steps, feed_fn, init_op, init_feed_fn, init_fn, device_fn, monitors, log_every_steps, fail_on_nan_loss, max_steps)
    548       features, targets = input_fn()
    549       self._check_inputs(features, targets)
--> 550       train_op, loss_op = self._get_train_ops(features, targets)
    551 
    552       # Add default monitors.

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/linear.pyc in _get_train_ops(self, features, targets)
    334       raise ValueError("SDCAOptimizer does not currently support regression.")
    335     self._validate_linear_feature_columns(features)
--> 336     return super(LinearRegressor, self)._get_train_ops(features, targets)
    337 
    338   def _get_eval_ops(self, features, targets, metrics=None):

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.pyc in _get_train_ops(self, features, targets)
    180 
    181     features = self._get_feature_dict(features)
--> 182     logits = self._logits(features, is_training=True)
    183     if self._enable_centered_bias:
    184       centered_bias_step = [self._centered_bias_step(targets, features)]

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.pyc in _logits(self, features, is_training)
    269       logits = self._dnn_logits(features, is_training)
    270     else:
--> 271       logits = self._linear_logits(features, is_training)
    272 
    273     if self._enable_centered_bias:

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.pyc in _linear_logits(self, features, is_training)
    231   def _linear_logits(self, features, is_training):
    232     return self._linear_model.build_model(
--> 233         features, self._linear_feature_columns, is_training)
    234 
    235   def _centered_bias(self):

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/composable_model.pyc in build_model(self, features, feature_columns, is_training)
    175           num_outputs=self._num_label_columns,
    176           weight_collections=[self._weight_collection_name],
--> 177           scope=scope)
    178     return logits
    179 

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.pyc in weighted_sum_from_feature_columns(columns_to_tensors, feature_columns, num_outputs, weight_collections, trainable, scope)
    176     for column in sorted(set(feature_columns), key=lambda x: x.key):
    177       try:
--> 178         transformed_tensor = transformer.transform(column)
    179         predictions, variable = column.to_weighted_sum(transformed_tensor,
    180                                                        num_outputs,

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.pyc in transform(self, feature_column)
    382       return self._columns_to_tensors[feature_column]
    383 
--> 384     feature_column.insert_transformed_feature(self._columns_to_tensors)
    385 
    386     if feature_column not in self._columns_to_tensors:

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column.pyc in insert_transformed_feature(self, columns_to_tensors)
    362         columns_to_tensors[self.name].values,
    363         self.bucket_size,
--> 364         name=self.name + "_lookup")
    365     columns_to_tensors[self] = ops.SparseTensor(
    366         columns_to_tensors[self.name].indices, sparse_id_values,

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_string_ops.pyc in string_to_hash_bucket_fast(input, num_buckets, name)
    183   """
    184   result = _op_def_lib.apply_op("StringToHashBucketFast", input=input,
--> 185                                 num_buckets=num_buckets, name=name)
    186   return result
    187 

/Users/dennisy/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords)
    461             if input_arg.type != types_pb2.DT_INVALID:
    462               raise TypeError("%s expected type of %s." %
--> 463                               (prefix, dtypes.as_dtype(input_arg.type).name))
    464             else:
    465               raise TypeError(

TypeError: Input 'input' of 'StringToHashBucketFast' Op has type int64 that does not match expected type of string.

I am not quite sure what input I am passing to the StringToHashBucketFast operation. 我不太确定我传递给StringToHashBucketFast操作的输入是什么。 I have tried all the separate pieces and they run apart from when I call fit 我已经尝试了所有单独的部分,当我称之为fit时,它们会分开

Looking forward to some smart guys helping! 期待一些聪明人帮忙!

StringToHashBucketFast doesnt work on int64 . StringToHashBucketFast不适用于int64 The meaning of this error message is that you have declared one or more feature column as string (implicitely by using tf.contrib.layers.sparse_column_with_hash_bucket ) but the feature values you are supplying are of int64 . 此错误消息的含义是您已将一个或多个要素列声明为字符串(通过使用tf.contrib.layers.sparse_column_with_hash_bucket隐含地),但您提供的要素值为int64

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

相关问题 Python tensorflow lite 错误:无法设置张量:得到类型 1 的张量,但输入 88 的预期类型为 3 - Python tensorflow lite error:Cannot set tensor: Got tensor of type 1 but expected type 3 for input 88 Python tensorflow unhashable 类型错误 - Python tensorflow unhashable type error 查找 Tensorflow 操作的输入张量 - Finding the input Tensors of a Tensorflow Operation Using a TensorFlow 2.1.0 model built in Python in Java TensorFlow 1.15 | 图中没有名为 [input] 的操作 - Using a TensorFlow 2.1.0 model built in Python in Java TensorFlow 1.15 | No Operation named [input] in the Graph Python用户输入和数学运算错误 - Python user input and mathematical operation error Python Tensorflow复数运算 - Python tensorflow complex number operation 在 java 程序中使用来自 hub.KerasLayer 的 model 和 python 和 tensorflow 2.1.0 时,“图中没有名为 [input] 的操作” - "No Operation named [input] in the Graph" when using in a java programm a model from hub.KerasLayer with python and tensorflow 2.1.0 InvalidArgumentError:输入_1:0被同时获取和获取,tensorflow错误,python - InvalidArgumentError: input_1:0 is both fed and fetched, error with tensorflow, python Python:将元组解包为变量,算术运算类型错误 - Python: Unpacking tuple into variables, arithmetic operation type error python中的减法运算错误 - Error with subtraction operation in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM