4

How can I implement a K-fold Cross Validation on a model in Tensorflow? I have done it before using scikit learn but not with Tensorflow. For example, let's say I have the following model...

def random_forest(target, data):

# Drop the target label, which we save separately.
X = data.drop([target], axis=1).values
y = data[target].values

# Run Cross Validation on Random Forest Classifier.
clf_tree = ske.RandomForestClassifier(n_estimators=50)

Then I would run clf_tree through a cross validation function...

unique_permutations_cross_val(X, y, clf_tree)

...which is defined as...

def unique_permutations_cross_val(X, y, model):

# Split data 20/80 to be used in a K-Fold Cross Validation with unique permutations.
shuffle_validator = model_selection.ShuffleSplit(n_splits=10, test_size=0.2, random_state=0)

# Calculate the score of the model after Cross Validation has been applied to it. 
scores = model_selection.cross_val_score(model, X, y, cv=shuffle_validator)

# Print out the score (mean), as well as the variance.
print("Accuracy: %0.4f (+/- %0.2f)" % (scores.mean(), scores.std()))

This is very easy to do. However, let's say I have a working linear regression model defined as so...

# Drop the target label, which we save separately.
X = data.drop([target], axis=1).values
Y = data[target].values

iterable_X = np.asarray(X)
iterable_Y = np.asarray(Y)

rng = np.random

n_rows = X.shape[0]

X = tf.placeholder("float")
Y = tf.placeholder("float")

W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

pred = tf.add(tf.multiply(X, W), b)

cost = tf.reduce_sum(tf.pow(pred-Y, 2)/(2*n_rows))

optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cost)

init = tf.global_variables_initializer()

with tf.Session() as sess:

    sess.run(init)

    for epoch in range(FLAGS.training_epochs):

        avg_cost = 0

        for (x, y) in zip(iterable_X, iterable_Y):

            _, c = sess.run([optimizer, cost], feed_dict={X:x, Y:y})

            avg_cost += c / (n_rows/FLAGS.batch_size)

        # display logs per epoch step
        if (epoch + 1) % FLAGS.display_step == 0:

            print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))

    print("Optimization Finished!")

The model works, but how can I implement this model using a K-fold Cross Validation. I want to split my data into train data and test data and then run the model using Cross Validation. How can I do this?

buydadip
  • 8,890
  • 22
  • 79
  • 154
  • 1
    Possible duplicate of [How to perform k-fold cross validation with tensorflow?](https://stackoverflow.com/questions/39748660/how-to-perform-k-fold-cross-validation-with-tensorflow) – geher Apr 30 '18 at 10:11

0 Answers0