2

I'm using the estimator library of tensorflow on python. I want to train a student network by using a pre-trained teacher.I'm facing the following issue.

train_input_fn = tf.estimator.inputs.numpy_input_fn(
  x={"x": train_data},
  y=train_labels,
  batch_size=100,
  num_epochs=None,
  shuffle=True)

student_classifier.train(
  input_fn=train_input_fn,
  steps=20,
  hooks=None)

This code returns a generator object that is passed to a student classifier. Inside the generator, we have the inputs and labels (in batches of 100) as tensors. The problem is, I want to pass the same values to the teacher model and extract its softmax outputs. But unfortunately, the model input requires a numpy array as follows

student_classifier = tf.estimator.Estimator(
  model_fn=student_model_fn, model_dir="./models/mnist_student")

def student_model_fn(features, labels, mode): 
  sess=tf.InteractiveSession()
  tf.train.start_queue_runners(sess)
  data=features['x'].eval()
  out=labels.eval()
  sess.close()

  input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])
  eval_teacher_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x":data},
      y=out,
      num_epochs=1,
      shuffle=False)

This requires x and y to be numpy arrays so I converted it via using such as ugly hack of using a session to convert tensor to numpy. Is there a better way of doing this?

P.S. I tried tf.estimator.Estimator.get_variable_value() but it retrieves weights from the model, not the input and output

1 Answers1

0

Convert Tensor to Numpy_array using tf.make_ndarray.

tf.make_ndarray(), Create a numpy ndarray with the same shape and data as the tensor.

Sample working code:

import tensorflow as tf
a = tf.constant([[1,2,3],[4,5,6]])
proto_tensor = tf.make_tensor_proto(a)  
tf.make_ndarray(proto_tensor)

output:

array([[1, 2, 3],
     [4, 5, 6]], dtype=int32)
# output has shape (2,3)