Tensorflow:TypeError:预期的字符串,而是得到 1 类型的“int64" [英] Tensorflow: TypeError: Expected string, got 1 of type 'int64' instead

查看:62
本文介绍了Tensorflow:TypeError:预期的字符串,而是得到 1 类型的“int64"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 tensorflow 中创建逻辑回归模型.

I'm trying to create a logistic regression model in tensorflow.

当我尝试执行 model.fit(input_fn=train_input_fn, steps=200) 时,出现以下错误.

When I try to execute model.fit(input_fn=train_input_fn, steps=200) I get the following error.

    TypeError                                 Traceback (most recent call last)
<ipython-input-44-fd050d8188b5> in <module>()
----> 1 model.fit(input_fn=train_input_fn, steps=200)

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in fit(self, x, y, input_fn, steps, batch_size, monitors)
    180                              feed_fn=feed_fn,
    181                              steps=steps,
--> 182                              monitors=monitors)
    183     logging.info('Loss for final step: %s.', loss)
    184     return self

/home/praveen/anaconda/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)
    447       features, targets = input_fn()
    448       self._check_inputs(features, targets)
--> 449       train_op, loss_op = self._get_train_ops(features, targets)
    450 
    451       # Add default monitors.

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/linear.pyc in _get_train_ops(self, features, targets)
    105     if self._linear_feature_columns is None:
    106       self._linear_feature_columns = layers.infer_real_valued_columns(features)
--> 107     return super(LinearClassifier, self)._get_train_ops(features, targets)
    108 
    109   @property

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.pyc in _get_train_ops(self, features, targets)
    154     global_step = contrib_variables.get_global_step()
    155     assert global_step
--> 156     logits = self._logits(features, is_training=True)
    157     with ops.control_dependencies([self._centered_bias_step(
    158         targets, self._get_weight_tensor(features))]):

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.pyc in _logits(self, features, is_training)
    298       logits = self._dnn_logits(features, is_training=is_training)
    299     else:
--> 300       logits = self._linear_logits(features)
    301 
    302     return nn.bias_add(logits, self._centered_bias())

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.pyc in _linear_logits(self, features)
    255         num_outputs=self._num_label_columns(),
    256         weight_collections=[self._linear_weight_collection],
--> 257         name="linear")
    258     return logits
    259 

/home/praveen/anaconda/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, name, trainable)
    173     transformer = _Transformer(columns_to_tensors)
    174     for column in sorted(set(feature_columns), key=lambda x: x.key):
--> 175       transformed_tensor = transformer.transform(column)
    176       predictions, variable = column.to_weighted_sum(transformed_tensor,
    177                                                      num_outputs,

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.pyc in transform(self, feature_column)
    353       return self._columns_to_tensors[feature_column]
    354 
--> 355     feature_column.insert_transformed_feature(self._columns_to_tensors)
    356 
    357     if feature_column not in self._columns_to_tensors:

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column.pyc in insert_transformed_feature(self, columns_to_tensors)
    410         mapping=list(self.lookup_config.keys),
    411         default_value=self.lookup_config.default_value,
--> 412         name=self.name + "_lookup")
    413 
    414 

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/contrib/lookup/lookup_ops.pyc in string_to_index(tensor, mapping, default_value, name)
    349   with ops.op_scope([tensor], name, "string_to_index") as scope:
    350     shared_name = ""
--> 351     keys = ops.convert_to_tensor(mapping, dtypes.string)
    352     vocab_size = array_ops.size(keys)
    353     values = math_ops.cast(math_ops.range(vocab_size), dtypes.int64)

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in convert_to_tensor(value, dtype, name, as_ref)
    618     for base_type, conversion_func in funcs_at_priority:
    619       if isinstance(value, base_type):
--> 620         ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    621         if ret is NotImplemented:
    622           continue

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/constant_op.pyc in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    177                                          as_ref=False):
    178   _ = as_ref
--> 179   return constant(v, dtype=dtype, name=name)
    180 
    181 

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/constant_op.pyc in constant(value, dtype, shape, name)
    160   tensor_value = attr_value_pb2.AttrValue()
    161   tensor_value.tensor.CopyFrom(
--> 162       tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
    163   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
    164   const_tensor = g.create_op(

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in make_tensor_proto(values, dtype, shape)
    351       nparray = np.empty(shape, dtype=np_dt)
    352     else:
--> 353       _AssertCompatible(values, dtype)
    354       nparray = np.array(values, dtype=np_dt)
    355       # check to them.

/home/praveen/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.pyc in _AssertCompatible(values, dtype)
    288     else:
    289       raise TypeError("Expected %s, got %s of type '%s' instead." %
--> 290                       (dtype.name, repr(mismatch), type(mismatch).__name__))
    291 
    292 

TypeError: Expected string, got 1 of type 'int64' instead.

我不确定要检查哪个功能.有人可以告诉我如何调试吗?提前致谢

I'm not sure which feature to check. Could somebody tell me how could debug this please? Thanks in advance

推荐答案

我有几个数据类型为 int64 的分类列功能.因此,我将列从 int 转换为 string.之后,拟合步骤运行完成.显然,tensorflow 期望分类特征 dtype 是字符串.

I had few categorical columns features whose data types are int64. So, I converted the columns from int to string. After that the fit step ran to completion. Apparently, tensorflow expects the categorical features dtype to be string.

这篇关于Tensorflow:TypeError:预期的字符串,而是得到 1 类型的“int64"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆