在 TensorFlow 上训练随机森林
Posted
技术标签:
【中文标题】在 TensorFlow 上训练随机森林【英文标题】:Training a Random Forest on Tensorflow 【发布时间】:2018-06-13 00:47:56 【问题描述】:我正在尝试在数值和连续数据上训练基于张量流的随机森林回归。
当我尝试拟合我的估算器时,它会从以下消息开始:
INFO:tensorflow: 使用参数构建森林 =
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':假,'use_running_stats_method':假,'initialize_average_splits':假,'inference_tree_paths':假,'param_file':无,'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, 'regression': 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
然后过程中断,我在下面得到一个值错误:
ValueError:形状必须至少为 2 级,但对于具有输入形状:[?]、[?]、[?]、[] 和计算输入张量的“concat”(操作:“ConcatV2”)为 1 级: 输入[3] = .
这是我正在使用的代码:
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)
变量x是一个numpy数组的列表(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])
变量 y 是形状为 (512470,1) 的 numpy 数组:
array([[ 2.9],
[ 7.5],
[ 7.1],
...,
[ 2.5],
[ 8.8],
[ 8.3]])
【问题讨论】:
请将日志和错误消息作为文本而不是图像放在您的问题中。 【参考方案1】:使用 ndmin=2 强制 x 中的每个数组为 2 暗淡。那么形状应该匹配并且concat应该能够操作。
【讨论】:
以上是关于在 TensorFlow 上训练随机森林的主要内容,如果未能解决你的问题,请参考以下文章
如何在计算机中训练神经网络或随机森林算法并在以后的Android设备中进行测试?