Skip to content Skip to sidebar Skip to footer

Multiple Target Columns With SkFlow TensorFlowDNNRegressor

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, b

Solution 1:

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


Post a Comment for "Multiple Target Columns With SkFlow TensorFlowDNNRegressor"