title: Tensorflow - cannot fit training data to TensorForestEstimator
body: <p>Currently I am trying to to implement a random forest regression using Tensorflow's <code>TensorForestEstimator</code>. I have successfully done using scikit-learn's <code>RandomForestRegressor</code> and wants to replicate the same result using Tensorflow. </p>
  
  <p>I uploaded the data using pandas and I split the training and test set using scikit-learn's <code>train_test_split</code>. It contains 4 features (all numerical). </p>
  
  <pre><code>&gt;&gt;&gt; X_train.shape
  (2711, 4)
  &gt;&gt;&gt; y_train.shape
  (2711,)
  </code></pre>
  
  <p>I set the parameters of the for the tree</p>
  
  <pre><code>num_features = int(np.log2(len(clean_data.columns)))
  
  params = ForestHParams(num_classes=1, num_features=num_features,
                         regression=True,num_trees=447, max_nodes=1000)
  
  regressor = TensorForestEstimator(params)
  </code></pre>
  
  <p>Above i set the features as <code>int(np.log2(len(clean_data.columns)))</code> because i used <code>log2</code> for the <code>max_features</code> parameter in my original scikit-learn implementation. </p>
  
  <p>However, when tried to fit the training data, i receive and error like such </p>
  
  <pre><code>---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
      509                 as_ref=input_arg.is_ref,
  --&gt; 510                 preferred_dtype=default_dtype)
      511           except TypeError as err:
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
     1108     if ret is None:
  -&gt; 1109       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
     1110 
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
      945         "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
  --&gt; 946         (dtype.name, t.dtype.name, str(t)))
      947   return t
  
  ValueError: Tensor conversion requested dtype float32 for Tensor with dtype float64: 'Tensor("concat:0", shape=(?, 4), dtype=float64)'
  
  During handling of the above exception, another exception occurred:
  
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-18-79323408f7f7&gt; in &lt;module&gt;()
        1 # from tensorflow import cast, float32
        2 # X_train_cast = cast(X_train, float32)
  ----&gt; 3 regressor.fit(x=X_train, y=y_train)
        4 
        5 #regressor.score()
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
      430                 'in a future version' if date is None else ('after %s' % date),
      431                 instructions)
  --&gt; 432       return func(*args, **kwargs)
      433     return tf_decorator.make_decorator(func, new_func, 'deprecated',
      434                                        _add_deprecated_arg_notice_to_docstring(
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in fit(self, x, y, input_fn, steps, batch_size, monitors, max_steps)
      506     _verify_input_args(x, y, input_fn, None, batch_size)
      507     if x is not None:
  --&gt; 508       SKCompat(self).fit(x, y, batch_size, steps, max_steps, monitors)
      509       return self
      510 
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in fit(self, x, y, batch_size, steps, max_steps, monitors)
     1525         steps=steps,
     1526         max_steps=max_steps,
  -&gt; 1527         monitors=all_monitors)
     1528     return self
     1529 
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
      430                 'in a future version' if date is None else ('after %s' % date),
      431                 instructions)
  --&gt; 432       return func(*args, **kwargs)
      433     return tf_decorator.make_decorator(func, new_func, 'deprecated',
      434                                        _add_deprecated_arg_notice_to_docstring(
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in fit(self, x, y, input_fn, steps, batch_size, monitors, max_steps)
      522       hooks.append(basic_session_run_hooks.StopAtStepHook(steps, max_steps))
      523 
  --&gt; 524     loss = self._train_model(input_fn=input_fn, hooks=hooks)
      525     logging.info('Loss for final step: %s.', loss)
      526     return self
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in _train_model(self, input_fn, hooks)
     1039       self._check_inputs(features, labels)
     1040       training_util._get_or_create_global_step_read()  # pylint: disable=protected-access
  -&gt; 1041       model_fn_ops = self._get_train_ops(features, labels)
     1042       ops.add_to_collection(ops.GraphKeys.LOSSES, model_fn_ops.loss)
     1043       all_hooks.extend(hooks)
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in _get_train_ops(self, features, labels)
     1262       `ModelFnOps` object.
     1263     """
  -&gt; 1264     return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.TRAIN)
     1265 
     1266   def _get_eval_ops(self, features, labels, metrics):
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in _call_model_fn(self, features, labels, mode, metrics, config)
     1225     if 'model_dir' in model_fn_args:
     1226       kwargs['model_dir'] = self.model_dir
  -&gt; 1227     model_fn_results = self._model_fn(features, labels, **kwargs)
     1228 
     1229     if isinstance(model_fn_results, model_fn_lib.ModelFnOps):
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/tensor_forest/client/random_forest.py in _model_fn(features, labels, mode)
      169 
      170     logits, tree_paths, regression_variance = graph_builder.inference_graph(
  --&gt; 171         features)
      172 
      173     summary.scalar('average_tree_size', graph_builder.average_size())
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/tensor_forest/python/tensor_forest.py in inference_graph(self, input_data, **inference_args)
      512             data_spec,
      513             sparse_features=processed_sparse_features,
  --&gt; 514             **inference_args)
      515         probabilities.append(probs)
      516         paths.append(path)
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/tensor_forest/python/tensor_forest.py in inference_graph(self, input_data, data_spec, sparse_features)
      686         sparse_shape,
      687         input_spec=data_spec.SerializeToString(),
  --&gt; 688         params=self.params.serialized_params_proto)
      689 
      690   def size(self):
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/tensor_forest/python/ops/gen_model_ops.py in tree_predictions_v4(tree_handle, input_data, sparse_input_indices, sparse_input_values, sparse_input_shape, input_spec, params, name)
      467         sparse_input_values=sparse_input_values,
      468         sparse_input_shape=sparse_input_shape, input_spec=input_spec,
  --&gt; 469         params=params, name=name)
      470     _result = _op.outputs[:]
      471     _inputs_flat = _op.inputs
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
      531             if input_arg.type != types_pb2.DT_INVALID:
      532               raise TypeError("%s expected type of %s." %
  --&gt; 533                               (prefix, dtypes.as_dtype(input_arg.type).name))
      534             else:
      535               # Update the maps with the default, if needed.
  
  TypeError: Input 'input_data' of 'TreePredictionsV4' Op has type float64 that does not match expected type of float32.
  </code></pre>
  
  <p>My assumption was that i have to set the number of features to the number of all the features (i.e. using all features instead of a subset of the features). But i still get the same error as above. </p>
  
  <p>I tried to look at the source code directly but could not really understood where was the issue. A similar issue is being discuss on github <a href="https://github.com/tensorflow/tensorflow/issues/547" rel="nofollow noreferrer">here</a>.</p>
  
  <p>I was wondering if there was something missing in my implementation? Thanks in advance.</p>
  
  <p><strong>EDIT: Try to convert the <code>X_train</code> and <code>y_train</code> to float32</strong></p>
  
  <p>Another attempt was converting the input into float32.</p>
  
  <p><code>regressor.fit(x=X_train.astype("float32"), y=y_train.astype("float32"))</code></p>
  
  <p>However, i still got the same error as above. Then i tried using <code>tf.cast</code></p>
  
  <pre><code>X_train_cast = cast(X_train, float32)
  y_train_cast = cast(y_train, float32)
  regressor.fit(x=X_train_cast, y=y_train_cast)
  </code></pre>
  
  <p>But i got a different error saying </p>
  
  <pre><code>---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-19-89e4fa057afb&gt; in &lt;module&gt;()
        2 X_train_cast = cast(X_train, float32)
        3 y_train_cast = cast(y_train, float32)
  ----&gt; 4 regressor.fit(x=X_train_cast, y=y_train_cast)
        5 
        6 #regressor.score()
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs)
      430                 'in a future version' if date is None else ('after %s' % date),
      431                 instructions)
  --&gt; 432       return func(*args, **kwargs)
      433     return tf_decorator.make_decorator(func, new_func, 'deprecated',
      434                                        _add_deprecated_arg_notice_to_docstring(
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in fit(self, x, y, input_fn, steps, batch_size, monitors, max_steps)
      504     if (steps is not None) and (max_steps is not None):
      505       raise ValueError('Can not provide both steps and max_steps.')
  --&gt; 506     _verify_input_args(x, y, input_fn, None, batch_size)
      507     if x is not None:
      508       SKCompat(self).fit(x, y, batch_size, steps, max_steps, monitors)
  
  ~/Desktop/88sparses/recommendation/recom/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py in _verify_input_args(x, y, input_fn, feed_fn, batch_size)
      102 
      103     if tensor_util.is_tensor(x) or y is not None and tensor_util.is_tensor(y):
  --&gt; 104       raise ValueError('Inputs cannot be tensors. Please provide input_fn.')
      105 
      106     if feed_fn is not None:
  
  ValueError: Inputs cannot be tensors. Please provide input_fn.
  </code></pre>
  
  <p>I hope that could make the question much more clear. Thanks.</p>