2

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

Is it possible to make something like this work using SkFlow?

twiz
  • 9,041
  • 8
  • 52
  • 84

1 Answers1

2

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)

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.

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

Qiang Zhang
  • 820
  • 8
  • 32