代码: 从sklearn导入cross_validation作为cv 导入numpy为np 来自tensorflow.contrib.learn.python.learn.estimators import estimator 来自tensorflow.contrib.tensor_forest.python import tensor_forest
X = np.array([[ 74., 166., 331., 161., 159., 181., 180.],
[ 437., 427., 371., 361., 393., 465., 464.],
[ 546., 564., 588., 595., 536., 537., 520.],
[ 89., 89., 87., 87., 108., 113., 111.],
[ 75., 90., 74., 89., 130., 140., 135.]])
Y = np.array([[ 51., 43., 29., 43., 43., 41., 42.],
[ 22., 23., 26., 27., 25., 19., 19.],
[ 7., 7., 5., 5., 9., 8., 10.],
[ 55., 54., 55., 53., 51., 51., 51.],
[ 58., 57., 57., 58., 55., 55., 55.]])
train_X, test_X, train_Y, test_Y = cv.train_test_split(X, Y,
test_size=0.50, random_state=42)
def build_estimator() :
params = tensor_forest.ForestHParams(num_classes=7, num_features=7,
num_trees=30, max_nodes=100)
graph_builder_class = tensor_forest.RandomForestGraphs
graph_builder_class = tensor_forest.TrainingLossForest
return estimator.SKCompat(random_forest.TensorForestEstimator(
params, graph_builder_class=graph_builder_class,
model_dir=None))
est = build_estimator()
train_X = train_X.astype(dtype = np.float32)
train_Y = train_Y.astype(dtype = np.float32)
est = est.fit(x=train_X, y=train_Y, batch_size = 100)
我的输入和输出形状都是[Number_of_samples,7]。它完美地运行scikitlearn随机森林分类器。但是对于学习,我在拟合估算器时会出现以下错误:
INFO:tensorflow:使用params =构建森林 信息:tensorflow:{' valid_leaf_threshold':1,' split_after_samples': 250,' num_output_columns':8,' feature_bagging_fraction':1.0, ' split_initializations_per_input':1,' bagged_features':无, ' min_split_samples':5,' max_nodes':100,' num_features':7, ' num_trees':30,' num_splits_to_consider':7,' base_random_seed':0, ' num_outputs':1,' dominate_fraction':0.99,' max_fertile_nodes':50, ' bagged_num_features':7,' dominate_method':' bootstrap', ' bagging_fraction':1.0,'回归':False,' num_classes':7}
ValueErrorTraceback(最近一次调用最后一次) in() ----> 1 est = est.fit(x = train_X,y = train_Y,batch_size = 100)
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in fit(self,x,y,batch_size,steps,max_steps,monitors)1351
steps = steps,1352 max_steps = max_steps, - > 1353 monitor = all_monitors)1354 return self 1355/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tensor_forest/client/random_forest.pyc in fit(self,x,y,input_fn,steps,batch_size,monitors,max_steps) 262 elif输入不是None: 263 self._estimator.fit(input_fn = input_fn,steps = steps,monitors = monitors, - > 264 max_steps = max_steps) 265其他: 266引发ValueError('适合:必须同时提供x和y或input_fn。')
/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.pyc 在new_func(* args,** kwargs) 278 _call_location(),decorator_utils.get_qualified_name(func), 279 func。模块,arg_name,日期,说明) - > 280 return func(* args,** kwargs) 281 new_func。 doc = _add_deprecated_arg_notice_to_docstring( 282 func。 doc ,日期,说明)
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in fit(self,x,y,input_fn,steps,batch_size,monitors,max_steps) 424 hooks.append(basic_session_run_hooks.StopAtStepHook(steps,max_steps)) 425 - > 426 loss = self._train_model(input_fn = input_fn,hooks = hooks) 427 logging.info('最后一步的损失:%s。',损失) 428回归自我
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc 在_train_model中(self,input_fn,hooks) 932个功能,标签= input_fn() 933 self._check_inputs(功能,标签) - > 934 model_fn_ops = self._call_legacy_get_train_ops(功能,标签) 935 ops.add_to_collection(ops.GraphKeys.LOSSES,model_fn_ops.loss) 936 all_hooks.extend([
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in _call_legacy_get_train_ops(self,features,labels)1001 1002 def _call_legacy_get_train_ops(self,features,labels): - > 1003 train_ops = self._get_train_ops(features,labels)1004 if isinstance(train_ops,model_fn_lib.ModelFnOps):#Default 签名1005返回train_ops
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc 在_get_train_ops(自我,功能,标签)1160
ModelFnOps
宾语。 1161""" - > 1162返回self._call_model_fn(features,labels,model_fn_lib.ModeKeys.TRAIN)1163 1164 def _get_eval_ops(自我,功能,标签,指标):/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc 在_call_model_fn(自我,功能,标签,模式)1131 if ' model_dir'在model_fn_args中:1132 kwargs [' model_dir'] = self.model_dir - > 1133 model_fn_results = self._model_fn(features,labels,** kwargs)1134 1135 if isinstance(model_fn_results,model_fn_lib.ModelFnOps):
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tensor_forest/client/random_forest.pyc 在_model_fn中(功能,标签) 128如果标签不是None: 129 training_loss = graph_builder.training_loss( - > 130个功能,标签,名称= LOSS_NAME) 131 training_graph = control_flow_ops.group( 132 graph_builder.training_graph(
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tensor_forest/python/tensor_forest.pyc 在training_loss(自我,功能,标签,名称) 559 560 def training_loss(自我,功能,标签,名称=' training_loss'): - > 561返回array_ops.identity(self._get_loss(features,labels),name = name) 562 563
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tensor_forest/python/tensor_forest.pyc 在_get_loss中(自我,功能,标签) 548 self._loss = control_flow_ops.cond( 549 self.average_size()> 0,_average_loss, - > 550 lambda:constant_op.constant(sys.maxsize,dtype = dtypes.float32)) 551 552返回self._loss
/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.pyc 在cond(pred,fn1,fn2,name)1757 context_t = CondContext(pred,pivot_1,branch = 1)1758 context_t.Enter() - > 1759 orig_res,res_t = context_t.BuildCondBranch(fn1)1760 context_t.ExitResult(res_t)1761 context_t.Exit()
/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.pyc 在BuildCondBranch(self,fn)1658 def BuildCondBranch(self,fn): 1659"""将fn()定义的子图添加到图中。""" - > 1660 r = fn()1661 original_r = r 1662 result = []
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tensor_forest/python/tensor_forest.pyc 在_average_loss()中 544 probs = self.inference_graph(features) 545返回math_ops.reduce_sum(self.loss_fn( - > 546 probs,labels))/ math_ops.to_float(array_ops.shape(labels)[0]) 547 548 self._loss = control_flow_ops.cond(
/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/tensor_forest/python/tensor_forest.pyc in _loss(probs,targets) 508 def _loss(probs,targets): 509 if targets.get_shape()。ndims> 1: - > 510 targets = array_ops.squeeze(targets,squeeze_dims = [1]) 511 one_hot_labels = array_ops.one_hot( 512 math_ops.to_int32(targets),
/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.pyc 在挤压(输入,轴,名称,squeeze_dims)2270如果 np.isscalar(轴):2271轴= [轴] - > 2272返回gen_array_ops._squeeze(输入,轴,名称)2273 2274
/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.pyc 在_squeeze(输入,squeeze_dims,名称)3496""" 3497结果 = _op_def_lib.apply_op(" Squeeze",input = input, - > 3498 squeeze_dims = squeeze_dims,name = name)3499返回结果3500
/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.pyc 在apply_op中(self,op_type_name,name,** keywords) 761 op = g.create_op(op_type_name,inputs,output_types,name = scope, 762 input_types = input_types,attrs = attr_protos, - > 763 op_def = op_def) 764如果output_structure: 765输出= op.outputs
/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.pyc 在create_op中(self,op_type,inputs,dtypes,input_types,name,attrs, op_def,compute_shapes,compute_device)2395
original_op = self._default_original_op,op_def = op_def)2396 if compute_shapes: - > 2397 set_shapes_for_outputs(ret)2398 self._add_op(ret)2399
self._record_op_seen_by_control_dependencies(RET)/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.pyc 在set_shapes_for_outputs(op)1755 shape_func = _call_cpp_shape_fn_and_require_op 1756 - > 1757 shapes = shape_func(op)1758如果形状为None:1759引发RuntimeError(
/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.pyc 在call_with_requiring(op)1705 1706 def call_with_requiring(OP): - > 1707 return call_cpp_shape_fn(op,require_shape_fn = True)1708 1709 _call_cpp_shape_fn_and_require_op = call_with_requiring
/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.pyc 在call_cpp_shape_fn中(op,input_tensors_needed, input_tensors_as_shapes_needed,debug_python_shape_fn, require_shape_fn) 608 res = _call_cpp_shape_fn_impl(op,input_tensors_needed, 609 input_tensors_as_shapes_needed, - > 610 debug_python_shape_fn,require_shape_fn) 611如果不是isinstance(res,dict): 612#处理_call_cpp_shape_fn_impl调用unknown_shape(op)的情况。
/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.pyc 在_call_cpp_shape_fn_impl中(op,input_tensors_needed, input_tensors_as_shapes_needed,debug_python_shape_fn, require_shape_fn) 673 missing_shape_fn =真 674其他: - > 675引发ValueError(err.message) 676 677如果missing_shape_fn:
ValueError:无法挤压暗淡[1],预计维度为1,得到7 for' cond / Squeeze' (op:' Squeeze')输入形状:[?,7]。
答案 0 :(得分:1)
现在,当使用TrainingLossForest时,它会假定分类问题。我会在内部解决这个问题,但是现在你可以通过以下方式解决这个问题:
来自tensorflow.contrib.losses.python.losses import loss_ops
def _loss_fn(值,目标): return loss_ops.mean_squared_error(values,targets)
def _builder_class(params,** kwargs): return tensor_forest.TrainingLossForest( params,loss_fn = _loss_fn,** kwargs)
TensorForestEstimator(...,graph_builder_class = _builder_class)
或者你想要的任何损失函数(以MSE为例)。此外,简单地使用(graph_builder_class = tensor_forest.RandomForestGraphs)应该足够了,但是损失是节点的数量,因此当森林停止增长或达到max_nodes时训练停止,这可能不是你想要的。