问题描述
我是使用 Tensorflow/SkFlow 的新手,我想弄清楚是否可以使用多个目标列并生成多个输出预测.
I'm new to using Tensorflow/SkFlow, and I'm trying to figure out if it is possible to use multiple target columns and produce multiple output predictions.
我尝试了下面的代码,但这似乎不是可接受的输入:
I tried the code below, but this doesn't seem to be acceptable input:
import numpy as np
import tensorflow.contrib.learn as skflow
# Sample data (obviously actual data would contain a lot more rows)
training_data = np.asarray( [
np.asarray( [ 215.0, 5.0], dtype=np.float64 ),
np.asarray( [ 283.0, 2.0], dtype=np.float64 )
], dtype=np.float64 )
training_target = np.asarray( [
np.asarray( [ 220.0, 210.0], dtype=np.float64 ),
np.asarray( [ 285.0, 281.0], dtype=np.float64 )
], dtype=np.float64 )
regressor = skflow.TensorFlowDNNRegressor( hidden_units=[2,4,2] )
regressor.fit( x=training_data, y=training_target, steps=2000 )
print( regressor.predict( training_set.data )[0] )
当我运行此代码时,出现以下错误:
When I run this code, I get the following error:
File "/some/path/anaconda/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 741, in assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (?, 1) and (?, 2) are incompatible
是否可以使用 SkFlow 进行类似的工作?
Is it possible to make something like this work using SkFlow?
推荐答案
有一段使用 DNNRegressor 的代码:
There is a code which using the DNNRegressor:
import numpy as np
from sklearn.cross_validation import train_test_split
from tensorflow.contrib import learn
import tensorflow as tf
import logging
#logging.getLogger().setLevel(logging.INFO)
#Some fake data
N=200
X=np.array(range(N),dtype=np.float32)/(N/10)
X=X[:,np.newaxis]
Y=np.sin(X.squeeze())+np.random.normal(0, 0.5, N)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y,
train_size=0.8,
test_size=0.2)
reg=learn.DNNRegressor(hidden_units=[10,10])
reg.fit(X_train,Y_train,steps=500)
在我测试中,如果 Y_train 的形状为 N*1,则此代码将起作用,否则将失败.而且我不知道如何解决这个问题.
As I test, If the the shape of Y_train is N*1, this code will work, otherwise, it will fail. And I don't know how to fix this problem.
但是,我使用 tflearn 模块编写了一个多目标回归演示,可能会对您有所帮助.
However, I write a multiple target regression demo using tflearn module, may be it will help you.
import tflearn
import tflearn.datasets.mnist as mnist
X,Y,testX,testY = mnist.load_data(one_hot=True)
input_layer = tflearn.input_data(shape=[None, 784],name='input')
dense1 = tflearn.fully_connected(input_layer,128,name='dense1')
dense2 = tflearn.fully_connected(dense1,256,name='dense2')
final = tflearn.fully_connected(dense2,10,activation='relu')
regression = tflearn.regression(final,optimizer='adam',
learning_rate=0.001,
loss='mean_square')
model = tflearn.DNN(regression,checkpoint_path='model.tf.ckpt')
model.fit(X,Y,n_epoch=1,
validation_set=(testX,testY),
show_metric=True,
snapshot_epoch=True,
snapshot_step=500,
run_id='tflearnDemo')
pred = model.predict(testX)
for i in range(len(testX)):
print('the original data: ', testY[i], \
'the predict data: ', pred[i])
print("[*]============================")
ZhQ
这篇关于带有 SkFlow TensorFlowDNNRegressor 的多个目标列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!