简体   繁体   中英

Training a Random Forest on Tensorflow

I am trying to train a tensorflow based random forest regression on numerical and continuos data.

When I try to fit my estimator it begins with the message below:

INFO:tensorflow:Constructing forest with params =

INFO:tensorflow:{'num_trees': 10, 'max_nodes': 1000, 'bagging_fraction': 1.0, 'feature_bagging_fraction': 1.0, 'num_splits_to_consider': 10, 'max_fertile_nodes': 0, 'split_after_samples': 250, 'valid_leaf_threshold': 1, 'dominate_method': 'bootstrap', 'dominate_fraction': 0.99, 'model_name': 'all_dense', 'split_finish_name': 'basic', 'split_pruning_name': 'none', 'collate_examples': False, 'checkpoint_stats': False, 'use_running_stats_method': False, 'initialize_average_splits': False, 'inference_tree_paths': False, 'param_file': None, 'split_name': 'less_or_equal', 'early_finish_check_every_samples': 0, 'prune_every_samples': 0, 'feature_columns': [_NumericColumn(key='Average_Score', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='lat', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), _NumericColumn(key='lng', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)], 'num_classes': 1, 'num_features': 2, 're gression': True, 'bagged_num_features': 2, 'bagged_features': None, 'num_outputs': 1, 'num_output_columns': 2, 'base_random_seed': 0, 'leaf_model_type': 2, 'stats_model_type': 2, 'finish_type': 0, 'pruning_type': 0, 'split_type': 0}

Then the process breaks down and I get a value error below:

ValueError: Shape must be at least rank 2 but is rank 1 for 'concat' (op: 'ConcatV2') with input shapes: [?], [?], [?], [] and with computed input tensors: input[3] = <1>.


This is the code I am using:

import tensorflow as tf
from tensorflow.contrib.tensor_forest.python import tensor_forest
from tensorflow.python.ops import resources
import pandas as pd
from tensorflow.contrib.tensor_forest.client import random_forest
from tensorflow.python.estimator.inputs import numpy_io
import numpy as np

def getFeatures():
    Average_Score = tf.feature_column.numeric_column('Average_Score')
    lat = tf.feature_column.numeric_column('lat')
    lng = tf.feature_column.numeric_column('lng')
    return [Average_Score,lat ,lng]

# Import hotel data
Hotel_Reviews=pd.read_csv("./DataMining/Hotel_Reviews.csv")

Hotel_Reviews_Filtered=Hotel_Reviews[(Hotel_Reviews.lat.notnull() | 
    Hotel_Reviews.lng.notnull())]

Hotel_Reviews_Filtered_Target = Hotel_Reviews_Filtered[["Reviewer_Score"]]
Hotel_Reviews_Filtered_Features = Hotel_Reviews_Filtered[["Average_Score","lat","lng"]]

#Preprocess the data
x=Hotel_Reviews_Filtered_Features.to_dict('list')
for key in x:
    x[key] = np.array(x[key])
y=Hotel_Reviews_Filtered_Target.values

#specify params
params = tf.contrib.tensor_forest.python.tensor_forest.ForestHParams(
  feature_colums= getFeatures(), 
  num_classes=1, 
  num_features=2, 
  regression=True, 
  num_trees=10, 
  max_nodes=1000)

#build the graph
graph_builder_class = tensor_forest.RandomForestGraphs

est=random_forest.TensorForestEstimator(
  params, graph_builder_class=graph_builder_class)

#define input function
train_input_fn = numpy_io.numpy_input_fn(
  x=x,
  y=y,
  batch_size=1000,
  num_epochs=1,
  shuffle=True)

est.fit(input_fn=train_input_fn, steps=500)

The variables x is a list of numpy array of shape (512470,) :

{'Average_Score': array([ 7.7,  7.7,  7.7, ...,  8.1,  8.1,  8.1]),
 'lat': array([ 52.3605759,  52.3605759,  52.3605759, ...,  48.2037451,
     48.2037451,  48.2037451]),
 'lng': array([  4.9159683,   4.9159683,   4.9159683, ...,  16.3356767,
     16.3356767,  16.3356767])}

The variable y is numpy array of shape (512470,1) :

array([[ 2.9],
   [ 7.5],
   [ 7.1],
   ..., 
   [ 2.5],
   [ 8.8],
   [ 8.3]])

Force each array in x to be 2 dim using ndmin=2. Then the shapes should match and concat should be able to operate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM