Recreating Tensorflow tutorials in R

I just realized that the below post is wrong, and my entire problem was a typo -- oops! Explanation (for anyone who is interested) is in a reply post.

Hi all,

I am trying to recreate a tensorflow tutorial in R using the tensorflow package. I have run into a bit of trouble, and I was wondering if anyone could help me (if this is the wrong place to post this, then I apologize and can move it elsewhere)

I was able to recreate everything until I got to the last jupyter cell. There, I got the following error:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: Fetch argument array([[1.15248305e-12, 9.86847939e-06, 7.58932298e-04, ...,
        4.14203021e-13, 9.73160734e-01, 1.03406331e-08],
       [4.78699061e-17, 4.34612179e-07, 2.33566067e-09, ...,
        5.94925042e-09, 2.69730827e-08, 3.23182274e-13],
       [1.23463545e-16, 5.32164115e-11, 1.38918274e-10, ...,
        3.07077648e-13, 9.88466978e-01, 1.02275732e-09],
       [3.56270467e-05, 5.81408607e-05, 2.43821193e-05, ...,
        7.16020143e-11, 5.28803610e-08, 9.38665188e-09],
       [6.12040572e-13, 2.48244746e-02, 5.31945389e-01, ...,
        3.08067320e-11, 3.35173942e-05, 9.82545601e-03],
       [4.49037557e-14, 9.99997383e-01, 1.41420331e-18, ...,
        2.69945635e-17, 1.58696386e-14, 2.65523867e-16]]) has invalid type <class 'numpy.ndarray'>, must be a string or Tensor. (Can not convert a ndarray into a Tensor or Operation.)

So my understanding is that the problem (which may not be correct) is, is that the array that I am inputing is just a matrix, (which gets transformed into a numpy.ndarray), and not directly being turned into a Tensor. Since I am trying to feed it in to a "placeholder", the placeholder requires a Tensor object, and it doesn't auto-convert it. However, when I run this in python-tensorflow (exactly copying the sample code into python), I have no problem.

Is there something I am missing? (about tensorflow in general or the r port of it)?

The relevant portion of my code is as follows:

batch_size <- 128

graph <- tf$Graph()
with(graph$as_default(), {
  # Input data. For the training data, we use a placeholder that will be fed
  # at run time with a training minibatch.
  tf_train_dataset <- tf$placeholder(tf$float64, shape=shape(batch_size, image_size * image_size), name = "tf_train_dataset")
  tf_train_labels <- tf$placeholder(tf$float64, shape=shape(batch_size, num_labels), name = "tf_train_labels")
  tf_valid_dataset <-  tf$constant(valid_dataset)
  tf_test_dataset <-  tf$constant(test_dataset)
  # Variables.
  weights <- tf$Variable(tf$truncated_normal(shape(image_size * image_size, num_labels)), name = 'weights')
  biases  <- tf$Variable(tf$zeros(shape(num_labels)), name = "biases")
  weights <- tf$cast(weights, tf$float64)
  biases  <- tf$cast(biases, tf$float64)
  # Training computation.
  logits <- tf$matmul(tf_train_dataset, weights) + biases
  loss <- tf$reduce_mean(tf$nn$softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))
  # Optimizer.
  optimizer <- tf$train$GradientDescentOptimizer(0.5)$minimize(loss)
  # Predictions for the training, validation, and test data.
  train_prediction <- tf$nn$softmax(logits)
  valid_prediction <- tf$nn$softmax(
    tf$matmul(tf_valid_dataset, weights) + biases)
  test_prediction <- tf$nn$softmax(tf$matmul(tf_test_dataset, weights) + biases)
num_steps <- 3001

with(tf$Session(graph = graph) %as% session, {
  for (step in 1:num_steps) {
    # Pick an offset within the training data, which has been randomized.
    # Note: we could use better randomization across epochs.
    offset <- (step * batch_size) %% (dim(train_labels)[1] - batch_size)
    # Generate a minibatch.
    batch_data   <- train_dataset[(offset + 1):(offset + batch_size), ]
    batch_labels <- train_labels[(offset + 1):(offset + batch_size), ]
    # Prepare a dictionary telling the session where to feed the minibatch.
    # The key of the dictionary is the placeholder node of the graph to be fed,
    # and the value is the numpy array to feed to it.
    feed_dict <- dict(tf_train_dataset = batch_data, 
                      tf_train_labels = batch_labels)
    values <- session$run(list(optimizer, loss, train_prediction), feed_dict=feed_dict)
    l <- values[[2]]
    train_prediction <- values[[3]]
    if (step %% 500 == 0) {
      print(paste0("Minibatch loss at step ", step, ": ", l))
      print(paste("Minibatch accuracy:", accuracy(predictions, batch_labels)))
      print(paste("Validation accuracy:", accuracy(valid_prediction$eval(), valid_labels)))
  print(paste("Test accuracy:", accuracy(test_prediction$eval(), test_labels)))

Thanks for all your help!

If possible (and I'm not sure that it is) could you mark your post as its own solution?

To explain my typo/error, in the second section of the code, I incorrectly typed train_prediction <- values[[3]] . I then switched train_predictions to just predictions -- and now it worked perfectly.

1 Like

I tried explaining my error in a different post, and marking that as the solution. Does that work?

Also, for the future, is there a way to delete posts? I couldn't find it...

If you click on the three dots next to the reply button, and a trashcan shows up, then, yes, you can delete the post. However, it seems like the deletion capabilities vary, and like it's really more of a hiding option when available. This site uses Discourse, so the explanations in the meta-discourse forum should apply: